Skip to main content
Ali Esmailpor
Associate II
January 9, 2020
Question

FR_DISK_ERROR always is returned by f_open.

  • January 9, 2020
  • 18 replies
  • 14247 views

Hi all.

My team has been working on a project that contains a SD card based on Standard Library. Recently we've decided to migrate to HAL and it started.

Fortunately, All part of our project were changed as well as possible to HAL and they're working great but we don't know why SD card doesn't work well.

We have not changed peripheral's configuration clocks, but we had to change "clock frequency of the SDMMC controller" to 1.5MHz in HAL while it was 24MHz in STDLibrary. Because, it didn't work at all.

In addition, our customers are using a wide range of SD card types and all of them are OK but not great. I mean, FR_DISK_ERR is returned a lot in during of working but our device tries to get FR_OK.

Unfortunately, we always receive FR_DISK_ERR in some SD cards while, it worked all the time in our STDLibrary version.

Furthermore, we've found if "f_mount" function was called once and after that you take away the SD card and put it again, it will never work until you reset your microcontroller.

My microcontroller is STM32F427VI and SDIO configured as same as this:

 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 = 14;

and it's working with 4bits wide bus.

Also, my device's clock is 96MHz and "APB2 Peripheral Clock" is 48MHz.

Edited (2020/02/24):

Finally, it worked when I updated my HAL Library to STM32Cube_FW_F4_V1.24.2. But HAL still doesn't work as well as Standard Peripheral. For example, I can't set 'ClockDiv' to '0' (24MHz) yet. It doesn't work at all. Now I set 'ClockDiv' to '1' (16MHz) that's not good enough for my project but I have to. Or if you take SDCard away and insert it again when program is working you can not init FATFS with f_mount. It's not going to work at all. You have to init SDIO Peripheral again by yourself. Unfortunately, now I can't put any time for getting more detail about what's going on in my schedule. Maybe in future.

This topic has been closed for replies.

18 replies

dbgarasiya
Senior
January 11, 2020

your sdio initialization is successfull or not?

dbgarasiya
Senior
January 11, 2020

may be sd card not iniilization succesfully

Ali Esmailpor
Associate II
January 12, 2020

Thank you.

As for re-inserting the card - I did initial again by recalling f_mount when FR_DISK_ERR was returned by f_open. I did it until giving FR_OK but it's never returned FR_OK in this case. I realized f_mount doesn't initial SDIO for second time.

Because there's a flag that doesn't let 'SD_initialize' be recall again (SD_initialize function includes BSP_SD_Init).

here is the diskio.c's code:

DSTATUS disk_initialize (

BYTE pdrv /* Physical drive nmuber to identify the drive */

)

{

 DSTATUS stat = RES_OK;

 if(disk.is_initialized[pdrv] == 0)

 {

  disk.is_initialized[pdrv] = 1;

  stat = disk.drv[pdrv]->disk_initialize(disk.lun[pdrv]);

 }

 return stat;

}

Now I used SD_PowerON and SD_InitCard before recalling f_mount and it worked correctly now.

It's a bug, isn't it? :sad_but_relieved_face:

But other problems have still remained. It doesn't work with 24MHz clock at all and FR_DISK_ERR is still returned by some SD cards.

dbgarasiya
Senior
January 13, 2020

you just have to consider below steps:

1.Link sd driver using this funtion : uint8_t FATFS_LinkDriver(const Diskio_drvTypeDef *drv, char *path)

2.Initialize sd card ( clock ,gpio, bus width ,checking of sd card detectd or not) : BSP_SD_Init()

3.mount 

FRESULT f_mount (

FATFS* fs, /* Pointer to the file system object (NULL:unmount)*/

const TCHAR* path, /* Logical drive number to be mounted/unmounted */

BYTE opt /* Mode option 0:Do not mount (delayed mount), 1:Mount immediately */

)

4.open

FRESULT f_open (

FIL* fp, /* Pointer to the blank file object */

const TCHAR* path, /* Pointer to the file name */

BYTE mode /* Access mode and file open mode flags */

)

5.write 

FRESULT f_write (

FIL* fp, /* Pointer to the file object */

const void* buff, /* Pointer to the data to be written */

UINT btw, /* Number of bytes to write */

UINT* bw /* Pointer to number of bytes written */

)

Best of luck

Ali Esmailpor
Associate II
January 13, 2020

Thank you.

As for re-inserting the card - it worked with adding SD_PowerON and SD_InitCard before f_mount.

But other problems have still remained. It doesn't work with 24MHz clock at all and FR_DISK_ERR is still returned by some SD cards not all of them.

Explorer
January 11, 2024

I had a same problem when I re-inserting the card and you have the MASTER SOLUTION THX! 
I used SD_PowerOn() and SD_disk_initialize(0) (as a generic number) thx

dbgarasiya
Senior
January 13, 2020

use 48 MHz clock

Ali Esmailpor
Associate II
January 13, 2020

If I set up clock to 48MHz it will never work even in the other SD cards what were OK.

dbgarasiya
Senior
January 13, 2020

i am attching you my project for sd card

which is for STM32F779BI controller

you will have to port it for your controller

Ali Esmailpor
Associate II
January 13, 2020

Thank you so much. I'll check it and let you know.

dbgarasiya
Senior
January 13, 2020

All ther best

dbgarasiya
Senior
January 16, 2020

this project is for stm32f779bi , you have to port it for your controller ,it just help you as refrence

if not check hardware connections

all pins getting enable or not? means proper supply

best of luck

Ali Esmailpor
Associate II
January 16, 2020

Yes I did change it to F4 series configuration.

All of pins are enabled and well supplied.

I think these are not my problems.

I have to remind it that my device doesn't work when I set up the clock of SDIO to 24MHz as the highest clock possible.

I'm doing some test and after that I'm going to let you know.

Thank you, pal. ;)

dbgarasiya
Senior
January 18, 2020

Have you solved out problem?

Ali Esmailpor
Associate II
January 19, 2020

Not yet. I'm working on it.

dbgarasiya
Senior
January 20, 2020

which controller you are using , ? study double check datasheet , or refrence manual related to clock

i think you are ok reated to firmware