response = SD_Command(CMD24_WRITE_SINGLE_BLOCK , startBlock,0x00); // if reponse not zero then there is error // try calling sdhc_write_block again
if(response != 0)
return response;
SD_CSenable(); //enable chip select by setting it zero
SPI_Byte( 0xfe ); // start block token 11111110b
for(i = 0; i < 512; i++) //write globally defined buffer into sd card
{
SPI_Byte(buffer1);
}
SPI_Byte(0xff); // bogus CRC (16-bit), CRC ignored
SPI_Byte(0xff);
response = SPI_Byte(0xff);
if( (response & 0x1f) != 0x05)
{
SD_CSdisable();
return response;
}
count = 0xfffe;
while(!SPI_Byte(0xff)) // wait for write complete and go idle
{
if(count-- < 10)
{
SD_CSdisable();
return SDHC_ERR_W_TIMEOUT;
}
}
SD_CSdisable();
SPI_Byte(0xff); // 8 clock cycle delay while SS line high
SD_CSenable(); // reset SS line low to verify if card still busy
[B][U]count = 0;
while(!SPI_Byte(0xff)) // wait for write complete and go idle
{
if(count++ > 0xfffe)
{
SD_CSdisable();
return SDHC_ERR_W_TIMEOUT;}
}
SD_CSdisable();[/U][/B]
return SDHC_ERR_NONE; |