How can I write and read a SD Card using FreeRTOS?

Anyone have a example code to show me how to do that using FreeRTOS? I tried with the example available in arduino ide, but it doesn’t work. It shows “Card mount failed”, and i saw that is a problem of a lot of people.

Thank you.

This is a very generic question - and it sounds like you have a potential solution already albeit one that requires a bit of investigation work.

There are lots of different file system options for FreeRTOS available - including those from HCC Embedded and Datalight (both of which are now part of Tuxera), Segger, Arm littleFS, etc. If FAT is good enough for your application then you can also try FreeRTOS+FAT.

[EDIT]Forgot to mention the obvious FatFS too. A lot of people use that with FreeRTOS.[/EDIT]

I’ve just been playing with littlefs. It seemed appealing to me because of its resilience to power failure. Unfortunately, it does not seem to scale well to the size of an SD card. Apparently, it slows down quadratically with the amount of data on the card. It is way too slow for the application I had in mind.

Another one that I keep looking at is RFAT; A Robust FAT implementation, but I have never tried it.

FreeRTOS+FAT is the obvious choice, but one limitation is that it doesn’t support exFAT. These days, that means that many (most?) SD cards won’t work out of the box; you’ll have to reformat them first.

These days, that means that many (most?) SD cards won’t work out of the box; you’ll have to reformat them first.

FreeRTOS+FAT provides functions to partition a disk, and format it to either FAT16 or FAT32.

Currently, it has drivers for these platforms.

FreeRTOS+FAT handles errors in a clear and transparent way: a 32-bit variable contains a code for the module, the function and the type of error that occurred. See ff_error.h.

Also it introduces a kind of errno, that is stored in the task local buffers.

It’s unfortunate that exFAT is encumbered by patent. Lack of exFAT support prevents (or makes difficult) some use cases, like an application that would like to be able to read SD cards written on some other device (e.g., a PC running Windows).

My experience is that you will pay dearly for a) non industrial sd cards and b) most open source/ non commercial fs middleware.

The big problem with the second is that a file system shows its robustness only under stress conditions, eg when the fs operates on an almost full medium with frequent reads/ writes for an extended time. Only middleware that is well regress tested is useful in unsupervised devices.

I just wanted to mention that while FatFS has some support for “Re-entrancy” or thread safety, it is limited to operations such as:

  • f_read
  • f_write
  • f_sync
  • f_close
  • f_lseek
  • f_closedir
  • f_readdir
  • f_truncate
    There does not appear to be sufficient FAT and directory locking to make operations like f_mkdir, f_chdir and f_getcwd thread safe.

I ported the old +FAT vMultiTaskStdioWithCWDTest to FatFS and it fails miserably unless fsTASKS_TO_CREATE == 1.