Tutorial: Using Processor Expert and FreeRTOS with Kinetis SDK V1.2

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.

Blinky Task with FreeRTOS and Kinetis SDK in KDS v3.0.0

Blinky Task with FreeRTOS and Kinetis SDK in KDS v3.0.0

💡 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:

FRDM-K64F Board

FRDM-K64F Board

Creating Project

I create a new project in Kinetis Design Studio with the menu File > New > Kinetis Project:

New Kinetis Project

New Kinetis Project

Then I give a name for the project:

Project name

Project name

Then select the board/device:

Board Device Selection

Board Device Selection

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

SDK with Processor Expert

SDK with Processor Expert

Then select the GNU C Compiler, and press Finish:

GNU C Compiler Selected

GNU C Compiler Selected

And the project is created:

Project Created

Project Created

Kinetis SDK V1.2 fsl_os_abstraction

With the SDK v1.2 there is now a fsl_os_abstraction component:

Kinetis SDK v1.2 fsl_os_abstraction

Kinetis SDK v1.2 fsl_os_abstraction

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 :-):

Timer set to none

Timer set to none

💡 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:

fsl_os_abstraction with FreeRTOS

fsl_os_abstraction with FreeRTOS

It then adds its own SDK FreeRTOS component to it:

FreeRTOS as subcomponent of fsl_os_abstraction

FreeRTOS as sub-component of fsl_os_abstraction

And in the properties I can configure the RTOS:

free_rtos component settings

free_rtos component settings

This component additionally adds a compiler preprocessor define “FSL_RTOS_FREE_RTOS” to tell the SDK sources which RTOS is used:

FSL_RTOS_FREE_RTOS Preprocessor Setting

FSL_RTOS_FREE_RTOS Preprocessor Setting

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’:

BareMetal Setting

BareMetal Setting

Add to the “FSL_RTOS_FREE_RTOS” compiler preprocessor defines so the SDK knows we are using FreeRTOS:

FSL_RTOS_FREE_RTOS Defined in Compiler Settings

FSL_RTOS_FREE_RTOS Defined in Compiler Settings

Then add the McuOnEclipse FreeRTOS component to the project. It will automatically add as well the Utility component:

Added McuOnEclipse FreeRTOS component

Added McuOnEclipse FreeRTOS 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:

Kinetis ARM FreeRTOS Settings

Kinetis ARM FreeRTOS Settings

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’:

FreeRTOS configuration for Kinetis SDK

FreeRTOS configuration for Kinetis SDK

With this, ready to generate Processor Expert code, and then do a build:

Generating Processor Expert Code

Generating Processor Expert Code

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:

main with task creation

main with task creation

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:

Blinky Task, not blinking yet

Blinky Task, not blinking yet

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:

fsl_gipo SDK Driver in Components Library

fsl_gipo SDK Driver in Components Library

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.

Red RGB LED Pin Settings

Red RGB LED Pin Settings

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:

Drag and Drop SDK Function

Drag and Drop SDK Function

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 🙂

Blinky on Freescale Kinetis FRDM-K64F Board

Blinky on Freescale Kinetis 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().

Enabled FreeRTOS Software Timers

Enabled FreeRTOS Software Timers

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 🙂

38 thoughts on “Tutorial: Using Processor Expert and FreeRTOS with Kinetis SDK V1.2

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

    Like

    • 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

      Like

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

    Like

    • 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 🙂

      Like

      • 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!

        Like

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

    Like

    • 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

      Like

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

    Like

      • 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!

        Like

        • 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?

          Like

      • 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?

        Like

      • 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!

        Like

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

    Like

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

      Like

      • 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?

        Like

  6. 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!

    Like

  7. Pingback: First steps: ARM Cortex-M7 and FreeRTOS on NXP TWR-KV58F220M | MCU on Eclipse

  8. 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!

    Like

    • 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

      Like

  9. Pingback: Tutorial: Blinky with Kinetis SDK V1.3 and Processor Expert | MCU on Eclipse

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.