fatfs sd card file system check and format

Dear ,

With nRF connect SDK, is there a way to check if the SD card is formatted with the correct file system, i.e. FATFS? If it's not in FATFS, how can we format it via the program (NOT from PC via USB)? I can see relevant posts using nRF52 SDK but not nRF connect SDK.

Basically what we are trying to achieve here are:

- Check SD card file system after power-up; and

- Format the SD card if it's not in FATFS.

Thanks a lot.

Kind regards, Kevin

  • Hi, this is default behavior unless the FS_MOUNT_FLAG_NO_FORMAT or FS_MOUNT_FLAG_READ_ONLY flags are set. The SD card will be formatted with FAT if the filesystem is not recognized, and the two flags are not set. If fs_mount is not able to mount the existing file system or format a new file system -ENODEV will be returned.

    What is the size of the SD card you are using? Maybe CONFIG_FS_FATFS_EXFAT needs to be set?

  • Dear Stian,

    I am planning to use an SD card with a capacity of 4-8 Gbyte. I was able to complete the sample with a few configurations modified. 

    But I just want to confirm that if the SD card is not initially formatted in the FAT file system, instead of reformatting the SD card via a PC, can this reformatting process be achieved by the program itself, e.g. by calling fs_mount or other relevant functions?

    As to my application, the device will be collecting data and storing to the file on the SD card. Once it's done, the data file can be accessed by directly connecting the device to a PC via a USB port.

    However, there is a risk that the user accidentally formats the device to any other file system rather than FAT when they are accessing the data using a PC, which could cause issues for its later use. 

    So when disconnected from the USB and power-up again, what is the function I could use to check if it's a FAT file system; and if it's not, which functions I can call to reformat the SD card?

    I found a few posts and tutorials showing how it can be achieved in nRF52 SDK, but can't see similar examples using nRF connected SDK.

    Would you mind kindly advising?

    Thanks again.

    Kind regards, Kevin

  • Hi, sorry for the late reply. What I meant in my first answer is that the use case you are describing is already implemented when mounting the card through fs_mount. fs_mount checks if the specified file system is present, and if not it reformats the card. If you want to do this in two steps; first check if it's a FAT filesystem, and then make the decision if you want to format or not, you can use fs_mount with the FS_MOUNT_FLAG_NO_FORMAT first (it will return an error if the file system is not present), then run fs_mount again without the flag (it will reformat the drive and hopefully not return any error).

    Let me know if I misunderstood, or the proposed solution doesn't work for you.

    Best regards,
    Stian

Related