fw_app.c 7.0 KB

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