fw_app.c 2.6 KB

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