I've stepped through the code and I get an error when the disk is being initialized and returns 'FR_NOT_READY' from the 'disk_initialize(fs->drv);' function.
This is a generic error. You need to find what specific problem is causing it.
Searching for the error code FR_NOT_READY, we find (in "ff.c"):-
stat = disk_initialize(pdrv);
if (stat & STA_NOINIT) return FR_NOT_READY;Searching for STA_NOINIT we find in "diskio.h"
/* Disk Status Bits (DSTATUS) */
#define STA_NOINIT 0x01 /* Drive not initialized */and in "sd_diskio.c"
DSTATUS SD_initialize(BYTE lun)
{
Stat = STA_NOINIT;
/* Configure the uSD device */
if(BSP_SD_Init() == MSD_OK)
{
Stat &= ~STA_NOINIT;
}
return Stat;
}
So if BSP_SD_Init() doesn't return MSD_OK then you will get the error FR_NOT_READY.
Now let's see what might cause BSP_SD_Init() to fail. In "bsp_driver_sd.c":-
uint8_t BSP_SD_Init(void)
{
uint8_t SD_state = MSD_OK;
/* Check if the SD card is plugged in the slot */
if (BSP_SD_IsDetected() != SD_PRESENT)
{
return MSD_ERROR;
}
SD_state = HAL_SD_Init(&hsd, &SDCardInfo);
#ifdef BUS_4BITS
if (SD_state == MSD_OK)
{
if (HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B) != SD_OK)
{
SD_state = MSD_ERROR;
}
else
{
SD_state = MSD_OK;
}
}
#endif
return SD_state;
}But checking if the SD card is plugged in cannot fail, because...
uint8_t BSP_SD_IsDetected(void)
{
__IO uint8_t status = SD_PRESENT;
/* USER CODE BEGIN 1 */
/* user code can be inserted here */
/* USER CODE END 1 */
return status;
}which leaves HAL_SD_Init() and HAL_SD_WideBusOperation_Config() as the only possible culprits.
The first function called by HAL_SD_Init() is HAL_SD_MspInit(hsd) (in "stm32f4xx_hal_msp.c"):-
void HAL_SD_MspInit(SD_HandleTypeDef* hsd)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hsd->Instance==SDIO)
{
/* USER CODE BEGIN SDIO_MspInit 0 */
/* USER CODE END SDIO_MspInit 0 */
/* Peripheral clock enable */
__SDIO_CLK_ENABLE();
/**SDIO GPIO Configuration
PC12 ------> SDIO_CK
PC11 ------> SDIO_D3
PC10 ------> SDIO_D2
PD2 ------> SDIO_CMD
PC9 ------> SDIO_D1
PC8 ------> SDIO_D0
*/
GPIO_InitStruct.Pin = uSD_CLK_Pin|uSD_D3_Pin|uSD_D2_Pin|uSD_D1_Pin
|uSD_D0_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = uSD_CMD_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
HAL_GPIO_Init(uSD_CMD_GPIO_Port, &GPIO_InitStruct);
/* USER CODE BEGIN SDIO_MspInit 1 */
/* USER CODE END SDIO_MspInit 1 */
}
}
Finally we begin to find out what it is actually doing!
The code looks complicated, but I'm betting it's fine and you have a wiring error or incorrect hardware configuration. First thing to do is make sure the sd card is wired to the correct pins and has good power. Then verify that the SPI clock rate is correct (should run at <=400kHz during card initialization).