1

I am using a SD Card Module that has no pin for checking existence of SD Card, So i should do the check using fatfs libs. The micro-controller is STM32F103RB and i'am using SPI protocol to communicate with SD Card. Up to now i can detect a removal state with this code:

bool check_SdCard()
{
    FATFS check_fatfs;

    if(f_mount(&check_fatfs,"",1)==FR_OK)
    {
        f_mount(NULL,"",1);
        return true;
    }
    else return false;
}

The problem is that i can't check insertion of SD Card (or i cant re init the SD Card properly) to Read/Write to it after removing it from a previous initialization.

the above code returns true if the SD Card is inserted to the module from first startup of the device but always returns false if SD Card removed/reinserted after startup. so f_mount could not reinitialize SD Card after reinsertion. I've also tried

disk_initialize(0)
with no success. Does anyone know how to solve the problem of re initialization of SD Card using fatfs?
  • Try to unmount the card first before checking for re-insertion. I.e., unmount it once you detect it has been removed (r/w error or something). – JimmyB Jan 28 '19 at 09:27
  • According to f_mount documentation in this [link](http://elm-chan.org/fsw/ff/doc/mount.html), a `f_mount(NULL,"",0);` or `f_mount(0,"",0);` will do an unmount. I've also tried this but ended with the same result that the code in question has. @JimmyB – sina jahromi Jan 28 '19 at 13:04

1 Answers1

0

This isn't exactly the same scenario as the question, but I haven't found a working solution to reinitializing a new card after one was removed and reinserted anywhere on the internet. I managed to figure out how to do it myself, so I figured I'd share it so anyone else searching can fix it.

I've found that when you detect that the card was removed, you need to first call FATFS_UnLinkDriver(SDPath); followed by MX_FATFS_Init(); before you can try to reinitialize the drive. The unlink call is needed to clean up and decrement a counter the initialization function checks. The initialization function clears the state on an internal struct indicating that the drive was already initialized. This'll make it reinitialize the drive next time you go and mount it.

This isn't entirely consistent, but it works most of the time. I'm sure I'm missing something else though.

RomanPort
  • 31
  • 3