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.
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.
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.
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.
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.
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,
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.
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.