From e53a1a32089e2f0d1983cc92d1d4a6d28c4db07f Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 25 Apr 2010 09:31:04 +0000 Subject: Added command shell to the STM32 FatFs demo. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1890 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARMCM3-STM32F103-FATFS-GCC/Makefile | 2 +- demos/ARMCM3-STM32F103-FATFS-GCC/main.c | 205 ++++++++++++++++++++-------- demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt | 8 +- 3 files changed, 157 insertions(+), 58 deletions(-) (limited to 'demos/ARMCM3-STM32F103-FATFS-GCC') diff --git a/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile b/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile index 550e60ec8..1094cf3ca 100644 --- a/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile +++ b/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile @@ -74,7 +74,7 @@ CSRC = $(PORTSRC) \ $(PLATFORMSRC) \ $(BOARDSRC) \ $(FATFSSRC) \ - $(CHIBIOS)/os/various/evtimer.c \ + $(CHIBIOS)/os/various/shell.c \ $(CHIBIOS)/os/various/syscalls.c \ main.c diff --git a/demos/ARMCM3-STM32F103-FATFS-GCC/main.c b/demos/ARMCM3-STM32F103-FATFS-GCC/main.c index 8295e509b..ddd85c8fc 100644 --- a/demos/ARMCM3-STM32F103-FATFS-GCC/main.c +++ b/demos/ARMCM3-STM32F103-FATFS-GCC/main.c @@ -23,10 +23,15 @@ #include "ch.h" #include "hal.h" #include "test.h" +#include "shell.h" #include "evtimer.h" #include "ff.h" +/*===========================================================================*/ +/* MMC/SPI related. */ +/*===========================================================================*/ + /** * @brief FS object. */ @@ -58,22 +63,6 @@ static bool_t mmc_is_protected(void) {return !palReadPad(IOPORT3, GPIOC_MMCWP);} /* Generic large buffer.*/ uint8_t fbuff[1024]; -/* - * Red LEDs blinker thread, times are in milliseconds. - */ -static WORKING_AREA(waThread1, 128); -static msg_t Thread1(void *arg) { - - (void)arg; - while (TRUE) { - palClearPad(IOPORT3, GPIOC_LED); - chThdSleepMilliseconds(500); - palSetPad(IOPORT3, GPIOC_LED); - chThdSleepMilliseconds(500); - } - return 0; -} - static FRESULT scan_files(char *path) { FRESULT res; @@ -107,32 +96,140 @@ static FRESULT scan_files(char *path) return res; } +/*===========================================================================*/ +/* Command line related. */ +/*===========================================================================*/ + +#define SHELL_WA_SIZE THD_WA_SIZE(1024) +#define TEST_WA_SIZE THD_WA_SIZE(256) + +static void cmd_mem(BaseChannel *chp, int argc, char *argv[]) { + size_t n, size; + char buf[52]; + + (void)argv; + if (argc > 0) { + shellPrintLine(chp, "Usage: mem"); + return; + } + n = chHeapStatus(NULL, &size); + siprintf(buf, "core free memory : %lu bytes", chCoreFree()); + shellPrintLine(chp, buf); + siprintf(buf, "heap fragments : %lu", n); + shellPrintLine(chp, buf); + siprintf(buf, "heap free total : %lu bytes", size); + shellPrintLine(chp, buf); +} + +static void cmd_threads(BaseChannel *chp, int argc, char *argv[]) { + static const char *states[] = { + "READY", + "CURRENT", + "SUSPENDED", + "WTSEM", + "WTMTX", + "WTCOND", + "SLEEPING", + "WTEXIT", + "WTOREVT", + "WTANDEVT", + "SNDMSG", + "WTMSG", + "FINAL" + }; + Thread *tp; + char buf[60]; + + (void)argv; + if (argc > 0) { + shellPrintLine(chp, "Usage: threads"); + return; + } + shellPrintLine(chp, " addr stack prio refs state time"); + tp = chRegFirstThread(); + do { + siprintf(buf, "%8lx %8lx %4u %4i %9s %u", + (uint32_t)tp, (uint32_t)tp->p_ctx.r13, + (unsigned int)tp->p_prio, tp->p_refs - 1, + states[tp->p_state], (unsigned int)tp->p_time); + shellPrintLine(chp, buf); + tp = chRegNextThread(tp); + } while (tp != NULL); +} + +static void cmd_test(BaseChannel *chp, int argc, char *argv[]) { + Thread *tp; + + (void)argv; + if (argc > 0) { + shellPrintLine(chp, "Usage: test"); + return; + } + tp = chThdCreateFromHeap(NULL, TEST_WA_SIZE, chThdGetPriority(), + TestThread, chp); + if (tp == NULL) { + shellPrintLine(chp, "out of memory"); + return; + } + chThdWait(tp); +} + +static void cmd_tree(BaseChannel *chp, int argc, char *argv[]) { + FRESULT err; + uint32_t clusters; + FATFS *fsp; + + (void)argv; + if (argc > 0) { + shellPrintLine(chp, "Usage: tree"); + return; + } + if (!fs_ready) { + shellPrintLine(chp, "File System not mounted"); + return; + } + err = f_getfree("/", &clusters, &fsp); + if (err != FR_OK) { + shellPrintLine(chp, "FS: f_getfree() failed"); + return; + } + siprintf((void *)fbuff, + "FS: %lu free clusters, %lu sectors per cluster, %lu bytes free", + clusters, (uint32_t)MMC_FS.csize, + clusters * (uint32_t)MMC_FS.csize * (uint32_t)MMC_SECTOR_SIZE); + shellPrintLine(chp, (void *)fbuff); + fbuff[0] = 0; + scan_files((char *)fbuff); +} + +static const ShellCommand commands[] = { + {"mem", cmd_mem}, + {"threads", cmd_threads}, + {"test", cmd_test}, + {"tree", cmd_tree}, + {NULL, NULL} +}; + +static const ShellConfig shell_cfg1 = { + (BaseChannel *)&SD2, + commands +}; + /* - * Executed as event handler at 500mS intervals. + * Red LEDs blinker thread, times are in milliseconds. */ -static void TimerHandler(eventid_t id) { +static WORKING_AREA(waThread1, 128); +static msg_t Thread1(void *arg) { - (void)id; - if (palReadPad(IOPORT1, GPIOA_BUTTON)) { - if (fs_ready) { - FRESULT err; - uint32_t clusters; - FATFS *fsp; - - err = f_getfree("/", &clusters, &fsp); - if (err != FR_OK) { - iprintf("FS: f_getfree() failed\r\n"); - return; - } - iprintf("FS: %lu free clusters, %u sectors per cluster, %lu bytes free\r\n", - clusters, MMC_FS.csize, - clusters * (uint32_t)MMC_FS.csize * (uint32_t)MMC_SECTOR_SIZE); - fbuff[0] = 0; - scan_files((char *)fbuff); - } + (void)arg; + while (TRUE) { + palTogglePad(IOPORT3, GPIOC_LED); + if (fs_ready) + chThdSleepMilliseconds(200); else - TestThread(&SD2); + chThdSleepMilliseconds(500); } + return 0; } /* @@ -142,25 +239,18 @@ static void InsertHandler(eventid_t id) { FRESULT err; (void)id; - iprintf("MMC: inserted\r\n"); /* * On insertion MMC initialization and FS mount. */ - iprintf("MMC: initialization "); if (mmcConnect(&MMCD1)) { - iprintf("failed\r\n"); return; } - iprintf("ok\r\n"); - iprintf("FS: mount "); err = f_mount(0, &MMC_FS); if (err != FR_OK) { - iprintf("failed\r\n"); mmcDisconnect(&MMCD1); return; } fs_ready = TRUE; - iprintf("ok\r\n"); } /* @@ -169,7 +259,6 @@ static void InsertHandler(eventid_t id) { static void RemoveHandler(eventid_t id) { (void)id; - iprintf("MMC: removed\r\n"); fs_ready = FALSE; } @@ -179,12 +268,11 @@ static void RemoveHandler(eventid_t id) { */ int main(int argc, char **argv) { static const evhandler_t evhndl[] = { - TimerHandler, InsertHandler, RemoveHandler }; - static EvTimer evt; - struct EventListener el0, el1, el2; + Thread *shelltp = NULL; + struct EventListener el0, el1; (void)argc; (void)argv; @@ -194,6 +282,11 @@ int main(int argc, char **argv) { */ sdStart(&SD2, NULL); + /* + * Shell manager initialization. + */ + shellInit(); + /* * Initializes the MMC driver to work with SPI2. */ @@ -213,12 +306,16 @@ int main(int argc, char **argv) { * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and listen for events. */ - evtInit(&evt, MS2ST(500)); /* Initializes an event timer object. */ - evtStart(&evt); /* Starts the event timer. */ - chEvtRegister(&evt.et_es, &el0, 0); /* Registers on the timer event source. */ - chEvtRegister(&MMCD1.mmc_inserted_event, &el1, 1); - chEvtRegister(&MMCD1.mmc_removed_event, &el2, 2); - while (TRUE) + chEvtRegister(&MMCD1.mmc_inserted_event, &el0, 0); + chEvtRegister(&MMCD1.mmc_removed_event, &el1, 1); + while (TRUE) { + if (!shelltp) + shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); + else if (chThdTerminated(shelltp)) { + chThdRelease(shelltp); /* Recovers memory of the previous shell. */ + shelltp = NULL; /* Triggers spawning of a new shell. */ + } chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); + } return 0; } diff --git a/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt b/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt index 65f40a89a..619e89aa5 100644 --- a/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt +++ b/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt @@ -11,9 +11,11 @@ The demo will on an Olimex STM32-P103 board. This demo shows how to integrate the FatFs file system and use the SPI and MMC drivers. The demo flashes the board LED using a thread and monitors the MMC slot for -a card insertion. By pressing the button located on the board while a card is -inserted a directory dump on the serial port is performed, if a card is not -inserted then the test procedure is activated. +a card insertion. When a card is inserted then the file system is mounted +and the LED flashes faster. +A command line shell is spawned on SD2, all the interaction with the demo is +performed using the command shell, type "help" for a list of the available +commands. ** Build Procedure ** -- cgit v1.2.3