fw_app.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * fw_app.c
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include <no2usb/usb.h>
  27. #include <no2usb/usb_dfu_rt.h>
  28. #include <no2usb/usb_hw.h>
  29. #include <no2usb/usb_priv.h>
  30. #include "audio.h"
  31. #include "cdc-dlm.h"
  32. #include "console.h"
  33. #include "led.h"
  34. #include "mc97.h"
  35. #include "mini-printf.h"
  36. #include "spi.h"
  37. #include "utils.h"
  38. #include "config.h"
  39. extern const struct usb_stack_descriptors app_stack_desc;
  40. static void
  41. serial_no_init()
  42. {
  43. uint8_t buf[8];
  44. char *id, *desc;
  45. int i;
  46. flash_manuf_id(buf);
  47. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  48. flash_unique_id(buf);
  49. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  50. /* Overwrite descriptor string */
  51. /* In theory in rodata ... but nothing is ro here */
  52. id = hexstr(buf, 8, false);
  53. desc = (char*)app_stack_desc.str[1];
  54. for (i=0; i<16; i++)
  55. desc[2 + (i << 1)] = id[i];
  56. }
  57. static void
  58. boot_dfu(void)
  59. {
  60. /* Force re-enumeration */
  61. usb_disconnect();
  62. /* Boot firmware */
  63. volatile uint32_t *boot = (void*)0x80000000;
  64. *boot = (1 << 2) | (1 << 0);
  65. }
  66. void
  67. usb_dfu_rt_cb_reboot(void)
  68. {
  69. boot_dfu();
  70. }
  71. void
  72. main()
  73. {
  74. int cmd = 0;
  75. /* Init console IO */
  76. console_init();
  77. puts("Booting Audio image..\n");
  78. /* LED */
  79. led_init();
  80. led_color(48, 96, 5);
  81. led_blink(true, 200, 1000);
  82. led_breathe(true, 100, 200);
  83. led_state(true);
  84. /* SPI */
  85. spi_init();
  86. /* MC97 link */
  87. mc97_init();
  88. /* Init USB stack */
  89. serial_no_init();
  90. usb_init(&app_stack_desc);
  91. usb_dfu_rt_init();
  92. /* Init class drivers */
  93. audio_init();
  94. cdc_dlm_init();
  95. /* Connect */
  96. usb_connect();
  97. /* Main loop */
  98. while (1)
  99. {
  100. /* Prompt ? */
  101. if (cmd >= 0)
  102. printf("Command> ");
  103. /* Poll for command */
  104. cmd = getchar_nowait();
  105. if (cmd >= 0) {
  106. if (cmd > 32 && cmd < 127) {
  107. putchar(cmd);
  108. putchar('\r');
  109. putchar('\n');
  110. }
  111. switch (cmd)
  112. {
  113. case 'i': mc97_init(); break;
  114. case 'p': mc97_debug(); break;
  115. case 'r': mc97_set_aux_relay(false); break;
  116. case 'R': mc97_set_aux_relay(true); break;
  117. case 'h': mc97_set_hook(ON_HOOK); break;
  118. case 'H': mc97_set_hook(OFF_HOOK); break;
  119. case 'C': mc97_set_hook(CALLER_ID); break;
  120. case 'n': mc97_test_ring(); break;
  121. case '0': mc97_set_loopback(MC97_LOOPBACK_NONE); break;
  122. case '1': mc97_set_loopback(MC97_LOOPBACK_DIGITAL_ADC); break;
  123. case '2': mc97_set_loopback(MC97_LOOPBACK_ANALOG_LOCAL); break;
  124. case '3': mc97_set_loopback(MC97_LOOPBACK_DIGITAL_DAC); break;
  125. case '4': mc97_set_loopback(MC97_LOOPBACK_ANALOG_REMOTE); break;
  126. case '5': mc97_set_loopback(MC97_LOOPBACK_ISOCAP); break;
  127. case '6': mc97_set_loopback(MC97_LOOPBACK_ANALOG_EXTERNAL); break;
  128. case 's':
  129. for (int i=0; i<128; i+=2)
  130. printf("%02x: %04x\n", i, mc97_codec_reg_read(i));
  131. break;
  132. case 'b':
  133. boot_dfu();
  134. break;
  135. case 'c':
  136. usb_connect();
  137. break;
  138. case 'd':
  139. usb_disconnect();
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. /* USB poll */
  146. usb_poll();
  147. audio_poll();
  148. cdc_dlm_poll();
  149. }
  150. }