void initSPI (void)
{
UCTL0 = SWRST; // 8-bit SPI Master **SWRST**
UTCTL0 = CKPH | SSEL1 | SSEL0 | STC; // SMCLK, 3-pin mode, clock idle low, data valid on rising edge, UCLK delayed
UBR00 = 0x20; // 0x02: UCLK/2 (4 MHz), works also with 3 and 4
UBR10 = 0x00; // -"-
UMCTL0 = 0x00; // no modulation
UCTL0 = CHAR | SYNC | MM | SWRST; // 8-bit SPI Master **SWRST**
UCTL0 &= ~SWRST; // clear SWRST
ME1 |= USPIE0; // Enable USART1 SPI mode
while (!(IFG1 & UTXIFG0)); // USART1 TX buffer ready (empty)?
}
// Initialize MMC card
char initMMC (void)
{
//raise SS and MOSI for 80 clock cycles
//SendByte(0xff) 10 times with SS high
//RAISE SS
int i;
// Port 3 Function Dir On/Off
// 3.0-mmcCD Out 0 - card inserted
// 3.1-Dout Out 0 - off 1 - On -> init in SPI_Init
// 3.2-Din Inp 0 - off 1 - On -> init in SPI_Init
// 3.3-Clk Out - -> init in SPI_Init
// 2.6-mmcCS Out 0 - Active 1 - none Active
P2DIR |= BIT6;
P3SEL |= 0x0E;
P3SEL &= ~0x11;
P3OUT |= 0x10;
P3DIR |= 0x0A;
initSPI();
//initialization sequence on PowerUp
CS_LOW();
for(i=0;i<10;i++)
spiSendByte(0xff);
CS_HIGH();
for(i=0;i<=9;i++)
spiSendByte(0xff);
return (mmc_GoIdle());
}
char mmc_GoIdle(void)
{
char response=0x01;
CS_LOW();
//Send Command 0 to put MMC in SPI mode
mmcSendCmd(MMC_GO_IDLE_STATE,0,0x95);
//Now wait for READY RESPONSE
if(mmcGetResponse()!=0x00)//应该是收到响应R1,也就是0,可是我总是收到是高电平,也不知是不是太快了,没收到,示波器看了,没有低电平的产生
return MMC_INIT_ERROR;
while(response==0x00)
{
CS_HIGH();
spiSendByte(0xff);
CS_LOW();
mmcSendCmd(MMC_SEND_OP_COND,0x00,0xff);
response=mmcGetResponse();
}
CS_HIGH();
spiSendByte(0xff);
return (MMC_SUCCESS);
}
// mmc Get Responce
char mmcGetResponse(void)
{
//Response comes 1-8bytes after command
//the first bit will be a 0
//followed by an error code
//data will be 0xff until response
int i=0;
char response;
while(i<=64)
{
response=spiSendByte(0xff);
if(response==0x00)break;
if(response==0x01)break;
i++;
}
return response;
}