fw_app.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "console.h"
  32. #include "led.h"
  33. #include "mini-printf.h"
  34. #include "spi.h"
  35. #include "utils.h"
  36. #include "config.h"
  37. extern const struct usb_stack_descriptors app_stack_desc;
  38. static void
  39. serial_no_init()
  40. {
  41. uint8_t buf[8];
  42. char *id, *desc;
  43. int i;
  44. flash_manuf_id(buf);
  45. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  46. flash_unique_id(buf);
  47. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  48. /* Overwrite descriptor string */
  49. /* In theory in rodata ... but nothing is ro here */
  50. id = hexstr(buf, 8, false);
  51. desc = (char*)app_stack_desc.str[1];
  52. for (i=0; i<16; i++)
  53. desc[2 + (i << 1)] = id[i];
  54. }
  55. static void
  56. boot_dfu(void)
  57. {
  58. /* Force re-enumeration */
  59. usb_disconnect();
  60. /* Boot firmware */
  61. volatile uint32_t *boot = (void*)0x80000000;
  62. *boot = (1 << 2) | (1 << 0);
  63. }
  64. void
  65. usb_dfu_rt_cb_reboot(void)
  66. {
  67. boot_dfu();
  68. }
  69. void
  70. main()
  71. {
  72. int cmd = 0;
  73. /* Init console IO */
  74. console_init();
  75. puts("Booting Audio image..\n");
  76. /* LED */
  77. led_init();
  78. led_color(48, 96, 5);
  79. led_blink(true, 200, 1000);
  80. led_breathe(true, 100, 200);
  81. led_state(true);
  82. /* SPI */
  83. spi_init();
  84. /* Enable USB directly */
  85. serial_no_init();
  86. usb_init(&app_stack_desc);
  87. usb_dfu_rt_init();
  88. /* Audio */
  89. audio_init();
  90. /* Connect */
  91. usb_connect();
  92. /* Main loop */
  93. while (1)
  94. {
  95. /* Prompt ? */
  96. if (cmd >= 0)
  97. printf("Command> ");
  98. /* Poll for command */
  99. cmd = getchar_nowait();
  100. if (cmd >= 0) {
  101. if (cmd > 32 && cmd < 127) {
  102. putchar(cmd);
  103. putchar('\r');
  104. putchar('\n');
  105. }
  106. switch (cmd)
  107. {
  108. case 'p':
  109. audio_debug_print();
  110. break;
  111. case 'b':
  112. boot_dfu();
  113. break;
  114. case 'c':
  115. usb_connect();
  116. break;
  117. case 'd':
  118. usb_disconnect();
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. /* USB poll */
  125. usb_poll();
  126. audio_poll();
  127. }
  128. }