From d5cfd83d179d484cb8497c736adb7daf7027efab Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 28 Jun 2012 13:54:19 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4355 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- boards/OLIMEX_STM32_E407/board.c | 29 +++++ .../ARMCM4-STM32F407-LWIP-FATFS-USB/halconf.h | 2 +- demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c | 113 +++++++++++++++++- 3 files changed, 141 insertions(+), 3 deletions(-) diff --git a/boards/OLIMEX_STM32_E407/board.c b/boards/OLIMEX_STM32_E407/board.c index 59b6e9de6..6c149bbca 100644 --- a/boards/OLIMEX_STM32_E407/board.c +++ b/boards/OLIMEX_STM32_E407/board.c @@ -51,6 +51,35 @@ void __early_init(void) { stm32_clock_init(); } +#if HAL_USE_SDC +/* + * Card detection through the card internal pull-up on D3. + */ +bool_t sdc_lld_is_card_inserted(SDCDriver *sdcp) { + + (void)sdcp; + palSetPadMode(GPIOC, GPIOC_SD_D3, PAL_MODE_INPUT); + if (palReadPad(GPIOC, GPIOC_SD_D3) != PAL_LOW) { + /* Switching the pin to SDIO mode because after detecting the card the + SDC driver will start accessing it.*/ + palSetPadMode(GPIOC, GPIOC_SD_D3, PAL_MODE_ALTERNATE(12)); + return TRUE; + } + /* Leaving the pin in input mode, it will be polled again.*/ + return FALSE; +} + +/* + * Card write protection detection is not possible, the card is always + * reported as not protected. + */ +bool_t sdc_lld_is_write_protected(SDCDriver *sdcp) { + + (void)sdcp; + return FALSE; +} +#endif /* HAL_USE_SDC */ + /* * Board-specific initialization code. */ diff --git a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/halconf.h b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/halconf.h index 507e35235..5dc4e0452 100644 --- a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/halconf.h +++ b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/halconf.h @@ -122,7 +122,7 @@ * @brief Enables the SDC subsystem. */ #if !defined(HAL_USE_SDC) || defined(__DOXYGEN__) -#define HAL_USE_SDC FALSE +#define HAL_USE_SDC TRUE #endif /** diff --git a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c index ed7833d2c..98d9335eb 100644 --- a/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c +++ b/demos/ARMCM4-STM32F407-LWIP-FATFS-USB/main.c @@ -27,6 +27,103 @@ #include "usbshell.h" #include "web/web.h" +/*===========================================================================*/ +/* Card insertion monitor. */ +/*===========================================================================*/ + +#define POLLING_INTERVAL 10 +#define POLLING_DELAY 10 + +/** + * @brief Card monitor timer. + */ +static VirtualTimer tmr; + +/** + * @brief Debounce counter. + */ +static unsigned cnt; + +/** + * @brief Card event sources. + */ +static EventSource inserted_event, removed_event; + +/** + * @brief Insertion monitor timer callback function. + * + * @param[in] p pointer to the @p BaseBlockDevice object + * + * @notapi + */ +static void tmrfunc(void *p) { + BaseBlockDevice *bbdp = p; + + /* The presence check is performed only while the driver is not in a + transfer state because it is often performed by changing the mode of + the pin connected to the CS/D3 contact of the card, this could disturb + the transfer.*/ + blkstate_t state = blkGetDriverState(bbdp); + chSysLockFromIsr(); + if ((state != BLK_READING) && (state != BLK_WRITING)) { + /* Safe to perform the check.*/ + if (cnt > 0) { + if (blkIsInserted(bbdp)) { + if (--cnt == 0) { + chEvtBroadcastI(&inserted_event); + } + } + else + cnt = POLLING_INTERVAL; + } + else { + if (!blkIsInserted(bbdp)) { + cnt = POLLING_INTERVAL; + chEvtBroadcastI(&removed_event); + } + } + } + chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, bbdp); + chSysUnlockFromIsr(); +} + +/** + * @brief Polling monitor start. + * + * @param[in] p pointer to an object implementing @p BaseBlockDevice + * + * @notapi + */ +static void tmr_init(void *p) { + + chEvtInit(&inserted_event); + chEvtInit(&removed_event); + chSysLock(); + cnt = POLLING_INTERVAL; + chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, p); + chSysUnlock(); +} + +/*===========================================================================*/ +/* Main and generic code. */ +/*===========================================================================*/ + +/* + * Card insertion event. + */ +static void InsertHandler(eventid_t id) { + + (void)id; +} + +/* + * Card removal event. + */ +static void RemoveHandler(eventid_t id) { + + (void)id; +} + /* * Green LED blinker thread, times are in milliseconds. */ @@ -47,6 +144,11 @@ static msg_t Thread1(void *arg) { * Application entry point. */ int main(void) { + static const evhandler_t evhndl[] = { + InsertHandler, + RemoveHandler + }; + struct EventListener el0, el1; /* * System initializations. @@ -68,6 +170,11 @@ int main(void) { */ sdStart(&SD6, NULL); + /* + * Activates the card insertion monitor. + */ + tmr_init(&SDCD1); + /* * Creates the blinker thread. */ @@ -87,12 +194,14 @@ int main(void) { /* * Normal main() thread activity, in this demo it does nothing except - * sleeping in a loop and check the button state. + * sleeping in a loop and listen for events. */ + chEvtRegister(&inserted_event, &el0, 0); + chEvtRegister(&removed_event, &el1, 1); while (TRUE) { usStateCheck(); if (palReadPad(GPIOA, GPIOA_BUTTON_WKUP) != 0) { } - chThdSleepMilliseconds(500); + chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); } }