fw_app.c 7.8 KB

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