Freescale has released the new Kinetis SDK v1.2 back in May. Because of the changes in v1.2, previous applications (or my tutorials) do not work any more :-(. So time to write a new tutorial: how I can use Processor Expert with FreeRTOS for the Kinetis SDK v1.2.
💡 The SDK v1.2 API is very different from the v1.1 version. And indeed it turns out that porting an SDK v1.1 application to SDK v2.0 is very hard. As for myself, I gave up on that task 😦
Hardware and Software Used
I’m using in this tutorial Kinetis Design Studio v3.0.0 with the Kinetis SDK v1.2. Make sure you have added the Processor Expert KSDK v1.2 update (C:\Freescale\KSDK_1.2.0\tools\eclipse_update\KSDK_1.2.0_Eclipse_Update.zip) with the menu Help > Install new Software. Additionally, I’m using my McuOnEclipse components from SourceForge (McuOnEclipse Components: 05-July-2015 Release).
As board I’m using the FRDM-K64F which has an ARM Cortex-M4F on it:
Creating Project
I create a new project in Kinetis Design Studio with the menu File > New > Kinetis Project:
Then I give a name for the project:
Then select the board/device:
💡 For non-SDK Processor Expert projects, there is a bug in KDS v3.0.0 if I select the board. After project creation, it will complain that the CPU is not selected or set. The workaround is to select the device, not the board.
In the next dialog I specify to use the SDK v1.2 and Processor Expert. With the ‘Standalone’ option I make sure that the SDK files get copied into my project. This will make sure that I have them locally.
Then select the GNU C Compiler, and press Finish:
And the project is created:
Kinetis SDK V1.2 fsl_os_abstraction
With the SDK v1.2 there is now a fsl_os_abstraction component:
At the first sight, that component looks nice. But I don’t like it, as in bare metal mode it kind of tries to mimic an RTOS with the SysTick timer. I do not need that kind of thing: If I’m going bare metal, then I want bare metal without that timer, so what I always do first: disable that timer :-):
💡 I wish I could get rid of that component completely as it is really not necessary for al my use cases. However, the SDK insists that it is there 😦
The added benefit of disabling that timer is that the code size gets reduced by 2922 bytes (!!!!!). Yet another reason to disable that timer :-).
Kinetis SDK V1.2 FreeRTOS Component
The benefit of that fsl_os_abstraction component would be to add easily the desired RTOS:
It then adds its own SDK FreeRTOS component to it:
And in the properties I can configure the RTOS:
This component additionally adds a compiler preprocessor define “FSL_RTOS_FREE_RTOS” to tell the SDK sources which RTOS is used:
What I like in that component is that it uses folders for the functions (can I do this with the Bean Wizard for my component too?) and automatically takes the clock speed from the SDK settings.
However, I have found several problems with that component:
- It does not support Percepio FreeRTOS+Trace (see “Updated Percepio Tracealyzer and Trace Library to Version V2.7.0“)
- The default values of the component are far too high (e.g. 64 task priorities, enabled software timers, …) which creates an unnecessary code bloat. However, depending on what SDK functions you need, you need to enable Timers.
- Tickless idle mode is not supported.
- Enabling runtime statistics is not supported.
- No choice of different stack overflow detection strategies.
- No setting to enable/disable RTOS asserts.
- Cannot turn on/off floating point stack framing.
- No setting for IDLE task resource cleanup.
- No Task Exit Error handler.
- No support for 16bit internal tick counter (ok, that’s less relevant for ARM with SysTick).
- No interrupt priority setting support: at which priority the kernel shall run, Max SysCall Interrupt priority is missing.
Last but not least, it is using FreeRTOS v8.0.0 instead of the more recent v8.2.x. So if you want to use that free_rtos SDK component, this is up to you. I recommend to use the McuOnEclipse component which is using FreeRTOS v8.2.0 or later 🙂
Using McuOnEclipse FreeRTOS Component with Freescale Kinetis SDK v1.2
To use the McuOnEclipse FreeRTOS port instead, have the OS set to ‘BareMetal’:
Add to the “FSL_RTOS_FREE_RTOS” compiler preprocessor defines so the SDK knows we are using FreeRTOS:
Then add the McuOnEclipse FreeRTOS component to the project. It will automatically add as well the Utility component:
Verify the settings for the ARM Microcontroller you are using. As I’m using the K64F, I set it to M4F and have floating point enabled, as I want to use hardware floating point in my application:
As this component can work with and without the Kinetis SDK, and in this tutorial I’m using the Kinetis SDK, enable that in the setting. Additionally have the special Kinetis SDK component added: that component ensures that all necessary typedef’s are available. As the SDK v1.2 now comes with the OSA functions generated, have that setting set to ‘no’:
With this, ready to generate Processor Expert code, and then do a build:
This should build without warnings or errors.
Adding Blinky Task
I already can run that program, and it will only run one task: the FreeRTOS Idle task. To make things a bit flashy, let’s add a Blinky task. Creating a FreeRTOS task is as easy as this:
if (FRTOS1_xTaskCreate( BlinkyTask, /* pointer to the task */ "Blinky", /* task name for kernel awareness debugging */ configMINIMAL_STACK_SIZE, /* task stack size */ (void*)NULL, /* optional task startup argument */ tskIDLE_PRIORITY, /* initial priority */ (xTaskHandle*)NULL /* optional task handle to create */ ) != pdPASS) { for(;;){} /* error! probably out of memory */ }
It creates a task (function BlinkyTask) with a name (“Blinky”), having the minimal stack size as configured in the component settings (which is 200, or 200x32bits = 800 bytes). It does not pass an initial task startup value (NULL), sets the task initial priority to taskIDLE_PRIORITY (which is 0, and the same priority as for the IDLE task. As I do not need a task handle created, I pass NULL as last parameter.
I have this code added to main() in main.c:
Next is to implement the task itself in an endless loop (never leave the task!):
static void BlinkyTask(void *pvParameters) { (void)pvParameters; /* parameter not used */ for(;;) { vTaskDelay(100/portTICK_RATE_MS); } }
I can add that to main.c too:
That ‘Blinky’ task is not blinking yet. What I need to add is the code to blink the LED on the board. On the FRDM-K64F the Red RGB LED is on pin PTB22.
I add the fsl_gpio from the SDK v1.2.0 repository to the project:
As I’m only using output pins, I disable the Input pins and assing PTB22 (LEDRGB_RED) as my output pin. As the LED is low active (cathode pin on the microcontroller pin), I use ‘1’ as initial Output Logic.
Because I have added/changed components, I should generate Processor Expert code again.
To toggle the LED in my code, I can use the assigned ‘Pin Name’ (LEDRGB_RED):
GPIO_DRV_TogglePinOutput(LEDRGB_RED);
A cool trick is to drag&drop the function name into the source:
With this, my tasks is extended with the LED toggling functionality:
static void BlinkyTask(void *pvParameters) { (void)pvParameters; /* parameter not used */ for(;;) { GPIO_DRV_TogglePinOutput(LEDRGB_RED); vTaskDelay(100/portTICK_RATE_MS); } }
And I have the red RGB LED blinking on the FRDM-K64F board 🙂
Software Timer used by SDK
Depending on what functionality you are using in the SDK, you might need to turn on FreeRTOS Software timers. This is the case if you add the fsl_i2c SDK driver in your application. This one depends on having software timers enabled in FreeRTOS, because it is using xEventGroupSetBitsFromISR().
The values depend on your needs:
- Priority should be ideally the highest priority in your system, or high enough that the software timer gets served often enough.
- Queue Length: 10 should be fine (this is what the SDK demos are using too).
- Stack Depth: The SDK demos use configMINIMAL_STACK_SIZE * 2, but in my experience 200 should be fine too. That might depend on your interrupt load/stack size for interrupts.
I recommend that you enable Timers in FreeRTOS only if you need it, because this will add one extra task to the system.
Summary
The new Kinetis SDK v1.2 comes with several changes, including a fsl_os_abstraction component. While the SDK v1.2 comes with its own FreeRTOS component, you might consider the McuOnEclipse FreeRTOS component as a replacement, as it comes with a newer RTOS (V8.2.x) and includes features like FreeRTOS+Trace and command line support, plus more advanced settings to configure the RTOS.
The project created in this tutorial is available on GitHub.
Happy SDKing 🙂
Sorry for newbie questions.
* What’s the difference between the Kinetsis SDK and the McuOnEclipse components. Are they effectively the same thing (just with different versions/features)?
* Do the FreeRTOS sources come with the SDK or Components — or must FreeRTOS be downloaded and added to the project separately?
I have a project I want to get up and running on a FRDM KL25Z board with an RS485 shield, using the latest FreeRTOS (v8.2.1). However I may want to port this to an STM32 board in the future (e.g. an Olimex STM32-H407 or STM32-E407) — one reason being some of the FreeRTOS middleware (+TCP, +FAT, +CLI) have a special free commercial license for some devices (STM32, LPC17xx, LPC18xx, SAM3, SAM4).
Can the components be used with other vendor devices? e.g. STM32
I’m just wondering how easy it would be to port code that uses the SDK/Components, or whether it would be better/easier to use another approach.
Thanks, Brendan.
LikeLike
Hi Brendan,
no need to say sorry: we are all learning new things.
>* What’s the difference between the Kinetsis SDK and the McuOnEclipse components. Are they effectively the same thing (just with different versions/features)?
The Kinetis SDK is a set of low level and high level drivers for Kinetis, and Kinetis only. The drivers can be configured with Processor Expert.
The McuOnEclipse components are a set of Processor Expert components, for Processor Expert LDD (Logical Device Drivers) and ‘Beans’, and cover S08, S12(X), ColdFire, DSC and Kinetis. Some components work as well with the Kinetis SDK, but not all of them.
>* Do the FreeRTOS sources come with the SDK or Components — or must FreeRTOS be downloaded and added to the project separately?
FreeRTOS (from freertos.org) does not come with the SDK or any components (except what I have published on FreeRTOS as user contributions). If you download and install the SDK, then it comes with a port (v8.0.0) of FreeRTOS. That SDK port actually has been generated by my McuOnEclipse FreeRTOS component (you can generate source code and use it otherwise, that’s a great thing with using Processor Expert :-)).
Nothing prevents you to use the FreeRTOS sources and to use it for any other device (it is a very permissible open source license).
>Can the components be used with other vendor devices? e.g. STM32.
No, Processor Expert only exists for Freescale devices (and I guess this will be extended to support NXP devices too?). But you could use the generated code, at least from most of the McuOnEclipse components, depending on the licensing terms of each component. I cannot give you legal advice (of course!), so I recommend that you read the SDK license agreement if you want to use the SDK for other devices than Freescale devices.
I hope this helps,
Erich
LikeLike
Hi Erich,
Thanks gods for your and your blog existance!!! Really helpfull.
You arespend a lot of time reviewing FREERtos may be last half a year. What about MQXlite? Do you prefer FREERTOS much more? Is there any comparison about that two RTOSes?
I am starting new project at a time and am choosing rtos for that. Lwevents seems nice to me and can’t see any simmilar things in the FREERtos.
Best regards,
Alexey,
LikeLike
Hi Alexey,
I have not used MQXlite in my production projects, so I cannot really give you a good advice. And probably I’m biased to FreeRTOS anyway. Compared to the full MQX, in my view MQXlite really makes sense, and is much closer to FreeRTOS than MQX from a footprint point of view. I think MQXlite makes sense if you are used to the MQX API, but want to use it on a smaller device. Or if you want to start with MQXlite and then move up to the ‘full’ MQX. Still, compared to FreeRTOS (in my view) MQXlite is more complex and harder to learn than FreeRTOS. And unlike FreeRTOS, MQX and MQXlite are not open source. As for LWEvents (Light Weight Events): FreeRTOS has this with the really neat and efficient Task Notification mechanism which has been introduced with V8.0.0. At the end, you will need to make an informed decision what you want to use. So keep asking around, and poke the forums with your questions too 🙂
LikeLike
Hi again Erich:)
Thanks a lot for detailed response. I have not known about Task Notification mechanism-have to read about it. For me as a new member to embeded OSes FreeRTOS looks a bit simplier too.
Have a good day!
LikeLike
Hi Erich,
Thanks very much for this tutorial, really useful! Just a quick observation, it might be good to make xSemaphoreCreateBinary a function in the PE FreeRTOS component since vSemaphoreCreateBinary is deprecated (http://www.freertos.org/a00121.html) and also it starts the Semaphore in a state that can be ‘taken’ which is a bit of a gotcha if you don’t realize.
LikeLike
Also heap_4.c has a typo on line 115
static uint8_t ucHeap[ configfTOTAL_HEAP_SIZE ];
should read
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
I also found that when using a c++ compiler i need to mark vTaskSwitchContext __attribute__((used)) or it gets removed even on -O0
LikeLike
Hi Tom,
thanks for reporting! About the ucHeap macro: this had been fixed with the release from 5-July-2015 (https://mcuoneclipse.com/2015/07/05/mcuoneclipse-components-05-july-2015-release/). Are you using this one or are you still on the previous one?
LikeLike
Hi Tom,
strange, in my projects (even with C++ enabled) I have not seen vTaskSwitchContext() being removed. What version of GNU are you using? I’m happy to add __attribute__((used)), but would be good to know why or when this happens.
LikeLike
Hi Tom,
in any case, I have made that change: https://github.com/ErichStyger/McuOnEclipse_PEx/commit/670a3587cd15af9bf192064be9e4a08c4deebace
Erich
LikeLike
Hi Eric,
Tanks for letting me know about ucHeap. Shamefully i found this workspace was pointing to an older version of those files, so pointing it towards the newer versions fixed this.
The vTaskSwitchContext error however remained. It was happening for me with standard c++11, abi >= 5 (g++ 4.6). I’d also like to know why its optimized out as im definitely context switching 😉 but obviously the fix isnt too painful, thanks very much for adding it.
LikeLike
Hi Tom,
thanks for catching that one! I have made the changes and depreciated vSemaphoreCreateBinary() and added xSemaphoreCreateBinary(), see https://github.com/ErichStyger/McuOnEclipse_PEx/commit/b1e3a49b64bf165b625b12e37849ba7f67f883fb
LikeLike
Hi Eric !
When you add the component KSDK 1.2.0 fsl_dspi, the linker error “undefined reference to `xTimerPendFunctionCallFromISR’ “
LikeLike
xTimerPendFunctionCallFromISR() is defined in file timers.c (it is part of the RTOS). Do you have that function?
You might need to turn it on in FreeRTOSConfig.h, and set the following defines:
/* Software timer definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY 2
#define configTIMER_QUEUE_LENGTH 10
#define configTIMER_TASK_STACK_DEPTH 200
#define INCLUDE_xEventGroupSetBitFromISR 1
#define INCLUDE_xTimerPendFunctionCall 1
LikeLike
What I missed to say in my response: you need to enable ‘Timers’ in the FreeRTOS component settings. See last screen shot in this post (“Enabled FreeRTOS Software Timers”). This will configure the mentioned defines in FreeRTOSConfig.h
LikeLike
Thanks Eric !
Best regards,
Konstantin
LikeLike
Hi Erich,
I followed your tutorial and my LEDs are working. I am now trying to add the accelerometer into my program and my program hangs (goes to default ISR). I have used the I2C code in other projects but it doesn’t seem to work when FreeRTOS is on. I enabled the timer like you suggested but that did not help. I moved the initialization code so that it happened outside of the RTOS but that didn’t work either. Is there a step I missed in using fsl_i2c other than enabling timer?
code I’m using:
uint8_t rxbuf[6];
cmd[0]=0x0D;
I2C_DRV_MasterReceiveDataBlocking(FSL_I2CCOM1,&i2cCom1_MasterConfig0,cmd,1,rxbuf,1,1000);
^^this code works in other projects.
Thanks,
Michael
LikeLike
Do you know what interrupt is firing which goes into the default ISR? Maybe it is a hardfault, so you have not initialized a handler correctly?
LikeLike
I’m not sure how to tell exactly what interrupt is firing, could you explain? What I can do is single step through the program and I found that I end up in DefaultISR when executing
I2C_DRV_SendAddress(instance, device, cmdBuff, cmdSize, kI2CReceive, timeout_ms);
which is part of the above mentioned “Master Receive” function. Now whats weirder than that is if I “step into” the “Send Address” command that is giving me the error, I can actually single step through that just fine and I get through the whole thing fine and even return the correct response “0xC7”. Thanks for your help this is my first experience with FreeRTOS and your tutorials are invaluable!
LikeLike
another thought: if it depends what and how you are stepping through the code, it means the timing has an effect. Are you sure you have the watchdog disabled in the startup code? Could it be that your watchdog timer fires and resets?
LikeLike
Increase the amount of memory (Total heap size).
I had the exact same problem fsl_dspi.
LikeLike
Right, that’s a good point. But in case you run out of heap, with enabled ‘out-of-heap’ hook, it simply should halt the program, and not crash.
LikeLike
By this do you mean increase the value in “Total Heap Size” inside the FreeRTOS->Memory tab? I tried that, increased it to 32k but still no luck…. Is there another place I should increase memory? Maybe there is something else you did?
LikeLike
Hi Michael,
yes, it means that toal heap size: configTOTAL_HEAP_SIZE
Erich
LikeLike
Increasing the Total Heap size did not fix my problem. Is there any other things needed to make it work? I have not tried fsl_dspi though only fsl_i2c to talk to onboard FXOS8700CQ. Your help is greatly appreciated!
LikeLike
What about increasing the stack size for the tasks? Can you try this?
LikeLike
Erich,thanks for your huge work
It is a couple of day that i’m trying to remove the osa1 component without adding any OS.
Is it possible?
Thanks
LikeLike
No, this is not possible from what I can tell. I tried it too, but I had no success to remove it. It seems that this is coming from the Kinetis SDK, as without the SDK, I do not need that component.
LikeLike
Thank you very much Erich to find the time to answer us and developing the beautiful website,
i’m a bit desperate because if you confirm my impression there is probably nothing to do.
My project needs to be light;even if i set the OS component for BareMetal,without any clock association to osa1 some OS code is generate and the program passes through it
Do you know if somewhere exist any skeleton of project helping me to initialize MK02 (clock settings,stack,global variabiles and so on )without re-invent the wheel working register by register ?I have no problem with peripheral registers once the processor and initial stuff are correctly set
Or maybe..is there any clean way to generate it with SDK and then re-use it in a non-SDK project?
LikeLike
Maybe you simply could use one of the examples in C:\Freescale\KSDK_1.2.0\examples\frdmkl02z as a starting point for your project?
LikeLike
Very good demo!
I changed it up a bit. I made BlinkyTask read in parameters to decide which RGB LED color it would handle, plus when to toggle. Then TaskCreated 3 BlinkyTasks to cycle through the color combinations. Still learning the basics.
I have to jump to freescale’s kboot and play with that next. I’ll eventually create a dual application/update recovery setup for kboot. I’m a bit disappointed I haven’t found something for what I thought was common feature. Any suggestions or tips?
Thank you so much! Keep up the good work!
LikeLike
Thanks :-). And yes, you can use the task parameter to decice what to do inside the task (I don’t use that feature very often).
I have not really played with kboot yet, and used my own bootloader so far instead: https://mcuoneclipse.com/2013/04/28/serial-bootloader-for-the-freedom-board-with-processor-expert/
LikeLike
Is it possible to do semi-hosting and use the above method with FreeRTOS?
I always get a multiple definition of `_sbrk’ error because it requires the UTIL component.
LikeLike
Yes, that works fine. But in the SDK it is not the UTIL, it is the utility module which has its own (and sometimes incompatible) _sbrk() implementation. You might have a look on that _sbrk() topic here:
So depending on which GNU libraries you are using, you might simply rename that _sbrk() in the Kinetis SDK, and use your own version (this is what I’m doing).
LikeLike
Pingback: First steps: ARM Cortex-M7 and FreeRTOS on NXP TWR-KV58F220M | MCU on Eclipse
Hey Erich,
Love your blog. I’m currently working on a project that uses the KSDK 1.3’s freertos component. I’d like to make the switch to your component to get the extra debugging features ( GDB Helper, Segger System Viewer, plus I can move up to FreeRTOS v9 for the static functions ).
After I made the switch, my code runs but eventually gets stuck in the IDLE task. When I look at the task list, it shows that all my tasks are blocked. When I dug a little bit deeper I found that the SysTick_Handler is never executed.
I know the scheduler is started and the Tick Timer is initiated so I’m not sure why the SysTick Handler isn’t getting hit.
Is there any other PEx settings or configurations that may help?
Note, I’m using Cortex-m4 and FreeRtos 8.2.3
Thanks again!
LikeLike
Hi Jake,
I belive the problem is that either SysTick is not running, or you are using the SysTick from the SDK so it executes the wrong interrupt service routine. Make sure that you have set the SDK fsl_os_abstraction component setting to ‘bare metal’ and the systick/timer disabled there.
And have the SysTick enabled in the McuOnEcliopse component.
I hope this helps,
Erich
LikeLike
Pingback: Tutorial: Blinky with Kinetis SDK V1.3 and Processor Expert | MCU on Eclipse