From 84cf9ce9ffa3d538b367f74c8206b8ef894d466f Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 15 Nov 2009 08:52:17 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1299 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARMCM3-STM32F103-FATFS-GCC/Makefile | 3 +- demos/ARMCM3-STM32F103-FATFS-GCC/main.c | 134 +++++++++++++------- demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt | 9 +- ext/ff007e-patched.zip | Bin 634232 -> 634232 bytes os/various/syscalls.c | 27 +++- readme.txt | 3 + 6 files changed, 125 insertions(+), 51 deletions(-) diff --git a/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile b/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile index afb044c49..b84adf3a5 100644 --- a/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile +++ b/demos/ARMCM3-STM32F103-FATFS-GCC/Makefile @@ -77,6 +77,7 @@ CSRC = ${PORTSRC} \ ${CHIBIOS}/os/io/platforms/STM32/spi_lld.c \ ${CHIBIOS}/os/io/platforms/STM32/stm32_dma.c \ ${CHIBIOS}/os/various/evtimer.c \ + ${CHIBIOS}/os/various/syscalls.c \ board.c main.c # C++ sources that can be compiled in ARM or THUMB mode depending on the global @@ -158,7 +159,7 @@ CPPWARN = -Wall -Wextra # # List all default C defines here, like -D_DEBUG=1 -DDEFS = +DDEFS = -DSTDOUT_SD=SD2 -DSTDIN_SD=SD2 # List all default ASM defines here, like -D_DEBUG=1 DADEFS = diff --git a/demos/ARMCM3-STM32F103-FATFS-GCC/main.c b/demos/ARMCM3-STM32F103-FATFS-GCC/main.c index 5fd6737a6..2ddf02b3f 100644 --- a/demos/ARMCM3-STM32F103-FATFS-GCC/main.c +++ b/demos/ARMCM3-STM32F103-FATFS-GCC/main.c @@ -27,52 +27,41 @@ #include +#include +#include + #include "board.h" -/* - * Maximum speed SPI configuration (18MHz, CPHA=0, CPOL=0, MSb first). +/** + * @brief FS object. */ -static SPIConfig hs_spicfg = { - IOPORT2, GPIOB_SPI2NSS, 0 -}; +FATFS MMC_FS; -/* - * Low speed SPI configuration (281.250KHz, CPHA=0, CPOL=0, MSb first). - */ -static SPIConfig ls_spicfg = { - IOPORT2, GPIOB_SPI2NSS, SPI_CR1_BR_2 | SPI_CR1_BR_1 -}; - -/* - * MMC driver instance. +/** + * MMC driver instance. */ MMCDriver MMCD1; -/* - * MMC configuration (empty). - */ +/* FS mounted and ready.*/ +static bool_t fs_ready = FALSE; + +/* Maximum speed SPI configuration (18MHz, CPHA=0, CPOL=0, MSb first).*/ +static SPIConfig hs_spicfg = {IOPORT2, GPIOB_SPI2NSS, 0}; + +/* Low speed SPI configuration (281.250KHz, CPHA=0, CPOL=0, MSb first).*/ +static SPIConfig ls_spicfg = {IOPORT2, GPIOB_SPI2NSS, SPI_CR1_BR_2 | SPI_CR1_BR_1}; + +/* MMC configuration (empty).*/ static const MMCConfig mmc_cfg = {}; -/* - * Card insertion verification. - */ -static bool_t mmc_is_inserted(void) { +/* Card insertion verification.*/ +static bool_t mmc_is_inserted(void) {return palReadPad(IOPORT3, GPIOC_MMCCP);} - return palReadPad(IOPORT3, GPIOC_MMCCP); -} +/* Card protection verification.*/ +static bool_t mmc_is_protected(void) {return !palReadPad(IOPORT3, GPIOC_MMCWP);} -/* - * Card protection verification. - */ -static bool_t mmc_is_protected(void) { - - return !palReadPad(IOPORT3, GPIOC_MMCWP); -} - -/** - * @brief FS object. - */ -FATFS MMC_FS; +/* Generic large buffer.*/ +uint8_t fbuff[1024]; /* * Red LEDs blinker thread, times are in milliseconds. @@ -90,14 +79,65 @@ static msg_t Thread1(void *arg) { return 0; } +static FRESULT scan_files(char *path) +{ + FRESULT res; + FILINFO fno; + DIR dir; + int i; + char *fn; + + res = f_opendir(&dir, path); + if (res == FR_OK) { + i = strlen(path); + for (;;) { + res = f_readdir(&dir, &fno); + if (res != FR_OK || fno.fname[0] == 0) + break; + if (fno.fname[0] == '.') + continue; + fn = fno.fname; + if (fno.fattrib & AM_DIR) { + siprintf(&path[i], "/%s", fn); + res = scan_files(path); + if (res != FR_OK) + break; + path[i] = 0; + } + else { + iprintf("%s/%s\r\n", path, fn); + } + } + } + return res; +} + /* * Executed as event handler at 500mS intervals. */ static void TimerHandler(eventid_t id) { (void)id; - if (palReadPad(IOPORT1, GPIOA_BUTTON)) - TestThread(&SD2); + 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); + } + else + TestThread(&SD2); + } } /* @@ -105,25 +145,27 @@ static void TimerHandler(eventid_t id) { */ static void InsertHandler(eventid_t id) { FRESULT err; - uint32_t clusters; - FATFS *fsp; (void)id; + iprintf("MMC: inserted\r\n"); /* * On insertion MMC initialization and FS mount. */ - if (mmcConnect(&MMCD1)) + 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; } - err = f_getfree("/", &clusters, &fsp); - if (err != FR_OK) { - mmcDisconnect(&MMCD1); - return; - } + fs_ready = TRUE; + iprintf("ok\r\n"); } /* @@ -132,6 +174,8 @@ static void InsertHandler(eventid_t id) { static void RemoveHandler(eventid_t id) { (void)id; + iprintf("MMC: removed\r\n"); + fs_ready = FALSE; } /* diff --git a/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt b/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt index 09f544035..65f40a89a 100644 --- a/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt +++ b/demos/ARMCM3-STM32F103-FATFS-GCC/readme.txt @@ -8,9 +8,12 @@ The demo will on an Olimex STM32-P103 board. ** The Demo ** -The demo flashes the board LED using a thread, by pressing the button located -on the board the test procedure is activated with output on the serial port -COM2 (USART2). +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. ** Build Procedure ** diff --git a/ext/ff007e-patched.zip b/ext/ff007e-patched.zip index 7b7cd3160aa62fa9be5fa095b00b412bc9171fa9..0e10e030825af53638c56955d91edd2fe0af5858 100644 GIT binary patch delta 80 zcmexyOYO%kwT2ePElj)`%xQl46Ai_;t7|ZQ=mc@vr8Y4EF*6Xe05K~NvjH*tcBxGq WmES=swohBh`H2zC>R!$HmJtBj1|G2h delta 79 zcmexyOYO%kwT2ePElj)`OcR$(G?3V?uEF%7lX>EjymqNgOhC*G#4JF}3dC$c%)VV} Y6G!EDkc#coR&st~1hcwVbG~H+0RLJbLI3~& diff --git a/os/various/syscalls.c b/os/various/syscalls.c index 258d9c9d6..e635b1388 100644 --- a/os/various/syscalls.c +++ b/os/various/syscalls.c @@ -63,18 +63,31 @@ #include #include +#if defined(STDOUT_SD) || defined(STDIN_SD) +#include +#endif /***************************************************************************/ int _read_r(struct _reent *r, int file, char * ptr, int len) { (void)r; +#if defined(STDIN_SD) + if (!len || (file != 0)) { + errno = EINVAL; + return -1; + } + *ptr++ = chIOGet(&STDOUT_SD); + if (--len > 0) + len = chIORead(&STDOUT_SD, (uint8_t *)ptr, (size_t)len); + return len; +#else (void)file; (void)ptr; (void)len; - errno = EINVAL; return -1; +#endif } /***************************************************************************/ @@ -93,10 +106,20 @@ int _lseek_r(struct _reent *r, int file, int ptr, int dir) int _write_r(struct _reent *r, int file, char * ptr, int len) { + int n; + (void)r; (void)file; (void)ptr; - +#if defined(STDOUT_SD) + if (file != 1) { + errno = EINVAL; + return -1; + } + n = len; + while (n--) + chIOPut(&STDOUT_SD, *ptr++); +#endif return len; } diff --git a/readme.txt b/readme.txt index 3ab69d454..3f60fb448 100644 --- a/readme.txt +++ b/readme.txt @@ -15,6 +15,9 @@ - NEW: Generic MMC (over SPI) driver. - NEW: Added a STM32 demo that integrates the MMC driver and the FatFs file system. +- NEW: Implemented I/O redirection on a serial driver into syscalls.c, now + it is possible (but not recommended) to use printf()/scanf() etc. An usage + example is in the new MMC/FatFs demo. Note the extra -D... into the Makefile. - CHANGE: Moved the STM32 firmware library under ./ext, this way there is no need to duplicate it in each demo program. - CHANGE: Moved the file stm32f10x.h from the demos to the platforms support