diff options
author | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2012-06-26 18:02:43 +0000 |
---|---|---|
committer | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2012-06-26 18:02:43 +0000 |
commit | 853b0fd51c80d9b8640639321a950f16800d57d4 (patch) | |
tree | 5253c1c4296dd8415b0ea3fc72b6c66d03cd5c7f /demos/ARMCM3-STM32F103-FATFS | |
parent | 80443125b286868f18342a9e6884a65b3218bf99 (diff) | |
download | ChibiOS-853b0fd51c80d9b8640639321a950f16800d57d4.tar.gz ChibiOS-853b0fd51c80d9b8640639321a950f16800d57d4.tar.bz2 ChibiOS-853b0fd51c80d9b8640639321a950f16800d57d4.zip |
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4348 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'demos/ARMCM3-STM32F103-FATFS')
-rw-r--r-- | demos/ARMCM3-STM32F103-FATFS/main.c | 93 |
1 files changed, 90 insertions, 3 deletions
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.
*/
@@ -278,6 +360,11 @@ int main(void) { mmcStart(&MMCD1, &mmccfg);
/*
+ * Activates the card insertion monitor.
+ */
+ tmr_init(&MMCD1);
+
+ /*
* Creates the blinker thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
@@ -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);
|