fw_app.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* Enable USB directly */
  87. serial_no_init();
  88. usb_init(&app_stack_desc);
  89. usb_dfu_rt_init();
  90. /* Init class drivers */
  91. audio_init();
  92. cdc_dlm_init();
  93. /* Connect */
  94. usb_connect();
  95. /* Main loop */
  96. while (1)
  97. {
  98. /* Prompt ? */
  99. if (cmd >= 0)
  100. printf("Command> ");
  101. /* Poll for command */
  102. cmd = getchar_nowait();
  103. if (cmd >= 0) {
  104. if (cmd > 32 && cmd < 127) {
  105. putchar(cmd);
  106. putchar('\r');
  107. putchar('\n');
  108. }
  109. switch (cmd)
  110. {
  111. case 'i': mc97_init(); break;
  112. case 'p': mc97_debug(); break;
  113. case 'r': mc97_set_aux_relay(false); break;
  114. case 'R': mc97_set_aux_relay(true); break;
  115. case 'h': mc97_set_hook(ON_HOOK); break;
  116. case 'H': mc97_set_hook(OFF_HOOK); break;
  117. case 'C': mc97_set_hook(CALLER_ID); break;
  118. case 'n': mc97_test_ring(); break;
  119. case '0': mc97_set_loopback(MC97_LOOPBACK_NONE); break;
  120. case '1': mc97_set_loopback(MC97_LOOPBACK_DIGITAL_ADC); break;
  121. case '2': mc97_set_loopback(MC97_LOOPBACK_ANALOG_LOCAL); break;
  122. case '3': mc97_set_loopback(MC97_LOOPBACK_DIGITAL_DAC); break;
  123. case '4': mc97_set_loopback(MC97_LOOPBACK_ANALOG_REMOTE); break;
  124. case '5': mc97_set_loopback(MC97_LOOPBACK_ISOCAP); break;
  125. case '6': mc97_set_loopback(MC97_LOOPBACK_ANALOG_EXTERNAL); break;
  126. case 's':
  127. for (int i=0; i<128; i+=2)
  128. printf("%02x: %04x\n", i, mc97_codec_reg_read(i));
  129. break;
  130. case 'b':
  131. boot_dfu();
  132. break;
  133. case 'c':
  134. usb_connect();
  135. break;
  136. case 'd':
  137. usb_disconnect();
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. /* USB poll */
  144. usb_poll();
  145. audio_poll();
  146. cdc_dlm_poll();
  147. }
  148. }