Developing uCOS applications for Kinetis devices

This tutorial shows how to use the uCOS III operating system that comes with the Kinetis KSDK to develop applications for the Kinetis devices. Before you begin, install VisualGDB and try creating a basic project for your board to ensure that you can build and debug it.

In this example we will create a basic application consisting of 2 threads: one thread will blink the LED and the other thread will control the first one via the event object. We will show how to use the Kinetis OS abstraction layer API to seamlessly switch from uCOS to FreeRTOS.

  1. Start Visual Studio and launch the VisualGDB Embedded Project Wizard:01-rtosdemo
  2. Proceed with the default setting of creating a new embedded binary:02-prjtype
  3. On the next page select your device. In this tutorial we will use the FRDM-KL25Z board, so we select the MK25Z128VLH4 device:03-device
  4. Select the LED port on your board. The default PTB18 setting corresponds to the red channel of the RGB LED on the FRDM-KL25Z:04-ledblink
  5. Select the debug method that works with your device. We will use the on-board OpenSDA with the firmware from Segger and hence will use the J-Link software. See this tutorial for instructions on downloading the Segger firmware into OpenSDA:05-jlink
  6. Press Finish to generate your project. Build it with pressing Ctrl-Shift-B:06-build
  7. Now we will modify the code to create 2 threads instead of 1:
    • The first thread will wait for an event object by calling the OSA_EventWait() function
    • The second thread will periodically set and clear the event object by calling OSA_EventSet() and OSA_EventClear()

    Replace the contents of the main source file with the following:

    #include "MKL25Z4.h"
    #include "fsl_os_abstraction.h"
    #include "fsl_gpio_driver.h"
     
    static event_t s_Event;
     
    void MainTaskBody(task_param_t param)
    {
        for (;;)
        {
            event_flags_t flags;
            OSA_EventWait(&s_Event, 1, true, OSA_WAIT_FOREVER, &flags);
            GPIO_HAL_ClearPinOutput(GPIOB_BASE_PTR, 18);
            OSA_TimeDelay(100);
            GPIO_HAL_SetPinOutput(GPIOB_BASE_PTR, 18);
            OSA_TimeDelay(100);
        }
    }
     
    void SecondTaskBody(task_param_t param)
    {
        for (;;)
        {
            OSA_EventSet(&s_Event, 1);
            OSA_TimeDelay(500);
            OSA_EventClear(&s_Event, 1);
            OSA_TimeDelay(500);
        }
    }
    enum { kMainTaskStackSize = 512, kMainTaskPriority = 1 };
     
    int main()
    {
        osa_status_t status;
        OSA_TASK_DEFINE(task_func, kMainTaskStackSize);
        OSA_TASK_DEFINE(task_func2, kMainTaskStackSize);
        OSA_EventCreate(&s_Event, kEventManualClear);
        
        SIM_BASE_PTR->SCGC5 |= SIM_SCGC5_PORTB_MASK;
        PORTB_BASE_PTR->PCR[18] = PORT_PCR_MUX(1);
        OSA_Init();
        
        GPIO_HAL_SetPinDir(GPIOB_BASE_PTR, 18, kGpioDigitalOutput);
     
        
        status = OSA_TaskCreate(MainTaskBody,
            (uint8_t *)"task_name",
            kMainTaskStackSize,
            task_func_stack,
            kMainTaskPriority,
            0,
            false,
            &task_func_task_handler);
        
        status = OSA_TaskCreate(SecondTaskBody,
            (uint8_t *)"task2_name",
            kMainTaskStackSize,
            task_func2_stack,
            kMainTaskPriority,
            0,
            false,
            &task_func2_task_handler);
     
        OSA_Start();
        return 0;
    }

    Then press F5 to build and debug you project. Observe how the LEDs blinks in series of short impulses.

  8. Set a breakpoint on the line calling OSA_EventSet() and step into the function. See how the Kinetis OS Abstraction layer calls the OSFlagPost() function that is a part of uCOS: 07-flagpost
  9. Now we will replace uCOS with FreeRTOS. Open VisualGDB Project Properties, go to the Embedded Frameworks page, uncheck the “Kinetis uCOS III” one and check “Kinetis FreeRTOS”:08-rtos
  10. Press “OK”. If you try building your project now, Visual Studio will complain that the FreerTOSConfig.h file is missing and the os_app_hooks.c file won’t build due to uCOS-specific code. Copy the FreeRTOSConfig.h file from a FreeRTOS project to the project directory and remove os_app_hook.c from the project. Now you can press F5 to build and start debugging the project. Note how now the OSA_EventSet() function calls xEventGroupSetBits() that is a part of FreeRTOS:09-setbits

The RTOS abstraction layer provided by Kinetis KSDK allows switching between RTOSes without changing the code of the application as long as you don’t call any RTOS-specific APIs. This allows easily comparing the performance and memory footprint of different RTOSes and picking the optimal one for your task.