From aece9ea5fcc12bc2ac7178bb231c9b7084518ff3 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sat, 3 Nov 2007 16:25:52 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@83 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/ARM7-LPC214x-GCC/buzzer.c | 10 ++++++++ demos/ARM7-LPC214x-GCC/buzzer.h | 1 + demos/ARM7-LPC214x-GCC/main.c | 44 ++++++++++++++++++++++----------- demos/ARM7-LPC214x-GCC/mmcsd.c | 55 ++++++++++++++++++++++++++++++++++++++++- demos/ARM7-LPC214x-GCC/mmcsd.h | 21 ++++++++++------ 5 files changed, 108 insertions(+), 23 deletions(-) (limited to 'demos') diff --git a/demos/ARM7-LPC214x-GCC/buzzer.c b/demos/ARM7-LPC214x-GCC/buzzer.c index b0abf9657..a93bafa98 100644 --- a/demos/ARM7-LPC214x-GCC/buzzer.c +++ b/demos/ARM7-LPC214x-GCC/buzzer.c @@ -80,3 +80,13 @@ void PlaySound(int freq, t_time duration) { chSysUnlock(); } + +void PlaySoundWait(int freq, t_time duration) { + TC *tc = T1Base; + + StopCounter(tc); + tc->TC_MR0 = tc->TC_MR1 = (PCLK / (freq * 2)); + StartCounter(tc); + chThdSleep(duration); + StopCounter(tc); +} \ No newline at end of file diff --git a/demos/ARM7-LPC214x-GCC/buzzer.h b/demos/ARM7-LPC214x-GCC/buzzer.h index 3334d8b5c..f5f9e82ac 100644 --- a/demos/ARM7-LPC214x-GCC/buzzer.h +++ b/demos/ARM7-LPC214x-GCC/buzzer.h @@ -22,6 +22,7 @@ void InitBuzzer(void); void PlaySound(int freq, t_time duration); +void PlaySoundWait(int freq, t_time duration); extern EventSource BuzzerSilentEventSource; diff --git a/demos/ARM7-LPC214x-GCC/main.c b/demos/ARM7-LPC214x-GCC/main.c index 614020271..9677918fd 100644 --- a/demos/ARM7-LPC214x-GCC/main.c +++ b/demos/ARM7-LPC214x-GCC/main.c @@ -55,8 +55,6 @@ static t_msg Thread2(void *arg) { return 0; } -static BYTE8 rwbuf[512]; - static void TimerHandler(t_eventid id) { t_msg TestThread(void *p); @@ -68,32 +66,50 @@ static void TimerHandler(t_eventid id) { if (!(IO0PIN & 0x00008000)) // Button 1 PlaySound(1000, 100); if (!(IO0PIN & 0x00010000)) { // Button 2 - MMCCSD data; - chFDDWrite(&COM1, (BYTE8 *)"Hello World!\r\n", 14); - if (mmcInit()) - return; - if (mmcGetSize(&data)) - return; - if (mmcBlockRead(0x200000, rwbuf)) - return; PlaySound(2000, 100); } } } +static void InsertHandler(t_eventid id) { + static BYTE8 rwbuf[512]; + MMCCSD data; + + PlaySoundWait(1000, 100); + PlaySoundWait(2000, 100); + if (mmcInit()) + return; + /* Card ready, do stuff.*/ + if (mmcGetSize(&data)) + return; + if (mmcBlockRead(0x200000, rwbuf)) + return; +} + +static void RemoveHandler(t_eventid id) { + + PlaySoundWait(2000, 100); + PlaySoundWait(1000, 100); +} + static BYTE8 waThread3[UserStackSize(128)]; static EvTimer evt; -static t_evhandler evhndl[1] = { - TimerHandler +static t_evhandler evhndl[] = { + TimerHandler, + InsertHandler, + RemoveHandler }; static t_msg Thread3(void *arg) { - struct EventListener el; + struct EventListener el0, el1, el2; evtInit(&evt, 500); - evtRegister(&evt, &el, 0); evtStart(&evt); + mmcStartPolling(); + evtRegister(&evt, &el0, 0); + chEvtRegister(&MMCInsertEventSource, &el1, 1); + chEvtRegister(&MMCRemoveEventSource, &el2, 2); while (TRUE) chEvtWait(ALL_EVENTS, evhndl); return 0; diff --git a/demos/ARM7-LPC214x-GCC/mmcsd.c b/demos/ARM7-LPC214x-GCC/mmcsd.c index 7867782c9..fb1c5c650 100644 --- a/demos/ARM7-LPC214x-GCC/mmcsd.c +++ b/demos/ARM7-LPC214x-GCC/mmcsd.c @@ -24,7 +24,10 @@ #include "mmcsd.h" -static EventSource MMCInsertEventSource; +EventSource MMCInsertEventSource, MMCRemoveEventSource; + +static VirtualTimer vt; +static int cnt; /* * Subsystem initialization. @@ -32,6 +35,56 @@ static EventSource MMCInsertEventSource; void InitMMC(void) { chEvtInit(&MMCInsertEventSource); + chEvtInit(&MMCRemoveEventSource); + cnt = POLLING_INTERVAL; +} + +void tmrfunc(void *par) { + + if (cnt) { + if (!(IO1PIN & (1 << 25))) { + if (!--cnt) + chEvtSendI(&MMCInsertEventSource); + } + else + cnt = POLLING_INTERVAL; + } + else { + if (IO1PIN & (1 << 25)) { + cnt = POLLING_INTERVAL; + chEvtSendI(&MMCRemoveEventSource); + } + } + chVTSetI(&vt, 10, tmrfunc, NULL); +} + +void mmcStartPolling(void) { + + chSysLock(); + + if (!chVTIsArmedI(&vt)) { + chVTSetI(&vt, 10, tmrfunc, NULL); + cnt = POLLING_INTERVAL; + } + + chSysUnlock(); +} + +void mmcStopPolling(void) { + + chSysLock(); + + if (chVTIsArmedI(&vt)) { + chVTResetI(&vt); + cnt = POLLING_INTERVAL; + } + + chSysUnlock(); +} + +BOOL mmcCardInserted (void) { + + return cnt == 0; } static void sendhdr(BYTE8 cmd, ULONG32 arg) { diff --git a/demos/ARM7-LPC214x-GCC/mmcsd.h b/demos/ARM7-LPC214x-GCC/mmcsd.h index 047fd0fb2..142d5f1a6 100644 --- a/demos/ARM7-LPC214x-GCC/mmcsd.h +++ b/demos/ARM7-LPC214x-GCC/mmcsd.h @@ -23,26 +23,31 @@ #define NICE_WAITING /* Following times are 10mS units.*/ -#define CMD0_RETRY 10 -#define CMD1_RETRY 100 +#define CMD0_RETRY 10 +#define CMD1_RETRY 100 +#define POLLING_INTERVAL 10 /* Byte transfer time units.*/ -#define MMC_WAIT_DATA 10000 +#define MMC_WAIT_DATA 10000 -#define CMDGOIDLE 0 -#define CMDINIT 1 -#define CMDREADCSD 9 -#define CMDREAD 17 -#define CMDWRITE 24 +#define CMDGOIDLE 0 +#define CMDINIT 1 +#define CMDREADCSD 9 +#define CMDREAD 17 +#define CMDWRITE 24 typedef struct { ULONG32 csize; ULONG32 rdblklen; } MMCCSD; +extern EventSource MMCInsertEventSource, MMCRemoveEventSource; + void InitMMC(void); BOOL mmcInit(void); +void mmcStartPolling(void); +void mmcStopPolling(void); BYTE8 mmcSendCommand(BYTE8 cmd, ULONG32 arg); BOOL mmcGetSize(MMCCSD *data); BOOL mmcBlockRead(ULONG32 blknum, BYTE8 *buf); -- cgit v1.2.3