SPI / SD on lpc2106
2005-10-11 by armqamp
Hello,
I'm trying to make an SD card to work with the lpc2106 and later want
to implement FAT.
The problem is I don't get any response of the SD card when I try to
initialize it.
Maybe somebody can disover what I'm doing wrong?
here is the code:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void ActivateSD(void){
unsigned int ii=0;
unsigned char resp, i;
unsigned char status;
print("Activating SD Card....\n\n");
// Turn off SD Card
SD_power_off;
// Wait for power to really go down
for(ii = 0; ii; ii++);
for(ii = 0; ii; ii++);
for(ii = 0; ii; ii++);
for(ii = 0; ii; ii++);
// Turn on SD Card
SD_power_on;
// Wait for power to really come up
for(status = 0; status < 10; ++status)
{
for(ii = 0; ii; ii++);
for(ii = 0; ii; ii++);
for(ii = 0; ii; ii++);
for(ii = 0; ii; ii++);
}
for(i=1; i<=10; i++){ //try 10 times to wake up the card
SD_disable; //CS high
InitSequence(); //send 80 clocks
SD_enable; //CS low
SD_SendCommand(CMD0,0,0,0,0); //send Reset command
resp = SD_Response();
if( resp == 0x01 ){ //wait for R1 response to be 0x01
break;
}
else{
}
}
spiSendByte(0xFF); // Response received, send the required 8
clocks after the response to finish up (page 5-6 Sandisk Manual)
if(i>10){
SD_disable;
print("SD activation failed\n");
}
else{
print("SD activation successfully\n");
while( resp != 0x00){ //try sending CMD1 until
response R1 is 0x00
SD_SendCommand(CMD1,0,0,0,0); //activate card initialization process
resp = SD_Response();
spiSendByte(0xFF); //send required 8 clocks
if(resp==0x00){ // Ready
print("SD successfully activated!!!\n");
break;
}
}
if(resp != 0x00){
print("Error: ");
print("0x"); printnum(16, 8, 0, '0', resp); print("\n");
}
}
print("\n-------end-----------\n");
}
unsigned char SD_Response(void) {
unsigned char i;
unsigned char resp;
for(i=0; i<10; i++) {
resp = spiGetByte();
if(resp != 0xFF) {
return resp;
}
}
return 0xFF;
}
void SD_SendCommand(unsigned char command, unsigned char arg_a,
unsigned char arg_b, unsigned char arg_c, unsigned char arg_d) {
spiSendByte(command);
spiSendByte(arg_a);
spiSendByte(arg_b);
spiSendByte(arg_c);
spiSendByte(arg_d);
spiSendByte(0x95); // correct CRC for first command in SPI after
that CRC is ignored, so no problem with always sending 0x95
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@