Write several files "simultaneously" on SD card with freeRTOS fails.
Hello,
I built a test app that writes data in files on SD cards in a freeRTOS enviromnent in order to test
Cube (and cubeMx) librairies 'integration.
I wrote a task that write data on one file, then another and so on, the file prefix is the (data) of the task:
void taskCreateFiles(void const* data) {
for (uint32_t i = 0; i < 256; i++) {
char i_[10];
itoa(i, i_, 10);
char filename[255];
strcpy(filename, (char*) data);
strcat(filename, i_);
strcat(filename, ".txt");
newFile(filename); // creates a file a add some data in it by calling f_write several times
RED_LED_TOGGLE;
}
while (1)
osDelay(1000);
}The task is instantiated twice with a different prefix.
A third task is a simple blinking led.
FatFS is defined as FS_REENTRANT.
It works until... it stops working because of invalid return values in f_write or f_close or f_open.
As cubeMx generates a sd_disk_io.c file "based on sd_diskio_dma_rtos_template.c v2.0.2 as FreeRTOS is enabled" (in the text) , I thought my app should work "as it".
Am I wrong to think like this? Did I miss something in my implementation?
To overcome my issue, I see several choicies :
- implement a single SD task with a queue which will be feed with a struct containing an operation (read/write/...) and relevant data ( nothing / fileId and data / ...). Feedback may be provided by a messaging system.
- protecting BSP read/write/erase functions with a mutex.
- protecting sd_diskio.c functions with a mutex.
Solution 1 might be a bit heavy to code.
Solution 2 might not work if a high level operation needs several access to SD module to complete
Solution 3 not really cool because there is no user tag to write data in source code. It will be erased by CubeMx.
What do you think about it?
Thanks
Julien
