nt mmc_init()
{
DWORD i;
/* Generate a data pattern for write block */
for(i=0;i<MMC_DATA_SIZE;i++)
{
MMCWRData[i] = i;
{
MMCStatus = 0;
IOSET0 = SPI_SEL; /* set SPI SSEL */
/* initialise the MMC card into SPI mode by sending 80 clks on */
/* Use MMCRDData as a temporary buffer for SPI_Send() */
for(i=0; i<10; i++)
{
MMCRDData[i] = 0xFF;
}
SPI_Send( MMCRDData, 10 );
IOCLR0 = SPI_SEL; /* clear SPI SSEL */
/* send CMD0(RESET or GO_IDLE_STATE) command, all the arguments
are 0x00 for the reset command, precalculated checksum */
MMCCmd[0] = 0x40;
MMCCmd[1] = 0x00;
MMCCmd[2] = 0x00;
MMCCmd[3] = 0x00;
MMCCmd[4] = 0x00;
MMCCmd[5] = 0x95;
SPI_Send( MMCCmd, MMC_CMD_SIZE );
/* if = 1 then there was a timeout waiting for 0x01 from the MMC */
if( mmc_response(0x01) == 1 )
{
MMCStatus = IDLE_STATE_TIMEOUT;
IOSET0 = SPI_SEL; /* set SPI SSEL */
return MMCStatus;
}
/* Send some dummy clocks after GO_IDLE_STATE */
IOSET0 = SPI_SEL; /* set SPI SSEL */
SPI_ReceiveByte();
IOCLR0 = SPI_SEL; /* clear SPI SSEL */
/* must keep sending command until zero response ia back. */
i = MAX_TIMEOUT;
do
{
/* send mmc CMD1(SEND_OP_COND) to bring out of idle state */
/* all the arguments are 0x00 for command one */
MMCCmd[0] = 0x41;
MMCCmd[1] = 0x00;
MMCCmd[2] = 0x00;
MMCCmd[3] = 0x00;
MMCCmd[4] = 0x00;
/* checksum is no longer required but we always send 0xFF */
MMCCmd[5] = 0xFF;
SPI_Send( MMCCmd, MMC_CMD_SIZE );
i--;
} while ( (mmc_response(0x00) != 0) && (i>0) );
/* timeout waiting for 0x00 from the MMC */
if ( i == 0 )
{
MMCStatus = OP_COND_TIMEOUT;
IOSET0 = SPI_SEL; /* set SPI SSEL */
return MMCStatus;
}
/* Send some dummy clocks after SEND_OP_COND */
IOSET0 = SPI_SEL; /* set SPI SSEL */
SPI_ReceiveByte();
IOCLR0 = SPI_SEL; /* clear SPI SSEL */
/* send MMC CMD16(SET_BLOCKLEN) to set the block length */
MMCCmd[0] = 0x50;
MMCCmd[1] = 0x00; /* 4 bytes from here is the block length */
/* LSB is first */
/* 00 00 00 10 set to 16 bytes */
/* 00 00 02 00 set to 512 bytes */
MMCCmd[2] = 0x00;
/* high block length bits - 512 bytes */
MMCCmd[3] = 0x02;
/* low block length bits */
MMCCmd[4] = 0x00;
/* checksum is no longer required but we always send 0xFF */
MMCCmd[5] = 0xFF;
SPI_Send( MMCCmd, MMC_CMD_SIZE );
if( (mmc_response(0x00))==1 )
{
MMCStatus = SET_BLOCKLEN_TIMEOUT;
IOSET0 = SPI_SEL; /* set SPI SSEL */
return MMCStatus;
}
IOSET0 = SPI_SEL; /* set SPI SSEL */
SPI_ReceiveByte();
return 0;
}