ntaxyz wrote on Saturday, March 18, 2017:
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 “FR_DISK_ERR”, when try to open file.
could you please guide me why this code not work more than one time?!
FATFS SDFatFs;
bool WriteToSDCard(uint8_t* FileName,uint8_t* Data,uint32_t Seek,uint32_t ByteToWrite)
{
while((osMutexWait(SDMutexHandle,0)!=osOK))
{
Debugger_Send_Data("-MSWL-");
osDelay(500);
}
FIL MyFile;
uint32_t byteswritten;
char SDPath[4];
bool FinalStatus=false;
while(!FinalStatus)
{
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)
{
if(f_open(&MyFile,(const TCHAR*) FileName, FA_OPEN_ALWAYS | FA_WRITE | FA_READ) == FR_OK)
{
if(f_lseek(&MyFile, Seek)==FR_OK)
{
if(f_write(&MyFile, Data, ByteToWrite, (void *)&byteswritten) == FR_OK)
{
FinalStatus= true;
}
}
}
}
f_close(&MyFile);
f_mount(NULL,(TCHAR const*)SDPath, 0);
}
osMutexRelease(SDMutexHandle);
return FinalStatus;
}
bool ReadFromSDCard(uint8_t* FileName,uint8_t* Data,uint32_t Seek,uint32_t ByteToRead)
{
while((osMutexWait(SDMutexHandle,0)!=osOK))
{
Debugger_Send_Data("-MSRL-");
osDelay(500);
}
FIL MyFile;
uint32_t bytesReed;
char SDPath[4];
bool FinalStatus=false;
while(!FinalStatus)
{
memset(Data,0,ByteToRead);
if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)
{
if(f_open(&MyFile,(const TCHAR*) FileName, FA_OPEN_ALWAYS | FA_WRITE | FA_READ) == FR_OK)
{
if(f_lseek(&MyFile, Seek)==FR_OK)
{
if(f_read(&MyFile, Data, ByteToRead, (void *)&bytesReed) == FR_OK)
{
FinalStatus= true;
}
}
}
}
f_close(&MyFile);
f_mount(NULL,(TCHAR const*)SDPath, 0);
}
osMutexRelease(SDMutexHandle);
return FinalStatus;
}