368,7 → 368,6 |
return SDC_GetData(CMD_SEND_CSD, 0UL, pCSD, 16); |
} |
|
|
//________________________________________________________________________________________________________________________________________ |
// Funtion: SDC_Init(void); |
// |
378,21 → 377,24 |
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode. |
//________________________________________________________________________________________________________________________________________ |
|
SD_Result_t SDC_Init(void) |
SD_Result_t SDC_Init(unsigned char print) |
{ |
u32 timeout = 0; |
u16 speed; |
u8 rsp[6]; // SD-SPI response buffer |
SD_Result_t result = SD_ERROR_UNKNOWN; |
if(SPI_Speed < 500) SPI_Speed = 500; // nur zur Sicherheit |
speed = SPI_Speed; // weil das bei SSC_Init() auf 400 gesetzt werden würde |
|
//SDCardWriteRetryCounterMax = 0; |
|
if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot |
{ |
UART1_PutString("\r\n SSC init..."); |
if(print) UART1_PutString("\r\n SSC init..."); |
SSC_Init(); |
UART1_PutString("ok"); |
if(print) UART1_PutString("ok"); |
|
UART1_PutString("\r\n SDC init..."); |
if(print) UART1_PutString("\r\n SDC init..."); |
SDCardInfo.Valid = 0; |
/* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and |
start to supply at least 74 SD clocks to the SD card with keeping cmd line to high. In case of SPI |
530,7 → 532,9 |
SSC_Disable(); // set SD_CS high |
// here is the right place to inrease the SPI baud rate to maximum |
|
SSC_Speed(2000); |
SSC_Speed(speed); |
if(print) { sprintf(text,"\r\n Setting SPI speed to %d ", SPI_Speed);UART1_PutString(text); } |
|
SSC_Enable(); // set SD_CS high |
|
// read CID register |
538,10 → 542,11 |
if(result != SD_SUCCESS) |
{ |
|
UART1_PutString(" Set interface to low speed..."); |
if(print) UART1_PutString(" failed - Set interface to low speed..."); |
SSC_Disable(); // set SD_CS high |
// here is the right place to inrease the SPI baud rate to maximum |
SSC_Speed(1000); |
SPI_Speed = 1000; |
SSC_Speed(SPI_Speed); |
SSC_Enable(); // set SD_CS high |
result = SDC_GetCID((u8 *)&SDCardInfo.CID); |
} |
561,7 → 566,7 |
goto end; |
} |
|
UART1_PutString("ok\r\n"); |
if(print) UART1_PutString("ok\r\n"); |
|
u8 c_size_mult, read_bl_len; |
u32 c_size; |
608,7 → 613,7 |
break; |
} |
|
switch(SDCardInfo.Version) |
if(print) switch(SDCardInfo.Version) |
{ |
case VER_1X: |
UART1_PutString(" SD-CARD V1.x"); |
618,11 → 623,14 |
default: |
break; |
} |
u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L)); |
sprintf(text, "\r\n Capacity = %i MB", mb_size); |
UART1_PutString(text); |
if(print) |
{ |
u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L)); |
sprintf(text, "\r\n Capacity = %i MB", mb_size); |
UART1_PutString(text); |
} |
|
SDC_PrintCID((u8 *)&SDCardInfo.CID); |
if(print) SDC_PrintCID((u8 *)&SDCardInfo.CID); |
SDCardInfo.Valid = 1; |
// jump point for error condition before |
end: |
767,12 → 775,25 |
//SSC_Disable(); // disable CS |
if(result != SD_SUCCESS) |
{ |
sprintf(text,"Error %02X writing data to sd card (R=%02X).\r\n", result, rsp); |
|
sprintf(text,"\r\nError %02X writing data to sd card (R=%02X)", result, rsp); |
UART1_PutString(text); |
retryCounter++; |
} |
else break; // Success -> end retry loop |
|
if(SPI_Speed > 1000) |
{ |
SPI_Speed -= 200; |
sprintf(text," new Speed = %d", SPI_Speed); |
UART1_PutString(text); |
} |
SDC_Init(0); |
} |
else |
{ |
if(retryCounter == 1) UART1_PutString(" Problem solved\r\n"); |
break; // Success -> end retry loop |
} |
|
} |
if (retryCounter > SDCardWriteRetryCounterMax) |
{ SDCardWriteRetryCounterMax = retryCounter; |