Hello I've been trying to initialize an SD card in SPI mode (yes I know there is a SD library) so that I can read the raw data from the card and hopefully recover some of the stuff that was on one of my cards that has corrupted. If I can't manage to save anything at least I'll learn a thing or two about SD cards in the process.
So the problem i've been having is that my code seems to halt after SPI.transfer() i've been troubleshooting by writing to the Serial console at different parts in the code
#include <SPI.h>
const int CS_PIN = 10;
const byte GO_IDLE_STATE[6] = {0x40,0x00,0x00,0x00,0x00,0x95};
const byte CARD_READY[6] = {0x41,0x00,0x00,0x00,0x00,0xFF};
const byte SET_BLOCK_SIZE[6] = {0x50,0x00,0x00,0x02,0x00,0xFF};
const byte READ_BLOCK[6] = {0x51,0x00,0x00,0x00,0x00,0xFF};
void setup(){
Serial.begin(9600);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
}
void loop(){
Serial.println("Initializing.....");
for(int i = 0; i < 10; i++){
SPI.transfer(0xFF); //these 10 SPI.transfer() s dont halt the code
}
SD_Command(GO_IDLE_STATE);
SD_Command(CARD_READY);
SD_Command(SET_BLOCK_SIZE);
while(1){} //this is just to stop it so i can see what the output is
}
void repeatSPI(int num, byte data){
for(int i = 0; i < num; i++){
SPI.transfer(data);
}
}
byte getSDResponse(){
byte R = 0xFF;
byte temp;
for(int i = 0; i < 8; i++){
if(R == 0xFF){
R = SPI.transfer(0xFF); //the first time an SPI.transfer() call is made after the first loop the code halts
temp = R;
}
else{
temp = SPI.transfer(0xFF);
}
Serial.print(temp,HEX);
if(i < 7){Serial.print(":");}
}
Serial.println();
return R;
}
byte SD_Command(const byte cmd[]){
byte temp;
pinMode(CS_PIN, LOW);
Serial.print("Sending Command: ");
for(int i = 0; i < 6; i++){
Serial.print(cmd[i], HEX);
if(i<5){
Serial.print(":");
}
}
Serial.println();
for(int i = 0; i < 6; i++){
shiftOut(11,13,MSBFIRST,cmd[i]); //I originally tried to get around the problem by using shiftOut instead of SPI.transfer()
}
temp = getSDResponse();
pinMode(CS_PIN, HIGH);
return temp;
}
any help would be appreciated thanks