From 853b0fd51c80d9b8640639321a950f16800d57d4 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Tue, 26 Jun 2012 18:02:43 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4348 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARMCM3-STM32F103-FATFS/main.c | 93 +++++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) (limited to 'demos/ARMCM3-STM32F103-FATFS') diff --git a/demos/ARMCM3-STM32F103-FATFS/main.c b/demos/ARMCM3-STM32F103-FATFS/main.c index 16d04dd49..d3e1e7e73 100644 --- a/demos/ARMCM3-STM32F103-FATFS/main.c +++ b/demos/ARMCM3-STM32F103-FATFS/main.c @@ -30,7 +30,85 @@ #include "ff.h" /*===========================================================================*/ -/* MMC/SPI related. */ +/* 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); + if ((state == BLK_READING) || (state == BLK_WRITING)) + return; + + /* Safe to perform the check.*/ + chSysLockFromIsr(); + 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(); +} + +/*===========================================================================*/ +/* FatFs related. */ /*===========================================================================*/ /** @@ -190,6 +268,10 @@ static const ShellConfig shell_cfg1 = { commands }; +/*===========================================================================*/ +/* Main and generic code. */ +/*===========================================================================*/ + /* * Red LEDs blinker thread, times are in milliseconds. */ @@ -277,6 +359,11 @@ int main(void) { mmcObjectInit(&MMCD1); mmcStart(&MMCD1, &mmccfg); + /* + * Activates the card insertion monitor. + */ + tmr_init(&MMCD1); + /* * Creates the blinker thread. */ @@ -286,8 +373,8 @@ int main(void) { * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and listen for events. */ - chEvtRegister(&MMCD1.inserted_event, &el0, 0); - chEvtRegister(&MMCD1.removed_event, &el1, 1); + chEvtRegister(&inserted_event, &el0, 0); + chEvtRegister(&removed_event, &el1, 1); while (TRUE) { if (!shelltp) shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); -- cgit v1.2.3