aboutsummaryrefslogtreecommitdiffstats
path: root/demos/ARM7-AT91SAM7X-FATFS-GCC
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-07-02 09:13:14 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-07-02 09:13:14 +0000
commitc1da0e9344cc90bb8f1aa6e75d4706a7d5f8221c (patch)
treec31b37765d25bea282cd93aaa975c8318af34415 /demos/ARM7-AT91SAM7X-FATFS-GCC
parent3440f11481452204d68283230b2e421df01e5d56 (diff)
downloadChibiOS-c1da0e9344cc90bb8f1aa6e75d4706a7d5f8221c.tar.gz
ChibiOS-c1da0e9344cc90bb8f1aa6e75d4706a7d5f8221c.tar.bz2
ChibiOS-c1da0e9344cc90bb8f1aa6e75d4706a7d5f8221c.zip
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4383 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'demos/ARM7-AT91SAM7X-FATFS-GCC')
-rw-r--r--demos/ARM7-AT91SAM7X-FATFS-GCC/main.c97
1 files changed, 94 insertions, 3 deletions
diff --git a/demos/ARM7-AT91SAM7X-FATFS-GCC/main.c b/demos/ARM7-AT91SAM7X-FATFS-GCC/main.c
index 0b3f838fd..c1b5c5a1d 100644
--- a/demos/ARM7-AT91SAM7X-FATFS-GCC/main.c
+++ b/demos/ARM7-AT91SAM7X-FATFS-GCC/main.c
@@ -29,6 +29,87 @@
#include "evtimer.h"
#include "ff.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();
+}
+
+/*===========================================================================*/
+/* FatFs related. */
+/*===========================================================================*/
+
#define MAX_SPI_BITRATE 100
#define MIN_SPI_BITRATE 250
@@ -198,6 +279,10 @@ static const ShellConfig shell_cfg1 = {
commands
};
+/*===========================================================================*/
+/* Main and generic code. */
+/*===========================================================================*/
+
/*
* LCD blinker thread, times are in milliseconds.
*/
@@ -242,6 +327,7 @@ static void InsertHandler(eventid_t id) {
static void RemoveHandler(eventid_t id) {
(void)id;
+ mmcDisconnect(&MMCD1);
fs_ready = FALSE;
}
@@ -285,6 +371,11 @@ int main(void) {
mmcStart(&MMCD1, &mmccfg);
/*
+ * Activates the card insertion monitor.
+ */
+ tmr_init(&MMCD1);
+
+ /*
* Creates the blinker threads.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
@@ -293,8 +384,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);
@@ -302,7 +393,7 @@ int main(void) {
chThdRelease(shelltp); /* Recovers memory of the previous shell. */
shelltp = NULL; /* Triggers spawning of a new shell. */
}
- chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS));
+ chEvtDispatch(evhndl, chEvtWaitOneTimeout(ALL_EVENTS, MS2ST(500)));
}
return 0;
}