Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT




Loading

use fatfs on freertos

Posted by ntaxyz on 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 "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

Posted by rtel on March 18, 2017

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.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS