cancel
Showing results for 
Search instead for 
Did you mean: 

FATFS_LinkDriver(&SD_Driver, SDPath) returning FR_DISC_ERR

VTOL_Aviations
Associate II

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.

2 REPLIES 2

Perhaps you can port some of the HAL examples?

I wouldn't use f_mkfs() like this, most cards don't need formatting.

Your option really is to go debug the lower levels of code to determine the source of the error.

Try increasing ClockDiv to 2 or 4, to see if it is just a speed issue.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi Clive,

Actually this is from the HAL examples. In the example he tries to write and read into the SD card. But here I'm trying to only write into SD card.

And as you suggested, I've tried debugging to find the source of error. I've removed the link and f_mkfs() APIs. Turns out, f_mount is returning FR_OK(funny part is, it is returning FR_OK even though the SD card is taken out.) and f_open is returning FR_NOT_READY. It is returning error at find_volume() in ff.c. In the comments it is written that the FR_NOT_READY is caused due to "Failed to initialize due to no medium or hard error ". Does that mean it is able to mount but not able to find any space in the disc to open the file? How to solve this?

Aditya.