fw_app.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. /* Local copy of last button register values */
  43. static button_regs_t last_button_regs;
  44. /* Disabled as the clock constraints won't accept it
  45. static inline uint32_t rdcycle(void)
  46. {
  47. uint32_t cycle;
  48. __asm__ volatile ("rdcycle %0" : "=r" (cycle));
  49. return cycle;
  50. }
  51. */
  52. static inline uint32_t my_rdcycle(void)
  53. {
  54. volatile uint32_t *timer_reg = (void*)TIMER_BASE;
  55. return *timer_reg;
  56. }
  57. /* Timer fuction */
  58. #define CYCLES_DIV 24000000.0f
  59. #define UPDATE_S 0.1f
  60. #define DEBUG_PRINT_S 3.0f
  61. // TODO: integers not floats?
  62. static uint32_t last_rdcycle;
  63. static uint32_t last_rdcycle_debug;
  64. static bool timer_expired(uint32_t * last_rdcycle, float timeout)
  65. {
  66. bool success = false;
  67. uint32_t current = my_rdcycle();
  68. uint32_t diff = current - *last_rdcycle;
  69. float elapsed_seconds = (float)(diff) / CYCLES_DIV;
  70. if (elapsed_seconds > timeout)
  71. {
  72. //printf("Timeout expired rdcycle: %d last %d\n", current, *last_rdcycle);
  73. *last_rdcycle = current;
  74. success = true;
  75. }
  76. return success;
  77. }
  78. static void write_regs_to_mailbox()
  79. {
  80. mailbox_regs->regs.period1 = 9900; //out_params.regs._period1;
  81. // TODO: seems like period2 etc are missing
  82. mailbox_regs->regs.delay1 = 4000; //out_params.regs._duty1;
  83. mailbox_regs->regs.period2 = 9000; //out_params.regs._period2;
  84. mailbox_regs->regs.delay2 = 3000; //out_params.regs._duty2;
  85. mailbox_regs->regs.period3 = 100;
  86. mailbox_regs->regs.duty3 = 50;
  87. //mailbox_regs->regs.delay3 = out_params.regs.D3 / FXP_SCALING;
  88. mailbox_regs->regs.npuls3 = 5; //out_params.regs.N3;
  89. mailbox_regs->regs.odd_train_flag = N_PULSE_TRAINS; // TODO: is this proper?
  90. }
  91. static void print_mailbox_contents()
  92. {
  93. int i = 0;
  94. printf("Mailbox contents:\n");
  95. for(i = 0; i < 16; ++i)
  96. printf("Mailbox[%d]: %d\n", i, mailbox_regs->data[i]);
  97. }
  98. static void print_kbrd(const button_deltas_t *pDeltas)
  99. {
  100. //printf("f1 : %d\n", pDeltas->inc_f1 );
  101. //printf("d1 : %d\n", pDeltas->inc_d1 );
  102. //printf("d2 : %d\n", pDeltas->inc_d2 );
  103. //printf("ph2: %d\n", pDeltas->inc_ph2);
  104. //printf("ph3: %d\n", pDeltas->inc_ph3);
  105. //printf("f3 : %d\n", pDeltas->inc_f3 );
  106. //printf("d3 : %d\n", pDeltas->inc_d3 );
  107. //printf("n3 : %d\n", pDeltas->inc_n3 );
  108. printf("mailbox_button_regs: 0x%08x\n", mailbox_button_regs);
  109. for(int i=0;i<16;i++){
  110. printf("butt[%d]=%d\n", i, mailbox_button_regs->data[i]);
  111. }
  112. }
  113. static void print_params(tweaked_params_s *pParams)
  114. {
  115. printf("User params:\n");
  116. printf("f1: %d\n", pParams->f1);
  117. printf("_period1: %d\n", pParams->_period1);
  118. printf("D1: %d\n", pParams->D1);
  119. printf("_duty1: %d\n", pParams->_duty1);
  120. printf("D2: %d\n", pParams->D2);
  121. printf("_duty2: %d\n", pParams->_duty2);
  122. printf("Ph2: %d\n", pParams->Ph2);
  123. printf("_phase2: %d\n", pParams->_phase2);
  124. printf("Ph3: %d\n", pParams->Ph3);
  125. printf("f3: %d\n", pParams->f3);
  126. printf("_period3: %d\n", pParams->_period3);
  127. printf("D3: %d\n", pParams->D3);
  128. printf("N3: %d\n", pParams->N3);
  129. printf("ena: %d\n", pParams->ena);
  130. }
  131. static void read_generation_values_from_flash()
  132. {
  133. flash_read((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
  134. // Copy data to actual registers
  135. for (int i = 0; i < 14; ++i)
  136. out_params.data[i] = copy_array[i];
  137. }
  138. static void store_generation_values_flash()
  139. {
  140. printf("store_generation_values_flash()\n");
  141. printf("WARNING - IMPLEMENTATION DISABLED\n");
  142. // // TODO: can we remove it already as we don't read directly to mailbox
  143. // // Copy current mailbox array to a temporary array for bulk memory write
  144. // // our Wishbone implementation does not support bulk writes
  145. // for (int i = 0; i < 14; ++i)
  146. // copy_array[i] = out_params.data[i];
  147. //
  148. // flash_write_enable();
  149. // flash_sector_erase(FLASH_MAILBOX_STORAGE);
  150. // // Wait for flash to finish (poll status register)
  151. // while (flash_read_sr() & 0x01) /* WIP bit */;
  152. // flash_write_enable();
  153. //
  154. // flash_page_program((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
  155. // while (flash_read_sr() & 0x01) /* WIP bit */;
  156. }
  157. static void
  158. serial_no_init()
  159. {
  160. uint8_t buf[8];
  161. flash_manuf_id(buf);
  162. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  163. flash_unique_id(buf);
  164. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  165. //dummy_write_to_flash();
  166. /* Testing floats TODO: test if calculation is correct
  167. dupa = 3.1415;
  168. kupa = 2.74f;
  169. dupa *= kupa;
  170. printf("Pi * e: %.3f\n", dupa);
  171. */
  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. /* SPI */
  197. spi_init();
  198. /* Enable USB directly */
  199. serial_no_init();
  200. /* Read generation reg values from flash */
  201. read_generation_values_from_flash();
  202. /* If values are wrong - fill defaults */
  203. if (validate_generation_values(&out_params.regs) != 0)
  204. init_params(&out_params.regs);
  205. write_regs_to_mailbox();
  206. /* Main loop */
  207. while (1)
  208. {
  209. /* Run the timer for button updates periodically */
  210. if (timer_expired(&last_rdcycle, UPDATE_S))
  211. {
  212. button_deltas_t button_deltas = {};
  213. obtain_button_deltas(mailbox_button_regs, &last_button_regs, &button_deltas);
  214. print_kbrd(&button_deltas);
  215. // button registers only show the delta between last and current value - we don't reset them explicitly
  216. update_three_signal_values(&out_params.regs, &button_deltas);
  217. write_regs_to_mailbox();
  218. // TODO: how do we use this? clarify!
  219. if (mailbox_regs->regs.write_flash)
  220. {
  221. store_generation_values_flash();
  222. mailbox_regs->regs.write_flash = 0x0;
  223. }
  224. }
  225. /* Debug timer */
  226. if (timer_expired(&last_rdcycle_debug, DEBUG_PRINT_S))
  227. {
  228. // Print user params
  229. print_params(&out_params.regs);
  230. }
  231. #ifdef USE_KEYBOARD
  232. /* Prompt ? */
  233. if (cmd >= 0)
  234. printf("Command> ");
  235. /* Poll for command */
  236. cmd = getchar_nowait();
  237. if (cmd >= 0) {
  238. if (cmd > 32 && cmd < 127) {
  239. putchar(cmd);
  240. putchar('\r');
  241. putchar('\n');
  242. }
  243. switch (cmd)
  244. {
  245. case '.':
  246. printf("Current rdcycle %d\n", my_rdcycle());
  247. break;
  248. case '/':
  249. boot_dfu();
  250. break;
  251. case 's':
  252. inc_D1(&out_params.regs, 1);
  253. break;
  254. case 'x':
  255. dec_D1(&out_params.regs, 1);
  256. break;
  257. case 'd':
  258. inc_D2(&out_params.regs, 1);
  259. break;
  260. case 'c':
  261. dec_D2(&out_params.regs, 1);
  262. break;
  263. case 'f':
  264. inc_f1(&out_params.regs, 1);
  265. break;
  266. case 'v':
  267. dec_f1(&out_params.regs, 1);
  268. break;
  269. case 'a':
  270. inc_Ph2(&out_params.regs, 1);
  271. break;
  272. case 'z':
  273. dec_Ph2(&out_params.regs, 1);
  274. break;
  275. case 'g':
  276. inc_f3(&out_params.regs, 1);
  277. break;
  278. case 'b':
  279. dec_f3(&out_params.regs, 1);
  280. break;
  281. case 'k':
  282. inc_Ph3(&out_params.regs, 1);
  283. break;
  284. case ',':
  285. dec_Ph3(&out_params.regs, 1);
  286. break;
  287. case 'j':
  288. inc_D3(&out_params.regs, 1);
  289. break;
  290. case 'm':
  291. dec_D3(&out_params.regs, 1);
  292. break;
  293. case 'h':
  294. inc_N3(&out_params.regs, 1);
  295. break;
  296. case 'n':
  297. dec_N3(&out_params.regs, 1);
  298. break;
  299. case 'e':
  300. ena_force_toggle(&out_params.regs);
  301. break;
  302. case ']':
  303. print_flash_contents();
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. #endif
  310. }
  311. }