AnsweredAssumed Answered

[HAL SD driver] Possible Bug in FATFS_LinkDriverEx?

Question asked by Valentin on Jul 12, 2017
Latest reply on Jul 12, 2017 by Valentin



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;



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):

                    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?