fw_app.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 void print_mailbox_contents()
  39. {
  40. int i = 0;
  41. printf("Mailbox contents:\n");
  42. for(i = 0; i < 16; ++i)
  43. printf("Mailbox[%d]: %d\n", i, mailbox_regs->data[i]);
  44. }
  45. static void store_mailbox_regs_flash()
  46. {
  47. // Copy current mailbox array to a temporary array for bulk memory write
  48. // our Wishbone implementation does not support bulk writes
  49. for (int i = 0; i < 16; ++i)
  50. copy_array[i] = mailbox_regs->data[i];
  51. flash_write_enable();
  52. flash_sector_erase(FLASH_MAILBOX_STORAGE);
  53. // Wait for flash to finish (poll status register)
  54. while (flash_read_sr() & 0x01) /* WIP bit */;
  55. flash_write_enable();
  56. flash_page_program((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
  57. while (flash_read_sr() & 0x01) /* WIP bit */;
  58. }
  59. static void read_mailbox_regs_flash()
  60. {
  61. flash_read((void*)copy_array, FLASH_MAILBOX_STORAGE, sizeof(copy_array));
  62. // Copy data to actual mailbox
  63. for (int i = 0; i < 16; ++i)
  64. mailbox_regs->data[i] = copy_array[i];
  65. }
  66. static void dummy_write_to_flash()
  67. {
  68. printf("Dummy writing to flash\n");
  69. int i = 0;
  70. for (i = 0; i < 16; ++i)
  71. {
  72. mailbox_regs->data[i] = 0x7890;
  73. }
  74. print_mailbox_contents();
  75. store_mailbox_regs_flash();
  76. }
  77. static void
  78. serial_no_init()
  79. {
  80. uint8_t buf[8];
  81. flash_manuf_id(buf);
  82. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  83. flash_unique_id(buf);
  84. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  85. //dummy_write_to_flash();
  86. /* Testing floats TODO: test if calculation is correct
  87. dupa = 3.1415;
  88. kupa = 2.74f;
  89. dupa *= kupa;
  90. printf("Pi * e: %.3f\n", dupa);
  91. */
  92. }
  93. //static char _printf_buf[128];
  94. static void write_period1()
  95. {
  96. printf("Writing period1 0x1\n");
  97. mailbox_regs->regs.period1 = 0x1;
  98. printf("Done writing\n");
  99. print_mailbox_contents();
  100. store_mailbox_regs_flash();
  101. }
  102. static void write_period1_2()
  103. {
  104. printf("Writing period1 0x2\n");
  105. mailbox_regs->regs.period1 = 0x2;
  106. printf("Done writing\n");
  107. print_mailbox_contents();
  108. store_mailbox_regs_flash();
  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. print_mailbox_contents();
  118. store_mailbox_regs_flash();
  119. }
  120. static void clear_period1()
  121. {
  122. printf("Clearing period1 val: %d\n", mailbox_regs->regs.period1);
  123. mailbox_regs->regs.period1 = 0x0;
  124. printf("Done clearing\n");
  125. }
  126. static void write_delay1()
  127. {
  128. printf("Writing delay1 0x1234\n");
  129. mailbox_regs->regs.delay1 = 0x1234;
  130. printf("Done writing\n");
  131. store_mailbox_regs_flash();
  132. }
  133. static void clear_delay1()
  134. {
  135. printf("Clearing delay1 val: %d\n", mailbox_regs->regs.delay1);
  136. mailbox_regs->regs.delay1 = 0x0;
  137. printf("Done clearing\n");
  138. }
  139. static void print_flash_contents()
  140. {
  141. read_mailbox_regs_flash();
  142. print_mailbox_contents();
  143. }
  144. static void
  145. boot_dfu(void)
  146. {
  147. /* Boot firmware */
  148. volatile uint32_t *boot = (void*)0x80000000;
  149. *boot = (1 << 2) | (1 << 0);
  150. }
  151. void
  152. usb_dfu_rt_cb_reboot(void)
  153. {
  154. boot_dfu();
  155. }
  156. void main()
  157. {
  158. int cmd = 0;
  159. /* Init console IO */
  160. console_init();
  161. puts("Booting App image..\n");
  162. /* LED */
  163. led_init();
  164. led_color(48, 96, 5);
  165. led_blink(true, 200, 1000);
  166. led_breathe(true, 100, 200);
  167. led_state(true);
  168. /* SPI */
  169. spi_init();
  170. /* Enable USB directly */
  171. serial_no_init();
  172. /* Main loop */
  173. while (1)
  174. {
  175. /* Prompt ? */
  176. if (cmd >= 0)
  177. printf("Command> ");
  178. /* Poll for command */
  179. cmd = getchar_nowait();
  180. if (cmd >= 0) {
  181. if (cmd > 32 && cmd < 127) {
  182. putchar(cmd);
  183. putchar('\r');
  184. putchar('\n');
  185. }
  186. switch (cmd)
  187. {
  188. case 'b':
  189. boot_dfu();
  190. break;
  191. case 'w':
  192. write_period1();
  193. break;
  194. case 'k':
  195. clear_period1();
  196. break;
  197. case 'a':
  198. write_period1_2();
  199. break;
  200. case 's':
  201. write_period1_4();
  202. break;
  203. case 'o':
  204. write_delay1();
  205. break;
  206. case 'm':
  207. clear_delay1();
  208. break;
  209. case 'f':
  210. print_flash_contents();
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. }
  217. }