2024-07-30 08:31 AM - edited 2024-08-08 07:12 AM
Hello,
I am trying to take a log using my sdcard on my project.I am using FATFS that included in STM32CubeIDE but I got some issues, without FREERTOS everything works as expected and fine but when I enable the FREERTOS and it want's me to enable USE_NEWLIN_REENTRANT.
When I enable it generates problem with FATFS USE_LFN(long file name) selection and normally it is Enable with dynamic working buffer on BSS. But the IDE wants me to change it and I change it for the STACK than I tried the HEAP and also tried for the disable option. But in both way my system got stuck in f_open function and returns FR_NOT_READY I searched online but I couldn't find the exact solution for this problem. When I searched I found examples for DMA option but I don't have the dma option in .ioc file you can see it in attachements.
I am trying to initialize my sdcard in main code and my function looks like this
FRESULT initialize_sd_card(void)
{
FRESULT res; /* FatFs function common result code */
FIL SDFile; /* File object */
// Get the current date from the RTC
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); // Optional if you need time as well
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
// Format the file name with the date and time
snprintf(filename, sizeof(filename), "%02d-%02d-%04d %02d.%02d.txt",
sDate.Month, sDate.Date, 2000 + sDate.Year,
sTime.Hours, sTime.Minutes);
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
return FR_NOT_READY;
}
printf("Mounted successfully.\n\r");
res = f_open(&SDFile, filename, FA_OPEN_ALWAYS | FA_WRITE);
if(res != FR_OK)
{
return res;
}
if (f_close(&SDFile) != FR_OK)
{
return FR_INT_ERR;
}
printf("SD Card initialized.\n\r");
return FR_OK;
}
In my initalization I am opening and closing the my document otherwise my write function does not working I couldn't understan it also. because I don't want to mount and unmount everytime that I use. I am using RTC for taking log with 10 seconds peroid. This is my write function.
FRESULT write_to_sdcard(uint8_t *wtext)
{
FRESULT res; /* FatFs function common result code */
UINT byteswritten; /* File write count */
FIL SDFile; /* File object */
res = f_open(&SDFile, filename, FA_OPEN_ALWAYS | FA_WRITE);
if(res != FR_OK)
{
f_mount(NULL, "", 0); // Unmount the file system
return res;
}
res = f_lseek(&SDFile, f_size(&SDFile));
if(res != FR_OK)
{
f_close(&SDFile);
f_mount(NULL, "", 0); // Unmount the file system
return res;
}
res = f_write(&SDFile, wtext, strlen((char *)wtext), &byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
f_close(&SDFile);
f_mount(NULL, "", 0); // Unmount the file system
return res;
}
if (f_close(&SDFile) != FR_OK)
{
f_mount(NULL, "", 0); // Unmount the file system
return FR_INT_ERR;
}
return FR_OK;
}
Can anyone help me with these issues:
1. How can I work with RTOS and FATFS at the same time what is causing that FR_NOT_READY issue for f_open function?
2. Why do I need to open and close file in itialization. Why not just mount isn't enough?
Thank you for your helps.
2024-08-28 06:14 AM
Hello @Aurentiaco, sorry for the delayed answer,
FATFS is not inherently thread-safe. When using it in a multi-threaded environment like FreeRTOS, you need to ensure that the file system operations are protected from concurrent access. You can do that by enabling the reentrant option and using mutexes.
Enabling USE_NEWLIN_REENTRANT is necessary for thread safety
Also, make sure that the file system is mounted only once
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.