/************************** MMC Init **************************************/
/* Initialises the MMC into SPI mode and sets block size, returns 0 on success */
int mmc_init()
{
int i;
spi_init();
spi_disable_cs(); // set SS = 1 (off)
for(i=0;i<12;i++) // initialise the MMC card into SPI mode by sending clks on
{
spi_write(0xFF);
}
spi_enable_cs(); // set SS = 0 (on) tells card to go to spi mode when it receives reset
spi_write(0x40); // send reset command
spi_write(0x00); // all the arguments are 0x00 for the reset command
spi_write(0x00);
spi_write(0x00);
spi_write(0x00);
spi_write(0x95); // precalculated checksum as we are still in MMC mode
if(mmc_response(0x01)==1) return 1; // if = 1 then there was a timeout waiting for 0x01 from the mmc
/************************** MMC get response **************************************/
/**** Repeatedly reads the MMC until we get the response we want or timeout ****/
int mmc_response(unsigned char response)
{
unsigned long count = 0xFFFF; // 16bit repeat, it may be possible to shrink this to 8 bit but there is not much point
while(spi_write(0xFF) != response && --count > 0);
if(count==0) return 1; // loop was exited due to timeout
else return 0; // loop was exited before timeout
}