cancel
Showing results for 
Search instead for 
Did you mean: 

[HAL SD driver] Possible Bug in FATFS_LinkDriverEx?

valentin
Senior
Posted on July 12, 2017 at 04:50

Hi,

following scenario:

I use one SD card on my pcb (4 wire sdio) with fatfs and FreeRTOS.

The generated driver init function from ST looks like following (ff_gen_drv.c):

uint8_t FATFS_LinkDriverEx(Diskio_drvTypeDef *drv, char *path, uint8_t lun)
{
 uint8_t ret = 1;
 uint8_t DiskNum = 0;
 
 if(disk.nbr <= _VOLUMES)
 {
 disk.is_initialized[disk.nbr] = 0;
 disk.drv[disk.nbr] = drv; 
 disk.lun[disk.nbr] = lun; 
 DiskNum = disk.nbr++;
 path[0] = DiskNum + '0';
 path[1] = ':';
 path[2] = '/';
 path[3] = 0;
 ret = 0;
 }
 
 return ret;
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Situation:

I have an exti interrupt on the SD_Detect pin of the socket to dynamically (de-)init the driver whenever the SD card is (un-)plugged.

On unplug, I call:

uint8_t SD_Deinit(void) {
uint8_t retval = FAT_unmount_SD();
return retval | FATFS_UnLinkDriver((const TCHAR*) SD_Path);
}�?�?�?�?

On plugging in, I call (HAL_SDIO_PowerOff/On controls power supply pin pmos of SD socket):

HAL_SDIO_powerOff();
HAL_Delay(200);
HAL_SDIO_powerOn();
HAL_Delay(50);
MX_FATFS_Init(); /* reinit SD card driver as SD card has just been plugged back in */�?�?�?�?�?

Now, what happens is that if I plug the card in and out fast enough, FATFS_LinkDriverEx gets called while the driver is still initialized. This should fail and do nothing due to:

if(disk.nbr <= _VOLUMES)�?

BUT it doesn't because of the <= !! Instead, that must be replace by

if(disk.nbr < _VOLUMES)�?

(as _VOLUMES = 1 in my case).

With that change, the whole procedure works as expected and the code also makes sense. disk.nbr gets incremented by 1 during the init function, after all.

Any optinions?

1 ACCEPTED SOLUTION

Accepted Solutions
Imen.D
ST Employee
Posted on July 12, 2017 at 11:30

Hello

Roehrig.Valentin

,

You are right,

Array check in FATFS_LinkDriverEx in 'ff_gen_drv.c' is wrong:

The array[_VOLUMES] index test is:

if(disk.nbr <

=

_VOLUMES) ,

when it should be:

if(disk.nbr < _VOLUMES)

We have already the limitation in our bugs list, and it will be fixed in coming version.

Thanks for the report.

With Regards

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

View solution in original post

2 REPLIES 2
Imen.D
ST Employee
Posted on July 12, 2017 at 11:30

Hello

Roehrig.Valentin

,

You are right,

Array check in FATFS_LinkDriverEx in 'ff_gen_drv.c' is wrong:

The array[_VOLUMES] index test is:

if(disk.nbr <

=

_VOLUMES) ,

when it should be:

if(disk.nbr < _VOLUMES)

We have already the limitation in our bugs list, and it will be fixed in coming version.

Thanks for the report.

With Regards

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on July 12, 2017 at 21:46

Great, thanks for confirmation!