main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <string.h>
  14. #include "ch.h"
  15. #include "hal.h"
  16. #include "chprintf.h"
  17. #include "shell.h"
  18. #include "ff.h"
  19. /*===========================================================================*/
  20. /* Card insertion monitor. */
  21. /*===========================================================================*/
  22. #define POLLING_INTERVAL 10
  23. #define POLLING_DELAY 10
  24. /**
  25. * @brief Card monitor timer.
  26. */
  27. static virtual_timer_t tmr;
  28. /**
  29. * @brief Debounce counter.
  30. */
  31. static unsigned cnt;
  32. /**
  33. * @brief Card event sources.
  34. */
  35. static event_source_t inserted_event, removed_event;
  36. /**
  37. * @brief Insertion monitor timer callback function.
  38. *
  39. * @param[in] p pointer to the @p BaseBlockDevice object
  40. *
  41. * @notapi
  42. */
  43. static void tmrfunc(void *p) {
  44. BaseBlockDevice *bbdp = p;
  45. chSysLockFromISR();
  46. if (cnt > 0) {
  47. if (blkIsInserted(bbdp)) {
  48. if (--cnt == 0) {
  49. chEvtBroadcastI(&inserted_event);
  50. }
  51. }
  52. else
  53. cnt = POLLING_INTERVAL;
  54. }
  55. else {
  56. if (!blkIsInserted(bbdp)) {
  57. cnt = POLLING_INTERVAL;
  58. chEvtBroadcastI(&removed_event);
  59. }
  60. }
  61. chVTSetI(&tmr, TIME_MS2I(POLLING_DELAY), tmrfunc, bbdp);
  62. chSysUnlockFromISR();
  63. }
  64. /**
  65. * @brief Polling monitor start.
  66. *
  67. * @param[in] p pointer to an object implementing @p BaseBlockDevice
  68. *
  69. * @notapi
  70. */
  71. static void tmr_init(void *p) {
  72. chEvtObjectInit(&inserted_event);
  73. chEvtObjectInit(&removed_event);
  74. chSysLock();
  75. cnt = POLLING_INTERVAL;
  76. chVTSetI(&tmr, TIME_MS2I(POLLING_DELAY), tmrfunc, p);
  77. chSysUnlock();
  78. }
  79. /*===========================================================================*/
  80. /* FatFs related. */
  81. /*===========================================================================*/
  82. /**
  83. * @brief FS object.
  84. */
  85. static FATFS SDC_FS;
  86. /* FS mounted and ready.*/
  87. static bool fs_ready = FALSE;
  88. /* Maximum speed SPI configuration (18MHz, CPHA=0, CPOL=0, MSb first).*/
  89. static SPIConfig hs_spicfg = {false, NULL, IOPORT2, GPIOB_SPI2NSS, 0, 0};
  90. /* Low speed SPI configuration (281.250kHz, CPHA=0, CPOL=0, MSb first).*/
  91. static SPIConfig ls_spicfg = {false, NULL, IOPORT2, GPIOB_SPI2NSS,
  92. SPI_CR1_BR_2 | SPI_CR1_BR_1,
  93. 0};
  94. /* MMC/SD over SPI driver configuration.*/
  95. static MMCConfig mmccfg = {&SPID2, &ls_spicfg, &hs_spicfg};
  96. /* Generic large buffer.*/
  97. static uint8_t fbuff[1024];
  98. static FRESULT scan_files(BaseSequentialStream *chp, char *path) {
  99. static FILINFO fno;
  100. FRESULT res;
  101. DIR dir;
  102. size_t i;
  103. char *fn;
  104. res = f_opendir(&dir, path);
  105. if (res == FR_OK) {
  106. i = strlen(path);
  107. while (((res = f_readdir(&dir, &fno)) == FR_OK) && fno.fname[0]) {
  108. if (FF_FS_RPATH && fno.fname[0] == '.')
  109. continue;
  110. fn = fno.fname;
  111. if (fno.fattrib & AM_DIR) {
  112. *(path + i) = '/';
  113. strcpy(path + i + 1, fn);
  114. res = scan_files(chp, path);
  115. *(path + i) = '\0';
  116. if (res != FR_OK)
  117. break;
  118. }
  119. else {
  120. chprintf(chp, "%s/%s\r\n", path, fn);
  121. }
  122. }
  123. }
  124. return res;
  125. }
  126. /*===========================================================================*/
  127. /* Command line related. */
  128. /*===========================================================================*/
  129. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
  130. static void cmd_tree(BaseSequentialStream *chp, int argc, char *argv[]) {
  131. FRESULT err;
  132. uint32_t clusters;
  133. FATFS *fsp;
  134. (void)argv;
  135. if (argc > 0) {
  136. chprintf(chp, "Usage: tree\r\n");
  137. return;
  138. }
  139. if (!fs_ready) {
  140. chprintf(chp, "File System not mounted\r\n");
  141. return;
  142. }
  143. err = f_getfree("/", &clusters, &fsp);
  144. if (err != FR_OK) {
  145. chprintf(chp, "FS: f_getfree() failed\r\n");
  146. return;
  147. }
  148. chprintf(chp,
  149. "FS: %lu free clusters, %lu sectors per cluster, %lu bytes free\r\n",
  150. clusters, (uint32_t)SDC_FS.csize,
  151. clusters * (uint32_t)SDC_FS.csize * (uint32_t)MMCSD_BLOCK_SIZE);
  152. fbuff[0] = 0;
  153. scan_files(chp, (char *)fbuff);
  154. }
  155. static const ShellCommand commands[] = {
  156. {"tree", cmd_tree},
  157. {NULL, NULL}
  158. };
  159. static const ShellConfig shell_cfg1 = {
  160. (BaseSequentialStream *)&SD2,
  161. commands
  162. };
  163. /*===========================================================================*/
  164. /* Main and generic code. */
  165. /*===========================================================================*/
  166. static thread_t *shelltp = NULL;
  167. MMCDriver MMCD1;
  168. /*
  169. * Card insertion event.
  170. */
  171. static void InsertHandler(eventid_t id) {
  172. FRESULT err;
  173. (void)id;
  174. /*
  175. * On insertion SDC initialization and FS mount.
  176. */
  177. if (mmcConnect(&MMCD1))
  178. return;
  179. err = f_mount(&SDC_FS, "/", 1);
  180. if (err != FR_OK) {
  181. mmcDisconnect(&MMCD1);
  182. return;
  183. }
  184. fs_ready = TRUE;
  185. }
  186. /*
  187. * Card removal event.
  188. */
  189. static void RemoveHandler(eventid_t id) {
  190. (void)id;
  191. mmcDisconnect(&MMCD1);
  192. fs_ready = FALSE;
  193. }
  194. /*
  195. * Shell exit event.
  196. */
  197. static void ShellHandler(eventid_t id) {
  198. (void)id;
  199. if (chThdTerminatedX(shelltp)) {
  200. chThdWait(shelltp); /* Returning memory to heap. */
  201. shelltp = NULL;
  202. }
  203. }
  204. /*
  205. * Blinker thread.
  206. */
  207. static THD_WORKING_AREA(waThread1, 128);
  208. static THD_FUNCTION(Thread1, arg) {
  209. (void)arg;
  210. chRegSetThreadName("blinker");
  211. while (TRUE) {
  212. palTogglePad(IOPORT3, GPIOC_LED);
  213. if (fs_ready)
  214. chThdSleepMilliseconds(200);
  215. else
  216. chThdSleepMilliseconds(500);
  217. }
  218. }
  219. /*
  220. * Application entry point.
  221. */
  222. int main(void) {
  223. static const evhandler_t evhndl[] = {
  224. InsertHandler,
  225. RemoveHandler,
  226. ShellHandler
  227. };
  228. event_listener_t el0, el1, el2;
  229. /*
  230. * System initializations.
  231. * - HAL initialization, this also initializes the configured device drivers
  232. * and performs the board-specific initializations.
  233. * - Kernel initialization, the main() function becomes a thread and the
  234. * RTOS is active.
  235. */
  236. halInit();
  237. chSysInit();
  238. /*
  239. * Activates the serial driver 1 using the driver default configuration.
  240. * PA9(TX) and PA10(RX) are routed to USART1.
  241. */
  242. sdStart(&SD2, NULL);
  243. /*
  244. * Shell manager initialization.
  245. */
  246. shellInit();
  247. /*
  248. * Initializes the MMC driver to work with SPI2.
  249. */
  250. palSetPadMode(IOPORT2, GPIOB_SPI2NSS, PAL_MODE_OUTPUT_PUSHPULL);
  251. palSetPad(IOPORT2, GPIOB_SPI2NSS);
  252. mmcObjectInit(&MMCD1);
  253. mmcStart(&MMCD1, &mmccfg);
  254. /*
  255. * Activates the card insertion monitor.
  256. */
  257. tmr_init(&MMCD1);
  258. /*
  259. * Creates the blinker thread.
  260. */
  261. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+1, Thread1, NULL);
  262. /*
  263. * Normal main() thread activity, handling SD card events and shell
  264. * start/exit.
  265. */
  266. chEvtRegister(&inserted_event, &el0, 0);
  267. chEvtRegister(&removed_event, &el1, 1);
  268. chEvtRegister(&shell_terminated, &el2, 2);
  269. while (true) {
  270. if (!shelltp) {
  271. shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  272. "shell", NORMALPRIO + 1,
  273. shellThread, (void *)&shell_cfg1);
  274. }
  275. chEvtDispatch(evhndl, chEvtWaitOneTimeout(ALL_EVENTS, TIME_MS2I(500)));
  276. }
  277. }