fw_app.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /*
  33. double dupa;
  34. float kupa;
  35. */
  36. static void
  37. serial_no_init()
  38. {
  39. uint8_t buf[8];
  40. //char *id, *desc;
  41. //int i;
  42. flash_manuf_id(buf);
  43. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  44. flash_unique_id(buf);
  45. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  46. //uint32_t old = *(volatile uint32_t *)(MAILBOX_BASE + 0x00);
  47. //printf("Old=0x%x\n", old);
  48. //*(volatile uint32_t *)(MAILBOX_BASE + 0x00) = 0x00000004;
  49. //uint32_t new = *(volatile uint32_t *)(MAILBOX_BASE + 0x00);
  50. //printf("New=0x%x\n", new);
  51. //printf("Old=0x%x, New=0x%x\n", old, new);
  52. // Print Mailbox contents just to be sure */
  53. //printf("Mailbox period1: %d\n", mailbox_regs->regs.period1);
  54. //printf("Mailbox delay1: %d\n", mailbox_regs->regs.delay1);
  55. //// Debug print all mailbox memory
  56. //for (i = 0; i < 64; ++i) {
  57. // //printf("%x ", *((int*)MAILBOX_BASE+i));
  58. // printf("%x ", *((int*)MAILBOX_BASE+i));
  59. // if (i != 0 && i % 16)
  60. // printf("\n");
  61. //}
  62. //mailbox_regs->regs.delay3= 0x1;
  63. //printf("Mailbox delay1: %d\n", mailbox_regs->regs.delay3);
  64. /* Testing floats TODO: test if calculation is correct
  65. dupa = 3.1415;
  66. kupa = 2.74f;
  67. dupa *= kupa;
  68. printf("Pi * e: %.3f\n", dupa);
  69. */
  70. }
  71. //static char _printf_buf[128];
  72. static void write_period1()
  73. {
  74. printf("Writing period1 0x1\n");
  75. //uint8_t *strMem = (uint8_t *)&_printf_buf;
  76. //for (int i=0; i<32; i++) {
  77. // printf("%x ", strMem[i]);
  78. //}
  79. mailbox_regs->regs.period1 = 0x1;
  80. printf("Done writing\n");
  81. //uint8_t * strMem = (uint8_t *)&_printf_buf;
  82. //for (int i=0; i<32; i++) {
  83. // printf("%x ", strMem[i]);
  84. //}
  85. printf("\n");
  86. }
  87. static void write_period1_2()
  88. {
  89. printf("Writing period1 0x2\n");
  90. //uint8_t *strMem = (uint8_t *)&_printf_buf;
  91. //for (int i=0; i<32; i++) {
  92. // printf("%x ", strMem[i]);
  93. //}
  94. mailbox_regs->regs.period1 = 0x2;
  95. printf("Done writing\n");
  96. //uint8_t * strMem = (uint8_t *)&_printf_buf;
  97. //for (int i=0; i<32; i++) {
  98. // printf("%x ", strMem[i]);
  99. //}
  100. }
  101. static void write_period1_4()
  102. {
  103. printf("Writing period1 0x4\n");
  104. mailbox_regs->regs.period1 = 0x4;
  105. printf("Done writing\n");
  106. printf("Reading now \n");
  107. printf("value: %d\n", mailbox_regs->regs.period1);
  108. }
  109. static void clear_period1()
  110. {
  111. printf("Clearing period1 val: %d\n", mailbox_regs->regs.period1);
  112. mailbox_regs->regs.period1 = 0x0;
  113. printf("Done clearing\n");
  114. }
  115. static void write_delay1()
  116. {
  117. printf("Writing delay1 0x1234\n");
  118. mailbox_regs->regs.delay1 = 0x1234;
  119. printf("Done writing\n");
  120. }
  121. static void clear_delay1()
  122. {
  123. printf("Clearing delay1 val: %d\n", mailbox_regs->regs.delay1);
  124. mailbox_regs->regs.delay1 = 0x0;
  125. printf("Done clearing\n");
  126. }
  127. static void
  128. boot_dfu(void)
  129. {
  130. /* Boot firmware */
  131. volatile uint32_t *boot = (void*)0x80000000;
  132. *boot = (1 << 2) | (1 << 0);
  133. }
  134. void
  135. usb_dfu_rt_cb_reboot(void)
  136. {
  137. boot_dfu();
  138. }
  139. void main()
  140. {
  141. int cmd = 0;
  142. /* Init console IO */
  143. console_init();
  144. puts("Booting App image..\n");
  145. /* LED */
  146. led_init();
  147. led_color(48, 96, 5);
  148. led_blink(true, 200, 1000);
  149. led_breathe(true, 100, 200);
  150. led_state(true);
  151. /* SPI */
  152. spi_init();
  153. /* Enable USB directly */
  154. serial_no_init();
  155. /* Main loop */
  156. while (1)
  157. {
  158. /* Prompt ? */
  159. if (cmd >= 0)
  160. printf("Command> ");
  161. /* Poll for command */
  162. cmd = getchar_nowait();
  163. if (cmd >= 0) {
  164. if (cmd > 32 && cmd < 127) {
  165. putchar(cmd);
  166. putchar('\r');
  167. putchar('\n');
  168. }
  169. switch (cmd)
  170. {
  171. case 'b':
  172. boot_dfu();
  173. break;
  174. case 'w':
  175. write_period1();
  176. break;
  177. case 'k':
  178. clear_period1();
  179. break;
  180. case 'a':
  181. write_period1_2();
  182. break;
  183. case 's':
  184. write_period1_4();
  185. break;
  186. case 'o':
  187. write_delay1();
  188. break;
  189. case 'm':
  190. clear_delay1();
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. }
  197. }