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.
[/quote]

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.

Hi htibosch,

I’m pretty new to this topic. But may I ask how can I create driver for SAMD21?

You are asking for a FreeRTOS+FAT driver for the SAMD21, I assume?

I haven’t heard that there is one, so you’d probably have to develop it yourself.

I would recommend look at the existing driver for ATSAM4E.

There is also an explanation here about “Creating a FreeRTOS-Plus-FAT Media Driver”.

And you can always ask questions here. If you decide to create a new driver, please open a new subject to discuss things.

I am exploring FreeRTOS + SDCard support and nobody mention how IO wait is implemented in each library. Will it hang waiting SD card respond or will it switch to different task?

I am trying to reinvent the wheel and make my own flight controller and I need a SD card for blackbox. I try to not copy code from existing project.

That depends on the block device driver, or " Media Driver". Generally, they will wait using something like xTaskNotifyWait(), xSemaphoreTake(), or xQueueReceive(), so other tasks can run. For example, see spi.c.