fw_app.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "registers.h"
  34. extern const struct usb_stack_descriptors app_stack_desc;
  35. static void
  36. serial_no_init()
  37. {
  38. uint8_t buf[8];
  39. char *id, *desc;
  40. int i;
  41. flash_manuf_id(buf);
  42. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  43. flash_unique_id(buf);
  44. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  45. // Print Mailbox contents just to be sure */
  46. printf("Mailbox period1: %d\n", mailbox_regs->regs.period1);
  47. /* Overwrite descriptor string */
  48. /* In theory in rodata ... but nothing is ro here */
  49. id = hexstr(buf, 8, false);
  50. desc = (char*)app_stack_desc.str[1];
  51. for (i=0; i<16; i++)
  52. desc[2 + (i << 1)] = id[i];
  53. }
  54. static void write_period1()
  55. {
  56. printf("Writing period1 0x1\n");
  57. mailbox_regs->regs.period1 = 0x1;
  58. printf("Done writing\n");
  59. }
  60. static void write_period1_2()
  61. {
  62. printf("Writing period1 0x2\n");
  63. mailbox_regs->regs.period1 = 0x2;
  64. printf("Done writing\n");
  65. }
  66. static void clear_period1()
  67. {
  68. printf("Clearing period1 current val: %d\n", mailbox_regs->regs.period1);
  69. mailbox_regs->regs.period1 = 0x0;
  70. printf("Done clearing\n");
  71. }
  72. static void
  73. boot_dfu(void)
  74. {
  75. /* Force re-enumeration */
  76. usb_disconnect();
  77. /* Boot firmware */
  78. volatile uint32_t *boot = (void*)0x80000000;
  79. *boot = (1 << 2) | (1 << 0);
  80. }
  81. void
  82. usb_dfu_rt_cb_reboot(void)
  83. {
  84. boot_dfu();
  85. }
  86. void main()
  87. {
  88. int cmd = 0;
  89. /* Init console IO */
  90. console_init();
  91. puts("Booting App image..\n");
  92. /* LED */
  93. led_init();
  94. led_color(48, 96, 5);
  95. led_blink(true, 200, 1000);
  96. led_breathe(true, 100, 200);
  97. led_state(true);
  98. /* SPI */
  99. spi_init();
  100. /* Enable USB directly */
  101. serial_no_init();
  102. usb_init(&app_stack_desc);
  103. usb_dfu_rt_init();
  104. /* Main loop */
  105. while (1)
  106. {
  107. /* Prompt ? */
  108. if (cmd >= 0)
  109. printf("Command> ");
  110. /* Poll for command */
  111. cmd = getchar_nowait();
  112. if (cmd >= 0) {
  113. if (cmd > 32 && cmd < 127) {
  114. putchar(cmd);
  115. putchar('\r');
  116. putchar('\n');
  117. }
  118. switch (cmd)
  119. {
  120. case 'p':
  121. usb_debug_print();
  122. break;
  123. case 'b':
  124. boot_dfu();
  125. break;
  126. case 'c':
  127. usb_connect();
  128. break;
  129. case 'd':
  130. usb_disconnect();
  131. break;
  132. case 'w':
  133. write_period1();
  134. break;
  135. case 'k':
  136. clear_period1();
  137. break;
  138. case 'a':
  139. write_period1_2();
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. /* USB poll */
  146. usb_poll();
  147. }
  148. }