diff --git a/demos/ARM7-LPC214x-GCC/chcore.c b/demos/ARM7-LPC214x-GCC/chcore.c index 5ee155eaa..128fb97aa 100644 --- a/demos/ARM7-LPC214x-GCC/chcore.c +++ b/demos/ARM7-LPC214x-GCC/chcore.c @@ -23,6 +23,7 @@ #include "vic.h" #include "lpc214x_serial.h" #include "lpc214x_ssp.h" +#include "mmcsd.h" #include "buzzer.h" @@ -135,6 +136,7 @@ void hwinit(void) { */ InitSerial(); InitSSP(); + InitMMC(); InitBuzzer(); } diff --git a/demos/ARM7-LPC214x-GCC/main.c b/demos/ARM7-LPC214x-GCC/main.c index 702a25d98..9e0a4222c 100644 --- a/demos/ARM7-LPC214x-GCC/main.c +++ b/demos/ARM7-LPC214x-GCC/main.c @@ -21,7 +21,8 @@ #include "lpc214x.h" #include "lpc214x_serial.h" -#include "lpc214x_ssp.h" +//#include "lpc214x_ssp.h" +#include "mmcsd.h" #include "buzzer.h" #include "evtimer.h" @@ -56,7 +57,7 @@ static t_msg Thread2(void *arg) { } static void TimerHandler(t_eventid id) { - static BYTE8 sspbuf[16]; +// static BYTE8 sspbuf[16]; t_msg TestThread(void *p); if (!(IO0PIN & 0x00018000)) { // Both buttons @@ -67,8 +68,11 @@ static void TimerHandler(t_eventid id) { if (!(IO0PIN & 0x00008000)) // Button 1 PlaySound(1000, 100); if (!(IO0PIN & 0x00010000)) { // Button 2 - sspRW(sspbuf, (BYTE8 *)"Hello World!\r\n", 14); - chFDDWrite(&COM1, sspbuf, 14); +// sspRW(sspbuf, (BYTE8 *)"Hello World!\r\n", 14); +// chFDDWrite(&COM1, sspbuf, 14); + chFDDWrite(&COM1, (BYTE8 *)"Hello World!\r\n", 14); + if (!mmcInit()) + PlaySound(2000, 500); } } } diff --git a/demos/ARM7-LPC214x-GCC/mmcsd.c b/demos/ARM7-LPC214x-GCC/mmcsd.c index ee4c2a93f..1c0d5c7d8 100644 --- a/demos/ARM7-LPC214x-GCC/mmcsd.c +++ b/demos/ARM7-LPC214x-GCC/mmcsd.c @@ -26,7 +26,7 @@ static EventSource MMCInsertEventSource; -void MMCInit(void) { +void InitMMC(void) { chEvtInit(&MMCInsertEventSource); } @@ -47,11 +47,11 @@ BOOL mmcInit(void) { sspRW(NULL, NULL, 16); /* 128 clock pulses without ~CS asserted. */ int i = 0; while (TRUE) { - chThdSleep(10); if (mmcSendCommand(0, 0) == 0x01) break; if (++i >= CMD0_RETRY) return TRUE; + chThdSleep(10); } /* @@ -59,15 +59,14 @@ BOOL mmcInit(void) { */ i = 0; while (TRUE) { - BYTE8 b; - chThdSleep(10); - b = mmcSendCommand(0, 0); + BYTE8 b = mmcSendCommand(1, 0); if (b == 0x00) break; if (b != 0x01) return TRUE; if (++i >= CMD1_RETRY) return TRUE; + chThdSleep(10); } /* @@ -77,21 +76,41 @@ BOOL mmcInit(void) { return FALSE; } +static void sendhdr(BYTE8 cmd, ULONG32 arg) { + BYTE8 buf[8]; + + buf[0] = 0xFF; + buf[1] = 0x40 | cmd; + buf[2] = arg >> 24; + buf[3] = arg >> 16; + buf[4] = arg >> 8; + buf[5] = arg; + buf[6] = 0x95; /* Valid for CMD0 ingnored by other commands. */ + buf[7] = 0xFF; + sspRW(NULL, buf, 8); +} + +static BYTE8 recvr1(void) { + int i; + BYTE8 r1[1]; + + for (i = 0; i < 8; i++) { + sspRW(r1, NULL, 1); + if (r1[0] != 0xFF) + return r1[0]; + } + return 0xFF; +} + /* * Sends a simple command and returns a R1-type response. */ BYTE8 mmcSendCommand(BYTE8 cmd, ULONG32 arg) { - BYTE8 buf[6]; + BYTE8 r1; - buf[0] = 0x40 | cmd; - buf[1] = arg >> 24; - buf[2] = arg >> 16; - buf[3] = arg >> 8; - buf[4] = arg; - buf[5] = 0x95; /* Valid for CMD0 ingnored by other commands. */ sspAcquireBus(); - sspRW(NULL, buf, 6); - sspRW(buf, NULL, 1); + sendhdr(cmd, arg); + r1 = recvr1(); sspReleaseBus(); - return buf[0]; + return r1; } diff --git a/demos/ARM7-LPC214x-GCC/mmcsd.h b/demos/ARM7-LPC214x-GCC/mmcsd.h index 40a559955..4c9f1d643 100644 --- a/demos/ARM7-LPC214x-GCC/mmcsd.h +++ b/demos/ARM7-LPC214x-GCC/mmcsd.h @@ -23,7 +23,7 @@ #define CMD0_RETRY 10 #define CMD1_RETRY 100 -void MMCInit(void); +void InitMMC(void); BOOL mmcInit(void); BYTE8 mmcSendCommand(BYTE8 cmd, ULONG32 arg); diff --git a/ports/ARM7-LPC214x/GCC/lpc214x_ssp.c b/ports/ARM7-LPC214x/GCC/lpc214x_ssp.c index 5224127cf..ab0c4f141 100644 --- a/ports/ARM7-LPC214x/GCC/lpc214x_ssp.c +++ b/ports/ARM7-LPC214x/GCC/lpc214x_ssp.c @@ -32,15 +32,14 @@ void sspAcquireBus(void) { chSemWait(&me); #endif IO0CLR = 1 << 20; - } void sspReleaseBus(void) { + IO0SET = 1 << 20; #ifdef SSP_USE_MUTEX chSemSignal(&me); #endif - IO0SET = 1 << 20; } /* @@ -103,7 +102,7 @@ void InitSSP(void) { PCONP = (PCONP & PCALL) | PCSPI1; /* Clock = PCLK / 2 (fastest). */ - SetSSP(2, CR0_DSS8BIT | CR0_FRFSPI | CR0_CLOCKRATE(0), CR1_LBM); + SetSSP(2, CR0_DSS8BIT | CR0_FRFSPI | CR0_CLOCKRATE(0), 0); #ifdef SSP_USE_MUTEX chSemInit(&me, 1);