diff options
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);
|