FreeRTOS with GCC, Cortex-M0+ and Kinetis KL25Z Freedom Board

Yesterday was my ‘lucky day’: My Kinetis-L Freedom board arrived :-). This board is really nice and features the KL25Z from the recently announced Kinetis L Family. And guess what is the first thing I want to flash on that processor? Yep: some FreeRTOS tasks. But to get there, a few important things have to be sorted out:

  1. Get the board up and running
  2. Get a compiler and Eclipse panels for the ARM Cortex-M0+
  3. Extend the Processor Expert FreeRTOS component to work with GCC and Cortex-M4 and M0+
  4. Test, Debug and Celebrate 🙂

The Freedom Board

The board can be ordered from Farnell for only about 10 Euro.

Note: I received a board directly from Freescale. The FRDM-KL25Z will be available publicly from end Sept. My Farnell/Element14 order shows 25. Sept. 2012.

The board has a nice and small form factor of 81 mm x 54 mm which makes it ideal for a lot of applications.

Freedom Board with Debug Connection

Freedom Board with Debug Connection

I love the headers as they will allow me to plug in other boards. The board is aimed to use Arduino shields. What shields are compatible is not clear yet, but the connector is able to provide 5V, while using 3.3V signals.

Freedom Board Block Diagram

Freedom Board Block Diagram

There are two mini USB connectors on the board: one is the target USB connector (the KL25Z implements USB), the other is the on-board debug connector. Yes, like the Tower board the Freedom board has on-board debug solution on it. Connecting the board using the debug connector automatically installs the drivers.

Freedom Board with Componen

Freedom Board with Componen

The debug connection features a bootloader similar to the Processor Expert USB Bootloader component. So with it is possible to program new firmware/software with simple copy or drag&drop of a file. Really nice, and looking forward to explore this more in the future.

Freescale ARM Cortex-M0+

The microcontroller on the board is a Kinetis KL25Z128 which is an ARM Cortex-M0+.

ARM Cortex-M0+

ARM Cortex-M0+

The instruction set is a subset of the Cortex-M4, while compatible with the Cortex-M0. The Cortex-M0+ is a very energy efficient microcontroller as demonstrated by Freescale at FTF 2012.

GCC and Eclipse Panels

The question is: what tool chain to use for Cortex-M0+? The good news is that there is a GCC maintained by ARM available both for Cortex-M0+ and Cortex-M4. Additionally, there are ARM GCC Eclipse panels available too. With this, it is not too hard to have a working Eclipse environment.

ARM GCC Eclipse Panels

ARM GCC Eclipse Panels

FreeRTOS Processor Expert Component

Next task was to make the Processor Expert component to work with GCC. This mainly means to change the port so it works with the GCC assembly syntax. For this the port.c of the FreeRTOS distribution had to be changed and extended. As I had a FreeRTOS port for CodeWarrior and Kinetis K-Family working, I was testing it first with a Cortex-M4 (Kinetis K60 Tower board). Below is an extract of the code which deals both with M4 and M0+:

__attribute__ ((naked)) void vPortSVCHandler(void) {
#if FREERTOS_CPU_CORTEX_M==4 /* Cortex M4 */
  __asm volatile (
  " ldr r3, pxCurrentTCBConst2 \n" /* Restore the context. */
  " ldr r1, [r3]               \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
  " ldr r0, [r1]               \n" /* The first item in pxCurrentTCB is the task top of stack. */
  /* pop the core registers */
  " ldmia r0!, {r4-r11}        \n"
  " msr psp, r0                \n"
  " mov r0, #0                 \n"
  " msr basepri, r0            \n"
  " orr r14, r14, #13          \n"
  " bx r14                     \n"
  "                            \n"
  " .align 2                   \n"
  "pxCurrentTCBConst2: .word pxCurrentTCB \n"
);
#else /* Cortex M0+ */
  __asm volatile (
  " ldr r3, pxCurrentTCBConst2 \n" /* Restore the context. */
  " ldr r1, [r3]               \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
  " ldr r0, [r1]               \n" /* The first item in pxCurrentTCB is the task top of stack. */
  " add r0, r0, #16            \n" /* Move to the high registers. */
  " ldmia r0!, {r4-r7}         \n" /* Pop the high registers. */
  " mov r8, r4                 \n"
  " mov r9, r5                 \n"
  " mov r10, r6                \n"
  " mov r11, r7                \n"
  "                            \n"
  " msr psp, r0                \n" /* Remember the new top of stack for the task. */
  "                            \n"
  " sub r0, r0, #32            \n" /* Go back for the low registers that are not automatically restored. */
  " ldmia r0!, {r4-r7}         \n" /* Pop low registers.  */
  " mov r1, r14                \n" /* OR R14 with 0x0d. */
  " movs r0, #0x0d             \n"
  " orr r1, r0                 \n"
  " bx r1                      \n"
  "                            \n"
  ".align 2                    \n"
  "pxCurrentTCBConst2: .word pxCurrentTCB \n"
);
#endif
}

My First Freedom Application

After a few iterations, my FreeRTOS Processor Expert component worked with all the previous cores and compilers, plus with GCC for Cortex-M4 and Cortex-M0+.

The Freedom board has a RGB LED on that board, so my first application was to run 3 task, each toggling one color of the LED. For this I added 3 LED components to my project:

Freedom Board Processor Expert Components

Freedom Board Processor Expert Components

Source code of the demo application where APP_RUN() is called from main():

/*
* Application.c
*      Author: Erich Styger
*/
#include "Application.h"
#include "LED1.h"
#include "LED2.h"
#include "LED3.h"
#include "FRTOS1.h"
#include "WAIT1.h"

static portTASK_FUNCTION(Task1, pvParameters) {
  (void)pvParameters; /* parameter not used */
  for(;;) {
    LED1_Neg();
    FRTOS1_vTaskDelay(1000/portTICK_RATE_MS);
  }
}

static portTASK_FUNCTION(Task2, pvParameters) {
  (void)pvParameters; /* parameter not used */
  for(;;) {
    LED2_Neg();
    FRTOS1_vTaskDelay(1050/portTICK_RATE_MS);
  }
}

static portTASK_FUNCTION(Task3, pvParameters) {
  (void)pvParameters; /* parameter not used */
  for(;;) {
    LED3_Neg();
    FRTOS1_vTaskDelay(2080/portTICK_RATE_MS);
  }
}

void APP_Run(void) {
  uint16_t i;

  for(i=0;i<4;i++) {
    LED1_Neg();
    LED2_Neg();
    LED3_Neg();
    WAIT1_Waitms(250);
  }
  if (FRTOS1_xTaskCreate(
    Task1,  /* pointer to the task */
    (signed portCHAR *)"Task1", /* 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) {
    /*lint -e527 */
    for(;;){}; /* error! probably out of memory */
    /*lint +e527 */
  }
if (FRTOS1_xTaskCreate(
    Task2,  /* pointer to the task */
    (signed portCHAR *)"Task2", /* 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) {
    /*lint -e527 */
    for(;;){}; /* error! probably out of memory */
     /*lint +e527 */
    }
  if (FRTOS1_xTaskCreate(
    Task3,  /* pointer to the task */
    (signed portCHAR *)"Task3", /* 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) {
    /*lint -e527 */
    for(;;){}; /* error! probably out of memory */
     /*lint +e527 */
  }
  FRTOS1_vTaskStartScheduler();
}

This creates a nice colorful changing ambient light: brightness with Freedom :-):

RGB Freedom LED controlled by 3 FreeRTOS Tasks

RGB Freedom LED controlled by 3 FreeRTOS Tasks

Happy Freedom 🙂

PS: the updated Processor Expert component for FreeRTOS with added GCC and Cortex-M0+ support is available here.

71 thoughts on “FreeRTOS with GCC, Cortex-M0+ and Kinetis KL25Z Freedom Board

  1. Erich,

    Wow! I can’t wait for my board to come. I ordered from Newark as soon as I heard about the board (from your blog). But it’s back-ordered for at least a month.

    Bill

    Like

    • Bill,
      I *love* that board! My new lecture/semester starts mid of September: Hopefully I can get my boards from Farnell soon enough. My plan is that students should get that board and can keep it. I’m already using the Tower boards for my classes. The Freedom board will be a nice extension.
      I’m working right now on porting the FatFS Processor Expert component to Kinetis and GCC, but I want to look into supporting all the other components on the Freedom board with Processor Expert. Will be for sure weekend(s) and nights full of fun (and work). Will see how much I can accomplish. We have really strong coffee 🙂
      Erich

      Like

  2. Yes, you WERE lucky. I’ve just ordered from Element14 in Australia and have been given an October delivery. Have to check back here when they turn up to see how far you’ve got. As I use a Linux hosted development environment, I can’t use MQX with CodeWarrior, as this only works with the Windows-delivered package, rather than any Eclipse-based CodeWarrior. So FreeRTOS is a must (and for other reasons.) Would also like to get it going on the QwikStik.

    Like

    • My experience with Farnell/Element14 is that they collect orders, and once it reaches a certain number, they produce a batch. So it could be that the boards get delivered earlier (at least I had that experience in the past with Farnell). And I have FreeRTOS running as well on the QwikStik. An article on this has been drafted, but the Freedom Board changed all my plans 🙂

      Like

  3. Very cool post. Thanks Erich!
    It looks like you were lucky enough to receive one of the prototype boards. I have a friend “in the know” on this project and he indicated that all the devices shown with a cross-section fill in the block diagram will not be populated on the production run. This matches up with the feature list provided over on e14’s preorder site too, so may actually be true. 😉 Also, I’ve heard the production boards will be a different color.

    I look forward to trying out FreeRTOS and ProcessorExpert when I get my KL25Z Freedom board. Looks like that is going to happen for most of us at the end of Sept (which likely means October the way these things go).

    -ZP

    Like

    • Thanks 🙂 That would mean that the debug headers will not be populated. They are 1 mm ones, but not much needed as there is the on-board OpenSDA debugging solution. Having the I/O headers not populated is not a big problem for me: anyway I like to use my own headers so I can choose the one I want and need. My element14 order still says 25th of Sept. Keeping fingers crossed for my other boards I have per-ordered, plus of course for everyone else who has placed an order.

      Like

      • From the not-very-clear photograph on the Element14 site, those look like little Samtec headers FTSH-105-01-L-DV, Element14 part number 1667759. There is an unpopulated programming header on the Kwikstik (to use the Kwikstik as a programmer, that is) that uses these – easy enough to fit. If it is those, the mating cable is Element14 part number 1667659, in case that’s of use to anyone. I have a packet of them on my desk in front of me 🙂

        Like

  4. Pingback: There is a Time and Date for both Worlds | MCU on Eclipse

  5. A nice lady from Newark / Element 14 called me on the phone the other day. Wanted to let me know about all the cool things from Freescale and specifically this Freedom demo board. I told her I had already ordered one. She was amazed. I’ve bought from Newark before, (not necessarily my go-to supplier), so I guess they had my phone number.

    /Bill

    Like

  6. Pingback: Software and Hardware Breakpoints | MCU on Eclipse

  7. Pingback: Debugger Shell: Test Automation | MCU on Eclipse

  8. Pingback: A Shell for the Freedom KL25Z Board | MCU on Eclipse

  9. Pingback: Tutorial: Enlightning the Freedom KL25Z Board | MCU on Eclipse

  10. Pingback: Software and Hardware Breakpoints « The Embedded Beat: Freescale Blog Community

  11. Pingback: Code Size Information with gcc for ARM/Kinetis | MCU on Eclipse

    • Sounds like they were being a bit over-optimistic. I ordered a couple of days after Erich did the original post – Element14 Australia have been saying mid-October all along.
      Had a phone call the other day asking if i wanted to change parts – what I ordered on was a prototype part number, they were offering to upgrade to a production part number, about $3 cheaper, too. After having had to modify my Beagle Bone to fix a hardware bug, sufficient to say, I went for the upgrade 🙂

      Like

      • I received an email from Newark a week or two ago stating the part nr. had gone obsolete. But they never mentioned a replacement for the one I had pre-ordered. The order status still shows it with the Z suffix. I assume they won’t be making/shipping the prototype.

        Like

      • Interesting, as I have not received such an offer. I just called Element14 about very long ‘in progress’ state of my order, and I was told that the material is ‘in transit’, so between the manufacturer and the inventory. “You should have it in a week or so”. Well, keeping fingers crossed, and looking forward what (and when) I will get it.

        Like

        • Just for reference, on the 21st of July I ordered 2115294, at $14.17 AUD. This part number is no longer active/orderable. I was contacted by Element14 on the 17th of September and offered 2191861 – the new active part – at $10.72. Newer AND cheaper – just hope they don’t take too long 🙂

          Like

  12. Overnight an email came in from Newark. With a UPS tracking number. The board was shipped (from South Carolina USA) and should be tomorrow (26 Sept).

    $12.95 for the board, $6.36 for shipping!, and $0.78 sales tax.

    Like

  13. Pingback: CodeWarrior for MCU10.3 beta is now available | MCU on Eclipse

  14. Pingback: S-Record, Intel Hex and Binary Files | MCU on Eclipse

  15. I received a black board today. The headers are not soldered on and some are missing. Looks like the serial flash chip is missing. Overall, a little bit of bait and switch going on here, although to their credit, Element14 did ask if I still wanted it.

    Like

  16. If Freescale is going to succeed with this, they need to standardize on one IDE and provide a lot of support routines to make thing easy. What will be the dominant IDE – CodeWarrior 10.3?

    Like

  17. Pingback: Tutorial: Freedom with FreeRTOS and Kinetis-L | MCU on Eclipse

  18. Pingback: Tutorial: Touching the Freedom KL25Z Board | MCU on Eclipse

  19. Pingback: Processor Expert, gcc C++ and Kinetis-L and MQXLite | MCU on Eclipse

  20. Pingback: A Library with ARM gcc and Eclipse | MCU on Eclipse

  21. Pingback: Removal of Processor Expert for a Project | MCU on Eclipse

  22. Pingback: Unsecuring the KL25Z Freedom Board | MCU on Eclipse

  23. Pingback: Using the 8 MHz Crystal on the FRDM-KL25Z Freedom Board | MCU on Eclipse

  24. Pingback: Defining Variables at Absolute Addresses with gcc | MCU on Eclipse

  25. Pingback: How (not) to Secure my Microcontroller | MCU on Eclipse

  26. Pingback: JTAG/SWD Debugging with the FRDM-KL25Z Board | MCU on Eclipse

  27. Pingback: Optimizing the Kinetis gcc Startup | MCU on Eclipse

  28. Pingback: Tutorial: Bits and Pins with Kinetis and the FRDM-KL25Z Board | MCU on Eclipse

  29. Pingback: Debugging Hard Faults on ARM Cortex-M | MCU on Eclipse

  30. Pingback: ARM Cortex-M0+ Interrupts and FreeRTOS | MCU on Eclipse

  31. Pingback: KL25Z and I2C: Missing Repeated Start Condition | MCU on Eclipse

  32. Pingback: Zero Cost 84×48 Graphical LCD for the Freedom Board | MCU on Eclipse

  33. Pingback: HD44780 2×16 Character Display for Kinetis and Freedom Board | MCU on Eclipse

  34. Pingback: PWM and Shell for a LED | MCU on Eclipse

  35. Pingback: Tutorial: Ultrasonic Ranging with the Freedom Board | MCU on Eclipse

  36. Pingback: The Freedom Zumo Robot | MCU on Eclipse

  37. Pingback: Tutorial: IAR + FreeRTOS + Freedom Board | MCU on Eclipse

  38. Pingback: USB MSD Host for the Freedom Board | MCU on Eclipse

  39. Pingback: A new Freedom Board: FRDM-K20D50M with ARM Cortex M4 | MCU on Eclipse

  40. Pingback: FRDM-KL25Z RevE Board arrived | MCU on Eclipse

  41. Hi there, thanks for the job.
    I have a FRDM-KL25Z board and I am using CodeWarrior for MCU v10.3. I followed all the instructions step by step, however I somehow cannot get the result as you had. I’m also having the same problem for the tutorial you published in this link: https://mcuoneclipse.com/2012/09/29/tutorial-freedom-with-freertos-and-kinetis-l/

    The exact problem is, despite there is no build or run problems and/or warnings, I do not see the light or any progress. For this code, the first loop in APP_Run() which negates three LEDs and waits for 250 ms 4 times is runs and the LEDs turn off in the rest. That makes me think “Tasks are created, but do not run, schedule and get in a cycle.” But I am not sure.

    I will be glad if you help it. It is important for my current internship by the way 🙂

    Like

    • Hello Caner,
      hard to guess what your problem is. I recommend that you set a breakpoint in one of the tasks: if it stops there, then tasks have been created and are running.
      Maybe you can try my project on GitHub (https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples) in case you have misconfigured something. That way you should have a working example. Otherwise send me your project to my email listed on the About page of this blog and I’ll have a look.
      I hope this helps.

      Like

    • Hi Caner,

      It’s been so long time and wonder if you’ve got it work already. If not, hope the following would be helpful because I experienced the same problem.

      I initially got it to work by increasing the Memory=>Total Heap Size to 8192. The default was 2048.

      Then I fine tuned Scheduler=>Minimal Stack Size to 24, after which Memory=>Total Heap Size could be set to 640 to work.

      Like

  42. Pingback: Introdução à KL25Z – CodeWarrior, Processor Expert e interrupções periódicas | void Hardwarizando( ) {

  43. Hi
    Is something wrong with official freertos port for cortex M0+ (what I suppose is port for Cortex M0, I look in to freeertos verison 9.0.0)? (I asked becouse you do not use it I suppose)

    Like

    • Hi Greg,
      nothing wrong with that. FreeRTOS 9.0.0 includes not only M0+, but as well ports for all the other supported architectures.
      The port I maintain has the following differences (from top of my head):
      – Processor Expert intergration
      – supports S08, S12, ColdFire V1/V2, DSC, ARM M0+ and M4/M7 ports
      – extended tickless idle mode with extra decision hooks
      – Keil, IAR, CW and GNU support
      – additional debugger helpers (OpenOCD/CMSIS-DAP)
      – intergrates a few extra fixes which will be in a release post v9.0.0
      – conditional heap memory usage
      – intergration with Segger RTT and Percepio Tracealizer
      – command line shell support with no-buffer-overflow string functions
      You certainly can use the normal 9.0.0 port too.

      Like

  44. Pingback: Black Magic Open Source Debug Probe for ARM with Eclipse and GDB | MCU on Eclipse

What do you think?

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