main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "ch.h"
  16. #include "hal.h"
  17. #include "rt_test_root.h"
  18. #include "chprintf.h"
  19. #include "shell.h"
  20. #include "lwipthread.h"
  21. #include "web/web.h"
  22. #include "ff.h"
  23. #include "portab.h"
  24. #include "usbcfg.h"
  25. /*===========================================================================*/
  26. /* Card insertion monitor. */
  27. /*===========================================================================*/
  28. #define POLLING_INTERVAL 10
  29. #define POLLING_DELAY 10
  30. /**
  31. * @brief Card monitor timer.
  32. */
  33. static virtual_timer_t tmr;
  34. /**
  35. * @brief Debounce counter.
  36. */
  37. static unsigned cnt;
  38. /**
  39. * @brief Card event sources.
  40. */
  41. static event_source_t inserted_event, removed_event;
  42. /**
  43. * @brief Insertion monitor timer callback function.
  44. *
  45. * @param[in] p pointer to the @p BaseBlockDevice object
  46. *
  47. * @notapi
  48. */
  49. static void tmrfunc(void *p) {
  50. BaseBlockDevice *bbdp = p;
  51. chSysLockFromISR();
  52. if (cnt > 0) {
  53. if (blkIsInserted(bbdp)) {
  54. if (--cnt == 0) {
  55. chEvtBroadcastI(&inserted_event);
  56. }
  57. }
  58. else
  59. cnt = POLLING_INTERVAL;
  60. }
  61. else {
  62. if (!blkIsInserted(bbdp)) {
  63. cnt = POLLING_INTERVAL;
  64. chEvtBroadcastI(&removed_event);
  65. }
  66. }
  67. chVTSetI(&tmr, TIME_MS2I(POLLING_DELAY), tmrfunc, bbdp);
  68. chSysUnlockFromISR();
  69. }
  70. /**
  71. * @brief Polling monitor start.
  72. *
  73. * @param[in] p pointer to an object implementing @p BaseBlockDevice
  74. *
  75. * @notapi
  76. */
  77. static void tmr_init(void *p) {
  78. chEvtObjectInit(&inserted_event);
  79. chEvtObjectInit(&removed_event);
  80. chSysLock();
  81. cnt = POLLING_INTERVAL;
  82. chVTSetI(&tmr, TIME_MS2I(POLLING_DELAY), tmrfunc, p);
  83. chSysUnlock();
  84. }
  85. /*===========================================================================*/
  86. /* FatFs related. */
  87. /*===========================================================================*/
  88. /**
  89. * @brief FS object.
  90. */
  91. static FATFS SDC_FS;
  92. /* FS mounted and ready.*/
  93. static bool fs_ready = FALSE;
  94. /* Generic large buffer.*/
  95. static uint8_t fbuff[1024];
  96. static FRESULT scan_files(BaseSequentialStream *chp, char *path) {
  97. static FILINFO fno;
  98. FRESULT res;
  99. DIR dir;
  100. size_t i;
  101. char *fn;
  102. res = f_opendir(&dir, path);
  103. if (res == FR_OK) {
  104. i = strlen(path);
  105. while (((res = f_readdir(&dir, &fno)) == FR_OK) && fno.fname[0]) {
  106. if (FF_FS_RPATH && fno.fname[0] == '.')
  107. continue;
  108. fn = fno.fname;
  109. if (fno.fattrib & AM_DIR) {
  110. *(path + i) = '/';
  111. strcpy(path + i + 1, fn);
  112. res = scan_files(chp, path);
  113. *(path + i) = '\0';
  114. if (res != FR_OK)
  115. break;
  116. }
  117. else {
  118. chprintf(chp, "%s/%s\r\n", path, fn);
  119. }
  120. }
  121. }
  122. return res;
  123. }
  124. /*===========================================================================*/
  125. /* Command line related. */
  126. /*===========================================================================*/
  127. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
  128. static void cmd_tree(BaseSequentialStream *chp, int argc, char *argv[]) {
  129. FRESULT err;
  130. uint32_t fre_clust;
  131. FATFS *fsp;
  132. (void)argv;
  133. if (argc > 0) {
  134. chprintf(chp, "Usage: tree\r\n");
  135. return;
  136. }
  137. if (!fs_ready) {
  138. chprintf(chp, "File System not mounted\r\n");
  139. return;
  140. }
  141. err = f_getfree("/", &fre_clust, &fsp);
  142. if (err != FR_OK) {
  143. chprintf(chp, "FS: f_getfree() failed\r\n");
  144. return;
  145. }
  146. chprintf(chp,
  147. "FS: %lu free clusters with %lu sectors (%lu bytes) per cluster\r\n",
  148. fre_clust, (uint32_t)fsp->csize, (uint32_t)fsp->csize * 512);
  149. fbuff[0] = 0;
  150. scan_files(chp, (char *)fbuff);
  151. }
  152. static const ShellCommand commands[] = {
  153. {"tree", cmd_tree},
  154. {NULL, NULL}
  155. };
  156. static const ShellConfig shell_cfg1 = {
  157. (BaseSequentialStream *)&PORTAB_SDU1,
  158. commands
  159. };
  160. /*===========================================================================*/
  161. /* Main and generic code. */
  162. /*===========================================================================*/
  163. static thread_t *shelltp = NULL;
  164. /*
  165. * Card insertion event.
  166. */
  167. static void InsertHandler(eventid_t id) {
  168. FRESULT err;
  169. (void)id;
  170. /*
  171. * On insertion SDC initialization and FS mount.
  172. */
  173. if (sdcConnect(&SDCD1))
  174. return;
  175. err = f_mount(&SDC_FS, "/", 1);
  176. if (err != FR_OK) {
  177. sdcDisconnect(&SDCD1);
  178. return;
  179. }
  180. fs_ready = TRUE;
  181. }
  182. /*
  183. * Card removal event.
  184. */
  185. static void RemoveHandler(eventid_t id) {
  186. (void)id;
  187. sdcDisconnect(&SDCD1);
  188. fs_ready = FALSE;
  189. }
  190. /*
  191. * Shell exit event.
  192. */
  193. static void ShellHandler(eventid_t id) {
  194. (void)id;
  195. if (chThdTerminatedX(shelltp)) {
  196. chThdRelease(shelltp);
  197. shelltp = NULL;
  198. }
  199. }
  200. /*
  201. * Green LED blinker thread, times are in milliseconds.
  202. */
  203. static THD_WORKING_AREA(waThread1, 128);
  204. static THD_FUNCTION(Thread1, arg) {
  205. (void)arg;
  206. chRegSetThreadName("blinker");
  207. while (true) {
  208. palToggleLine(PORTAB_BLINK_LED1);
  209. chThdSleepMilliseconds(fs_ready ? 250 : 500);
  210. }
  211. }
  212. /*
  213. * Application entry point.
  214. */
  215. int main(void) {
  216. static const evhandler_t evhndl[] = {
  217. InsertHandler,
  218. RemoveHandler,
  219. ShellHandler
  220. };
  221. event_listener_t el0, el1, el2;
  222. /*
  223. * System initializations.
  224. * - HAL initialization, this also initializes the configured device drivers
  225. * and performs the board-specific initializations.
  226. * - Kernel initialization, the main() function becomes a thread and the
  227. * RTOS is active.
  228. * - lwIP subsystem initialization using the default configuration.
  229. */
  230. halInit();
  231. chSysInit();
  232. /*
  233. * Initialize RNG
  234. */
  235. rccEnableAHB2(RCC_AHB2ENR_RNGEN, 0);
  236. RNG->CR |= RNG_CR_IE;
  237. RNG->CR |= RNG_CR_RNGEN;
  238. /* lwip */
  239. lwipInit(NULL);
  240. /*
  241. * Target-dependent setup code.
  242. */
  243. portab_setup();
  244. /*
  245. * Initializes a serial-over-USB CDC driver.
  246. */
  247. sduObjectInit(&PORTAB_SDU1);
  248. sduStart(&PORTAB_SDU1, &serusbcfg);
  249. /*
  250. * Activates the USB driver and then the USB bus pull-up on D+.
  251. * Note, a delay is inserted in order to not have to disconnect the cable
  252. * after a reset.
  253. */
  254. usbDisconnectBus(serusbcfg.usbp);
  255. chThdSleepMilliseconds(1500);
  256. usbStart(serusbcfg.usbp, &usbcfg);
  257. usbConnectBus(serusbcfg.usbp);
  258. /*
  259. * Shell manager initialization.
  260. */
  261. shellInit();
  262. /*
  263. * Activates the SDC driver 1 using default configuration.
  264. */
  265. sdcStart(&SDCD1, NULL);
  266. /*
  267. * Activates the card insertion monitor.
  268. */
  269. tmr_init(&SDCD1);
  270. /*
  271. * Creates the blinker thread.
  272. */
  273. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  274. /*
  275. * Creates the HTTPS thread (it changes priority internally).
  276. */
  277. chThdCreateStatic(wa_https_server, sizeof(wa_https_server), NORMALPRIO + 1,
  278. https_server, NULL);
  279. /*
  280. * Normal main() thread activity, handling SD card events and shell
  281. * start/exit.
  282. */
  283. chEvtRegister(&inserted_event, &el0, 0);
  284. chEvtRegister(&removed_event, &el1, 1);
  285. chEvtRegister(&shell_terminated, &el2, 2);
  286. while (true) {
  287. if (!shelltp && (PORTAB_SDU1.config->usbp->state == USB_ACTIVE)) {
  288. shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  289. "shell", NORMALPRIO + 1,
  290. shellThread, (void *)&shell_cfg1);
  291. }
  292. chEvtDispatch(evhndl, chEvtWaitOneTimeout(ALL_EVENTS, TIME_MS2I(500)));
  293. }
  294. }