fw_app.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /*
  36. double dupa;
  37. float kupa;
  38. */
  39. static void
  40. serial_no_init()
  41. {
  42. uint8_t buf[8];
  43. char *id, *desc;
  44. int i;
  45. flash_manuf_id(buf);
  46. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  47. flash_unique_id(buf);
  48. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  49. // Print Mailbox contents just to be sure */
  50. printf("Mailbox period1: %d\n", mailbox_regs->regs.period1);
  51. printf("Mailbox delay1: %d\n", mailbox_regs->regs.delay1);
  52. /* Testing floats TODO: test if calculation is correct
  53. dupa = 3.1415;
  54. kupa = 2.74f;
  55. dupa *= kupa;
  56. printf("Pi * e: %.3f\n", dupa);
  57. */
  58. /* Overwrite descriptor string */
  59. /* In theory in rodata ... but nothing is ro here */
  60. id = hexstr(buf, 8, false);
  61. desc = (char*)app_stack_desc.str[1];
  62. for (i=0; i<16; i++)
  63. desc[2 + (i << 1)] = id[i];
  64. }
  65. static void write_period1()
  66. {
  67. printf("Writing period1 0x1\n");
  68. mailbox_regs->regs.period1 = 0x1;
  69. printf("Done writing\n");
  70. }
  71. static void write_period1_2()
  72. {
  73. printf("Writing period1 0x2\n");
  74. mailbox_regs->regs.period1 = 0x2;
  75. printf("Done writing\n");
  76. }
  77. static void write_period1_4()
  78. {
  79. printf("Writing period1 0x4\n");
  80. mailbox_regs->regs.period1 = 0x3;
  81. printf("Done writing\n");
  82. printf("Reading now \n");
  83. printf("value: %d\n", mailbox_regs->regs.period1);
  84. }
  85. static void clear_period1()
  86. {
  87. printf("Clearing period1 val: %d\n", mailbox_regs->regs.period1);
  88. mailbox_regs->regs.period1 = 0x0;
  89. printf("Done clearing\n");
  90. }
  91. static void write_delay1()
  92. {
  93. printf("Writing delay1 0x1\n");
  94. mailbox_regs->regs.delay1 = 0x1;
  95. printf("Done writing\n");
  96. }
  97. static void clear_delay1()
  98. {
  99. printf("Clearing delay1 val: %d\n", mailbox_regs->regs.delay1);
  100. mailbox_regs->regs.delay1 = 0x0;
  101. printf("Done clearing\n");
  102. }
  103. static void
  104. boot_dfu(void)
  105. {
  106. /* Force re-enumeration */
  107. usb_disconnect();
  108. /* Boot firmware */
  109. volatile uint32_t *boot = (void*)0x80000000;
  110. *boot = (1 << 2) | (1 << 0);
  111. }
  112. void
  113. usb_dfu_rt_cb_reboot(void)
  114. {
  115. boot_dfu();
  116. }
  117. void main()
  118. {
  119. int cmd = 0;
  120. /* Init console IO */
  121. console_init();
  122. puts("Booting App image..\n");
  123. /* LED */
  124. led_init();
  125. led_color(48, 96, 5);
  126. led_blink(true, 200, 1000);
  127. led_breathe(true, 100, 200);
  128. led_state(true);
  129. /* SPI */
  130. spi_init();
  131. /* Enable USB directly */
  132. serial_no_init();
  133. usb_init(&app_stack_desc);
  134. usb_dfu_rt_init();
  135. /* Main loop */
  136. while (1)
  137. {
  138. /* Prompt ? */
  139. if (cmd >= 0)
  140. printf("Command> ");
  141. /* Poll for command */
  142. cmd = getchar_nowait();
  143. if (cmd >= 0) {
  144. if (cmd > 32 && cmd < 127) {
  145. putchar(cmd);
  146. putchar('\r');
  147. putchar('\n');
  148. }
  149. switch (cmd)
  150. {
  151. case 'p':
  152. usb_debug_print();
  153. break;
  154. case 'b':
  155. boot_dfu();
  156. break;
  157. case 'c':
  158. usb_connect();
  159. break;
  160. case 'd':
  161. usb_disconnect();
  162. break;
  163. case 'w':
  164. write_period1();
  165. break;
  166. case 'k':
  167. clear_period1();
  168. break;
  169. case 'a':
  170. write_period1_2();
  171. break;
  172. case 's':
  173. write_period1_4();
  174. break;
  175. //case 'o':
  176. // write_delay1();
  177. // break;
  178. //case 'm':
  179. // clear_delay1();
  180. // break;
  181. default:
  182. break;
  183. }
  184. }
  185. /* USB poll */
  186. usb_poll();
  187. }
  188. }