I'm using SDIO (1-bit mode) with an STM32F429 Discovery board to read from and write to an SD card. The code below works, I've seen that it does, the problem is it seems to randomly choose when to work and when to fail. It always fails on the f_open stage. Each time I reset the board it either works or it doesn't. I've even tried putting the f_open part in a loop to see if it won't work after a couple of tries, but it fails all 10 times. The error it gives when it fails is FR_DISK_ERR.
if(f_mount(&SDFatFS, (TCHAR const*) SDPath, 0) != FR_OK){
Error_Handler();
HAL_GPIO_WritePin(R_LED_GPIO_Port, R_LED_Pin, GPIO_PIN_SET);
}
else{
res = f_open(&SDFile, "FILE.TXT", FA_READ | FA_OPEN_ALWAYS);
if(res != FR_OK){
printf("f_open r: %d\n", res);
Error_Handler();
HAL_GPIO_WritePin(R_LED_GPIO_Port, R_LED_Pin, GPIO_PIN_SET);
}
else{
res = f_read(&SDFile, readBuff, sizeof(readBuff), (void*) &bytesRead);
if((res != FR_OK) || (bytesRead == 0)){
printf("f_read: %d\n", res);
Error_Handler();
HAL_GPIO_WritePin(R_LED_GPIO_Port, R_LED_Pin, GPIO_PIN_SET);
}else{
printf("%s\n", readBuff);
HAL_GPIO_WritePin(G_LED_GPIO_Port, G_LED_Pin, GPIO_PIN_SET);
}
f_close(&SDFile);
}
}
Any advice would be appreciated, thanks!
UPDATE: If I force mount it (f_mount(&SDFatFS, (TCHAR const*) SDPath, 1)), it also fails on DISK_ERR so it seems the mounting process is the one failing, but I still see no pettern as to why. I put a 1s delay in start-up to give the SD card time to "get ready", I suppose, but no dice.
UPDATE: Added pull-ups to the D0 and CMD lines and it drastically increased the success to fail ratio to a more workable level.