FATFS on FRDM-K64F with FreeRTOS

cancel
Showing results for 
Search instead for 
Did you mean: 

FATFS on FRDM-K64F with FreeRTOS

1,473 Views
thomasolenik
Contributor III

Howdy all! 

 

I have a project running FreeRTOS and I've used a customized version of the sdcard_fatfs driver demo on the FRDM-K64F. Both work well independently. I am trying to bring them together, and running into issues I thought I would share with the community. There may be a better way.

 

I started with my FreeRTOS project which is using SDK 2.0 drivers. It is simple and has just three tasks so far including the "Hello World" task that is generated when creating the project. The others blink an LED and perform CAN communications once per second with an external CAN transceiver. It works perfectly on its own.

 

In order to write data to the mircoSD card on the FRDM-K64F I imported files from the sdcard_fatfs demo which I had working correctly independently as well. Maybe there is a better way.

 

Well, they don't work together. I have traced the problem to the event.c file that is included in the sdcard_fatfs demo. This file is redefining a new SysTick_Handler. Here is what happens.

  • If I comment out all code in my main.c associate with the fatfs and comment out only the SysTick_Handler in event.c, then everything else works with the FreeRTOS application.
  • If I then uncomment SysTick_Handler each task runs once and stops. 
  • If I then uncomment the fatfs code, the fatfs code runs, but then FreeRTOS tasks all run just once then stop.
  • If I comment out the SysTick_Handler again, the fatfs code goes into hard fault.

 

So my task now is to rework this so that the fatfs and freeRTOS code are using the same system tick handler. What I don't know yet is where/how to find the sections of code in the fatfs files to change and what to change them to. Perhaps there is another approach that would be better as well. I will post again if I figure it out, but in the mean time, maybe someone has a solution to share with the community. I think I have an idea already, but I'm done writing. So might as well share.

 

I'm using Kinetis Design Studio 3.2.0.

 

Tom Olenik

Labels (1)
0 Kudos
7 Replies

416 Views
peterfurey
Contributor IV

Thank you for posting this question Thomas. I ran into this same problem although I'm using a custom board with SDK_2.0_MK24FN1M0xxx12. I've been wrestling with this for a few days now with no success. I'll let you know when I get this figured out and will also keep tabs on this thread.

0 Kudos

416 Views
peterfurey
Contributor IV

Hi again, My code appears to work fine after I comment out  EVENT_InitTimer() and SysTick_Handler() functions in event.c. The SysTick_Handler() configured through FreeRTOS seems to work fine with my sdcard_fatfs modules (using default FreeRTOSConfig.h). Doubt this gives you any new information but will share my code if you think it might help.

0 Kudos

416 Views
raymondchin
Contributor I

Hi Peter,

Can you please share your code?  I am running problems integrating FATFS with FreeRTOS.

0 Kudos

416 Views
thomasolenik
Contributor III

Update 2. If I open then close the file that I'm writing to on each execution of the logging task, the program runs and the xTickCounter increments, but my CAN communication task does not execute after the first time. I'm now thinking that maybe the fatfs and CAN code are clashing somewhere. Again, if I comment out the function calls to work with the fatfs, the CAN works fine.

0 Kudos

416 Views
BlackNight
NXP Employee
NXP Employee

Hi Thomas,

I don't know why in your case FatFS would use the SysTick. FatFs as I know it does not depend or use the SysTick. It could be that you use the SysTick as part of the SDK as 'mini scheduler'? As Richard wrote, FreeRTOS should be the one (and only one) using the SysTick (or should us its tick counter, whatever it is, like the LPTMR).

There is one important thing to check: that the interrupt priorities are set correctly, see https://mcuoneclipse.com/2016/08/28/arm-cortex-m-interrupts-and-freertos-part-3/ 

I hope this helps,

Erich

0 Kudos

416 Views
FreeRTOS_org
Contributor IV

Out of the box, by default (unless you explicitly override it, which is simple to do), FreeRTOS needs exclusive access to the SysTick peripheral and associated interrupt service routine.  If the FatFS code is wanting to just use a tick count, then it is possible to update the FatFS code to use the xTaskGetTickCount() function to get the RTOSs tick value.  If the FatFS code wants to perform processing in the SysTick interrupt then it is possible to add this processing into the tick hook function.  What the FatFS code must NOT do is meddle with (start, stop, or change the period of) the SysTick peripheral itself.  So look for anywhere that the FatFS code that might be touching the SysTick peripheral (not really a peripheral, but part of the Cortex-M core) and update that as appropriate.

0 Kudos

416 Views
thomasolenik
Contributor III

Update 1. I did confirm the issue is there is something in the fatfs code that is killing the xTickCount. 

I was able to edit code in the event.c file to use xTaskGetTickCount() instead of it's own tick counter. That got me to the point where I could create a logging task to write to the sd card. However, after writing to the sd card the first time, the xTickCounter stops updating. I confirmed that data did successfully write to the card once, but something about calling those functions crashes the tick counter or scheduler. When I watch the xTickCount variable in debug, it reads 0 after the crash. The system seems to be in the HardFault_Handler when this happens. If I comment out the section of the logging task that writes to the card, everything runs fine, and the xTickCount variable increments. The only functions called in that section of code are f_printf() and f_sync(). I've confirmed that both execute and return without error, and the data is on the sd card after.

0 Kudos