fw_app.c 5.6 KB

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