main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "lwip/apps/httpd.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 HAL_USE_SDC
  174. if (sdcConnect(&SDCD1))
  175. #else
  176. if (mmcConnect(&MMCD1))
  177. #endif
  178. return;
  179. err = f_mount(&SDC_FS, "/", 1);
  180. if (err != FR_OK) {
  181. #if HAL_USE_SDC
  182. sdcDisconnect(&SDCD1);
  183. #else
  184. mmcDisconnect(&MMCD1);
  185. #endif
  186. return;
  187. }
  188. fs_ready = TRUE;
  189. }
  190. /*
  191. * Card removal event.
  192. */
  193. static void RemoveHandler(eventid_t id) {
  194. (void)id;
  195. #if HAL_USE_SDC
  196. sdcDisconnect(&SDCD1);
  197. #else
  198. mmcDisconnect(&MMCD1);
  199. #endif
  200. fs_ready = FALSE;
  201. }
  202. /*
  203. * Shell exit event.
  204. */
  205. static void ShellHandler(eventid_t id) {
  206. (void)id;
  207. if (chThdTerminatedX(shelltp)) {
  208. chThdRelease(shelltp);
  209. shelltp = NULL;
  210. }
  211. }
  212. /*
  213. * Green LED blinker thread, times are in milliseconds.
  214. */
  215. static THD_WORKING_AREA(waThread1, 128);
  216. static THD_FUNCTION(Thread1, arg) {
  217. (void)arg;
  218. chRegSetThreadName("blinker");
  219. while (true) {
  220. palToggleLine(PORTAB_BLINK_LED1);
  221. chThdSleepMilliseconds(fs_ready ? 250 : 500);
  222. }
  223. }
  224. /*
  225. * Application entry point.
  226. */
  227. int main(void) {
  228. static const evhandler_t evhndl[] = {
  229. InsertHandler,
  230. RemoveHandler,
  231. ShellHandler
  232. };
  233. event_listener_t el0, el1, el2;
  234. /*
  235. * System initializations.
  236. * - HAL initialization, this also initializes the configured device drivers
  237. * and performs the board-specific initializations.
  238. * - Kernel initialization, the main() function becomes a thread and the
  239. * RTOS is active.
  240. * - lwIP subsystem initialization using the default configuration.
  241. */
  242. halInit();
  243. chSysInit();
  244. lwipInit(NULL);
  245. /*
  246. * Target-dependent setup code.
  247. */
  248. portab_setup();
  249. /*
  250. * Initializes a serial-over-USB CDC driver.
  251. */
  252. sduObjectInit(&PORTAB_SDU1);
  253. sduStart(&PORTAB_SDU1, &serusbcfg);
  254. /*
  255. * Activates the USB driver and then the USB bus pull-up on D+.
  256. * Note, a delay is inserted in order to not have to disconnect the cable
  257. * after a reset.
  258. */
  259. usbDisconnectBus(serusbcfg.usbp);
  260. chThdSleepMilliseconds(1500);
  261. usbStart(serusbcfg.usbp, &usbcfg);
  262. usbConnectBus(serusbcfg.usbp);
  263. /*
  264. * Shell manager initialization.
  265. */
  266. shellInit();
  267. #if HAL_USE_SDC
  268. /*
  269. * Activates the SDC driver 1 using default configuration.
  270. */
  271. sdcStart(&SDCD1, NULL);
  272. /*
  273. * Activates the card insertion monitor.
  274. */
  275. tmr_init(&SDCD1);
  276. #else
  277. /*
  278. * Initializes the MMC driver to work with SPI3.
  279. */
  280. palSetPad(IOPORT3, GPIOC_SPI3_SD_CS);
  281. mmcObjectInit(&MMCD1);
  282. mmcStart(&MMCD1, &portab_mmccfg);
  283. /*
  284. * Activates the card insertion monitor.
  285. */
  286. tmr_init(&MMCD1);
  287. #endif
  288. /*
  289. * Creates the blinker thread.
  290. */
  291. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  292. /*
  293. * Starts the HTTP server.
  294. */
  295. httpd_init();
  296. /*
  297. * Normal main() thread activity, handling SD card events and shell
  298. * start/exit.
  299. */
  300. chEvtRegister(&inserted_event, &el0, 0);
  301. chEvtRegister(&removed_event, &el1, 1);
  302. chEvtRegister(&shell_terminated, &el2, 2);
  303. while (true) {
  304. if (!shelltp && (PORTAB_SDU1.config->usbp->state == USB_ACTIVE)) {
  305. shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  306. "shell", NORMALPRIO + 1,
  307. shellThread, (void *)&shell_cfg1);
  308. }
  309. chEvtDispatch(evhndl, chEvtWaitOneTimeout(ALL_EVENTS, TIME_MS2I(500)));
  310. }
  311. }