use fatfs on freertos

Hi. I would like to use fatfs on freertos (stm32f407) so I write this 2 functions for read and write with mutex. but, it just work first time and after that I got “FRDISKERR”, when try to open file. could you please guide me why this code not work more than one time?! ~~~ FATFS SDFatFs; bool WriteToSDCard(uint8t* FileName,uint8t* Data,uint32t Seek,uint32t ByteToWrite) {
while((osMutexWait(SDMutexHandle,0)!=osOK)) { DebuggerSendData(“-MSWL-“); osDelay(500); } FIL MyFile;
uint32_t byteswritten;
char SDPath[4]; bool FinalStatus=false; while(!FinalStatus) {
if(fmount(&SDFatFs, (TCHAR const*)SDPath, 0) == FROK) { if(fopen(&MyFile,(const TCHAR*) FileName, FAOPENALWAYS | FAWRITE | FAREAD) == FROK) { if(flseek(&MyFile, Seek)==FROK) { if(fwrite(&MyFile, Data, ByteToWrite, (void *)&byteswritten) == FROK) { FinalStatus= true; } } } }
fclose(&MyFile); fmount(NULL,(TCHAR const*)SDPath, 0); } osMutexRelease(SDMutexHandle); return FinalStatus; } bool ReadFromSDCard(uint8t* FileName,uint8t* Data,uint32t Seek,uint32t ByteToRead) {
while((osMutexWait(SDMutexHandle,0)!=osOK)) { DebuggerSendData(“-MSRL-“); osDelay(500); } FIL MyFile;
uint32_t bytesReed;
char SDPath[4]; bool FinalStatus=false; while(!FinalStatus) { memset(Data,0,ByteToRead); if(fmount(&SDFatFs, (TCHAR const*)SDPath, 0) == FROK) { if(fopen(&MyFile,(const TCHAR*) FileName, FAOPENALWAYS | FAWRITE | FAREAD) == FROK) { if(flseek(&MyFile, Seek)==FROK) { if(fread(&MyFile, Data, ByteToRead, (void *)&bytesReed) == FROK) { FinalStatus= true; } } } } fclose(&MyFile); fmount(NULL,(TCHAR const*)SDPath, 0); } osMutexRelease(SDMutexHandle); return FinalStatus; } ~~~

use fatfs on freertos

FatFS support is out of scope for this forum, although there are some examples that use FatFS and demonstrate how to set up FatFS for use in a multi-threaded environment. As I recall there is a configuration setting that crudely wraps all the FatFS functions inside macros that take a mutex at the start of the function and return the mutex at the end of the function.
osMutexWait(SDMutexHandle,0)
I’m afraid we can only support use of the native FreeRTOS API, and not third party abstractions which are unknown to us.
osDelay(500);
I’m not sure why you are using a block time of zero when attempting to take the Mutex, then a separate 500 tick delay if the mutex could not be obtained. If you instead use a block time when attempting to obtain the mutex then you are guaranteed to have the highest priority waiting task obtain the mutex as soon as the mutex becomes available.