fw_app.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "utils.h"
  31. #include "registers.h"
  32. #define FLASH_MAILBOX_STORAGE 0x0F0000
  33. /*
  34. double dupa;
  35. float kupa;
  36. */
  37. static uint32_t copy_array[16];
  38. static inline uint32_t rdcycle(void)
  39. {
  40. uint32_t cycle;
  41. __asm__ volatile ("rdcycle %0" : "=r" (cycle));
  42. return cycle;
  43. }
  44. /* Timer fuction */
  45. #define CYCLES_DIV 24000000.0f
  46. #define UPDATE_MS 3.0f
  47. // TODO: integers not floats?
  48. static uint32_t last_rdcycle;
  49. static bool timer_expired()
  50. {
  51. bool success = false;
  52. uint32_t current = rdcycle();
  53. uint32_t diff = current - last_rdcycle;
  54. //printf(" diff %d last: %d current: %d\n", diff, last_rdcycle, current);
  55. // TODO: overflow handling
  56. float elapsed_seconds = (float)(diff) / CYCLES_DIV;
  57. if (elapsed_seconds > UPDATE_MS)
  58. {
  59. last_rdcycle = current;
  60. printf("Timer expired!\n");
  61. success = true;;
  62. }
  63. return success;
  64. }
  65. /* three signal generation params */
  66. static void update_three_signal_values()
  67. {
  68. //printf("Updating 3signal values\n");
  69. }
  70. static void print_mailbox_contents()
  71. {
  72. int i = 0;
  73. printf("Mailbox contents:\n");
  74. for(i = 0; i < 16; ++i)
  75. printf("Mailbox[%d]: %d\n", i, mailbox_regs->data[i]);
  76. }
  77. static void store_mailbox_regs_flash()
  78. {
  79. // Copy current mailbox array to a temporary array for bulk memory write
  80. // our Wishbone implementation does not support bulk writes
  81. for (int i = 0; i < 16; ++i)
  82. copy_array[i] = mailbox_regs->data[i];
  83. flash_write_enable();
  84. flash_sector_erase(FLASH_MAILBOX_STORAGE);
  85. // Wait for flash to finish (poll status register)
  86. while (flash_read_sr() & 0x01) /* WIP bit */;
  87. flash_write_enable();
  88. flash_page_program((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
  89. while (flash_read_sr() & 0x01) /* WIP bit */;
  90. }
  91. static void read_mailbox_regs_flash()
  92. {
  93. flash_read((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
  94. // Copy data to actual mailbox
  95. for (int i = 0; i < 16; ++i)
  96. mailbox_regs->data[i] = copy_array[i];
  97. }
  98. static void dummy_write_to_flash()
  99. {
  100. printf("Dummy writing to flash\n");
  101. int i = 0;
  102. for (i = 0; i < 16; ++i)
  103. {
  104. mailbox_regs->data[i] = 0x7890;
  105. }
  106. print_mailbox_contents();
  107. store_mailbox_regs_flash();
  108. }
  109. static void
  110. serial_no_init()
  111. {
  112. uint8_t buf[8];
  113. flash_manuf_id(buf);
  114. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  115. flash_unique_id(buf);
  116. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  117. //dummy_write_to_flash();
  118. /* Testing floats TODO: test if calculation is correct
  119. dupa = 3.1415;
  120. kupa = 2.74f;
  121. dupa *= kupa;
  122. printf("Pi * e: %.3f\n", dupa);
  123. */
  124. }
  125. //static char _printf_buf[128];
  126. static void write_period1()
  127. {
  128. printf("Writing period1 0x1\n");
  129. mailbox_regs->regs.period1 = 0x1;
  130. printf("Done writing\n");
  131. print_mailbox_contents();
  132. store_mailbox_regs_flash();
  133. }
  134. static void write_period1_2()
  135. {
  136. printf("Writing period1 0x2\n");
  137. mailbox_regs->regs.period1 = 0x2;
  138. printf("Done writing\n");
  139. print_mailbox_contents();
  140. store_mailbox_regs_flash();
  141. }
  142. static void write_period1_4()
  143. {
  144. printf("Writing period1 0x4\n");
  145. mailbox_regs->regs.period1 = 0x4;
  146. printf("Done writing\n");
  147. printf("Reading now \n");
  148. printf("value: %d\n", mailbox_regs->regs.period1);
  149. print_mailbox_contents();
  150. store_mailbox_regs_flash();
  151. }
  152. static void clear_period1()
  153. {
  154. printf("Clearing period1 val: %d\n", mailbox_regs->regs.period1);
  155. mailbox_regs->regs.period1 = 0x0;
  156. printf("Done clearing\n");
  157. }
  158. static void write_delay1()
  159. {
  160. printf("Writing delay1 0x1234\n");
  161. mailbox_regs->regs.delay1 = 0x1234;
  162. printf("Done writing\n");
  163. store_mailbox_regs_flash();
  164. }
  165. static void clear_delay1()
  166. {
  167. printf("Clearing delay1 val: %d\n", mailbox_regs->regs.delay1);
  168. mailbox_regs->regs.delay1 = 0x0;
  169. printf("Done clearing\n");
  170. }
  171. static void print_flash_contents()
  172. {
  173. read_mailbox_regs_flash();
  174. print_mailbox_contents();
  175. }
  176. static void
  177. boot_dfu(void)
  178. {
  179. /* Boot firmware */
  180. volatile uint32_t *boot = (void*)0x80000000;
  181. *boot = (1 << 2) | (1 << 0);
  182. }
  183. void
  184. usb_dfu_rt_cb_reboot(void)
  185. {
  186. boot_dfu();
  187. }
  188. void main()
  189. {
  190. int cmd = 0;
  191. /* Init console IO */
  192. console_init();
  193. puts("Booting App image..\n");
  194. /* LED */
  195. led_init();
  196. led_color(48, 96, 5);
  197. led_blink(true, 200, 1000);
  198. led_breathe(true, 100, 200);
  199. led_state(true);
  200. /* SPI */
  201. spi_init();
  202. /* Enable USB directly */
  203. serial_no_init();
  204. /* Main loop */
  205. while (1)
  206. {
  207. /* Run the timer for button updates periodically */
  208. if (timer_expired())
  209. update_three_signal_values();
  210. /* Prompt ? */
  211. if (cmd >= 0)
  212. printf("Command> ");
  213. /* Poll for command */
  214. cmd = getchar_nowait();
  215. if (cmd >= 0) {
  216. if (cmd > 32 && cmd < 127) {
  217. putchar(cmd);
  218. putchar('\r');
  219. putchar('\n');
  220. }
  221. switch (cmd)
  222. {
  223. case 'q':
  224. printf("Current rdcycle %d\n", rdcycle());
  225. break;
  226. case 'b':
  227. boot_dfu();
  228. break;
  229. case 'w':
  230. write_period1();
  231. break;
  232. case 'k':
  233. clear_period1();
  234. break;
  235. case 'a':
  236. write_period1_2();
  237. break;
  238. case 's':
  239. write_period1_4();
  240. break;
  241. case 'o':
  242. write_delay1();
  243. break;
  244. case 'm':
  245. clear_delay1();
  246. break;
  247. case 'f':
  248. print_flash_contents();
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. }
  255. }