FATFS_LinkDriver(&SD_Driver, SDPath) returning FR_DISC_ERR
Hi
I'm using STM32F407G-DISC1 with STM32F4DIS-BB expansion card for SD card. I've selected SDIO, SD1 bit and middleware as FatFs in cubeMX. I've generated code and added my custom code as follows:
#include "main.h"
#include "fatfs.h"
SD_HandleTypeDef hsd;
FATFS SDFatFs; /* File system object for SD card logical drive */
FIL MyFile; /* File object */
char SDPath[4]; /* SD card logical drive path */
static uint8_t buffer[_MAX_SS]; /* a work buffer for the f_mkfs() */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SDIO_SD_Init(void);
int main(void)
{
volatile FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
/*##-1- Link the micro SD disk I/O driver ##################################*/
res = FATFS_LinkDriver(&SD_Driver, SDPath);
if(res == 0)
{
/*##-2- Register the file system object to the FatFs module ##############*/
res = f_mount(&SDFatFs, (TCHAR const*)SDPath, 0);
if(res != FR_OK)
{
/* FatFs Initialization Error */
Error_Handler();
}
else
{
res = f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, buffer, sizeof(buffer));
if(res != FR_OK)
{
/* FatFs Format Error */
Error_Handler();
}
else
{
res = f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE);
if(res != FR_OK)
{
/* 'STM32.TXT' file Open for write Error */
Error_Handler();
}
else
{
/*##-5- Write data to the text file ################################*/
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
/*##-6- Close the open text file #################################*/
if (f_close(&MyFile) != FR_OK )
{
Error_Handler();
}
else
{}
}
}
}
}
/* USER CODE END 2 */
while (1)
{
}
}
static void MX_SDIO_SD_Init(void)
{
/* USER CODE BEGIN SDIO_Init 0 */
/* USER CODE END SDIO_Init 0 */
/* USER CODE BEGIN SDIO_Init 1 */
/* USER CODE END SDIO_Init 1 */
hsd.Instance = SDIO;
hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
hsd.Init.ClockDiv = 0;
/* USER CODE BEGIN SDIO_Init 2 */
/* USER CODE END SDIO_Init 2 */
}I've added SD card init code also. It is failing at FATFS_LinkDriver(&SD_Driver, SDPath) and returning FR_DISC_ERR. I'm using one of the latest versions of cubeMX. I've seen in the forums that this latest version of cubeMX has some issues with SD card. I've tried some of the solutions mentioned. But no luck.
How do I get my data written into SD card. Please help me out.
Aditya.
