Lesson 14: Real-Time OS FreeRTOS

About FreeRTOS Operation System

FreeRTOS is a real-time operating system kernel for embedded devices. It is a free RTOS, a portable, open source, and a mini Real Time kernel for microcontrollers. You can check http://www.FreeRTOS.org regularly for detailed information.

FreeRTOS has the following editions:

FreeRTOS

FreeRTOS has the following standard features:

  • Preemptive or co-operative operation
  • Very flexible task priority assignment
  • Queues, binary semaphores, counting semaphores, mutexes, recursive mutexes
  • Software timers, event groups
  • Tick and Idle hook functions
  • Stack overflow checking

OpenRTOS

OpenRTOS is a commercially licensed version of FreeRTOS provided under license from Real Time Engineers Ltd. by a third party.

SafeRTOS

SafeRTOS shares the same usage model as FreeRTOS but has been developed in accordance with the practices, procedures, and processes necessary to claim compliance with various internationally recognized safety-related standards.

Licensing

The FreeRTOS is based on an open-source license. It can be used in commercial applications and is freely available to everybody. FreeRTOS users retain ownership of their intellectual property.

See http://www.FreeRTOS.org/license for the latest open source license information.

Data Types

Specific Data Types

FreeRTOS has two definitions for the port-specific data type: TickType_t and BaseType_t in the portmacro.h header file.

Prefix Description Configuration
TickType_t a data type used to hold the tick count value configUSE_16_BIT_TICKS = 0TickType_t is defined as an unsigned 32-bit type;
configUSE_16_BIT_TICKS = 1TickType_t is defined as an unsigned 16-bit type;
BaseType_t generally used for return types that can take only a very limited range of values; and for the pdTRUE/pdFALSE type Booleans 8-bit architecture ➤ BaseType_t is defined as an 8-bit type;
16-bit architecture ➤ BaseType_t is defined as a 16-bit type;
32-bit architecture ➤ BaseType_t is defined as a 32-bit type;

Create FreeRTOS Project

  1. Create a new Project in PSoC Creator.
  2. Download FreeRTOS_10.0.0_PSoC56.zip, and unzip it into the project folder.
  3. Add FreeRTOS paths into the Project
    • From the Project menu, select Build Setting...
    • Expand the tree under ARM GCC XXXX, and click Compiler
    • Add the following directories into the Additional Include Directories item:
      .\FreeRTOS; .\FreeRTOS\include; .\FreeRTOS\portable\GCC\ARM_CM3; .\FreeRTOS\portable\MemMang
      addFreeRTOSpath s
  4. Create Folders and add FreeRTOS files to your project
    • Right-click on the Project in the Workspace Explorer window, then type FreeRTOS as the folder name.
      addNewFolder FreeRTOS
    • Select Add ➤ New Folder. Add the following folders under the FreeRTOS folder
      addSubFolders FreeRTOS
    • Right-click on the RTOS folder just created, and select Add ➤ Existing Item. Add the following files into the folders as shown as blows:
      addNewFiles FreeRTOS

Application Code

FreeRTOS Architecture

The following flowchart shows how to create a simple FreeRTOS-based application.

01 SimpleFreeRTOSApp s

In the main() function:

  1. FreeRTOS_Init(): Initializes the interrupt vector table for FreeRTOS.
  2. MyPSoCSetup(): Initializes the PSoC components and global variables
  3. xTaskCreate(...): Creates a new instance of a task
  4. vTaskStartScheduler(): Starts the FreeRTOS scheduler running. If all is well, the scheduler will be running, and the next infinite loop will never be reached
  5. Infinite loop: If this code is executed, that means that there was insufficient FreeRTOS heap memory available for the idle and/or timer tasks to be created

Example FreeRTOS code

#include <project.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

// FreeRTOS Header Files
#include "FreeRTOS_PSoC.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "timers.h"
#include "event_groups.h"

// Your Task Functions
void vAppTask( void *pvParameters );

//------------------------------------------------------------------------------
void MyPSoCSetup( void )
{
	/* Start-up the peripherals. */

	/* Enable and clear the LCD Display. */
    LCD_Start();
	LCD_ClearDisplay();
	LCD_PrintString( "AirSupplyLab" );
}
//------------------------------------------------------------------------------
int main( void )
{
    BaseType_t  err;

    FreeRTOS_Init();
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
	MyPSoCSetup();

    /* --- APPLICATION TASKS CAN BE CREATED HERE --- */
    err = xTaskCreate(
        vAppTask,                   // Pointer to the function that implements the task
        "Task 1",                   // Text name for the task. This is to facilitate debugging only
        configMINIMAL_STACK_SIZE,   // Stack depth - small microcontrollers will use much less stack than this
        NULL,                       // Task parameter
        5,                          // Task priority
        NULL);                      // Task handle

    if (err != pdPASS){
        LCD_Position(1,0);
        LCD_PrintString("Cannot create task!");
        while(1){};
    }
    /* Start the scheduler so the tasks start executing. */
	vTaskStartScheduler();

    /* Execution will only reach here if there was insufficient heap to start the scheduler. */
	/* Should never reach here as the kernel will now be running.  If
	vTaskStartScheduler() does return then it is very likely that there was
	insufficient (FreeRTOS) heap space available to create all the tasks,
	including the idle task that is created within vTaskStartScheduler() itself. */
	for( ;; ){
        vTaskDelay(1000);
    }
}
//------------------------------------------------------------------------------
void vAppTask(void *pvParameters)
{
    for(;;){
        LED_Write( !LED_Read());
        vTaskDelay(pdMS_TO_TICKS(500));    // Delay for 500ms
    }
}

Lesson 15: Real-Time OS μC/OS-III

About μC/OS Real-Time Operating System

Micriμm's μC/OS (pronounced "Micro C O S") is a preemptive, highly portable, and scalable real-time kernel. μC/OS has three versions:

μC/OS
  • Portable
  • ROMable
  • Very Scalable
  • Preemptive Real-Time
  • Multitasking Kernel
  • Posted over 45 different CPU architecture
  • 6 KB ~ 24 KB RTOS Kernel
μC/OS-II
  • Can manage up to 250 application tasks
  • Provides the following services:
    • Semaphores
    • Event Flags
    • Mutual-exclusion semaphores
    • Message mailboxes and queues
    • Task management
    • Time management
    • Timer management
    • Fixed-sized memory block management
μC/OS-III
  • Included all features in μC/OS-II
  • Additional features:
    • Allows multiple tasks to run at the same priority level
    • Round Robin Scheduling of tasks at equal priority
    • Unlimited number of application tasks
    • Unlimited number of kernel objects
    • Unlimited number of priorities (32~256)
    • The low interrupt disable time (near 0)
    • Real-time configurable

Licensing

μC/OS-II is provided in source form, and μC/OS-III is a linkable library for a FREE evaluation, educational use, or peaceful research. If you plan on using μC/OS-II or μC/OS-III in a commercial product, you need to contact Micriμm to properly license its use in your product.


Create μC/OS-III Project

  1. Create a new Project in PSoC Creator.
  2. Download RTOS_uCOS-III_V3.03.01.zip, and unzip it into the project folder.
  3. Add RTOS path into the Project
    • From the Project menu, select Build Setting...
    • Expand the tree under ARM GCC XXXX, and click Compiler
    • Add the following directories into the Additional Include Directories item:
      .\RTOS; .\RTOS\uC-CPU; .\RTOS\uC-CPU\ARM-Cortex-M3\GNU; .\RTOS\uC-LIB; .\RTOS\uCOS-III\Source; .\RTOS\uCOS-III\Ports\ARM-Cortex-M3\Generic\GNU; .\RTOS\PSoC5\BSP; .\RTOS\PSoC5\BSP\OS\uCOS-III

    addRTOSpath s
  4. Add RTOS Library to the Project
    • Expand the tree under ARM GCC XXXX, and click Linker
    • Add the following libraries to the Additional Libraries item:
      m; :uCOS-III-ARM-Cortex-M3.a
    • Add the following directory to the Additional Libraries Directories item:
      .\RTOS\PSoC5\LIB
    addRTOSlib s
  5. Create Folders and add RTOS files to your project
    • Right-click on the Project in the Workspace Explorer window, then type RTOS as the folder name.
      addNewFolder
    • Select Add ➤ New Folder. Add the following folders under the RTOS folder
      folders
    • Right-click on the RTOS folder just created, and select Add ➤ Existing Item. Add the following files into the folders as shown as blows:
      addRTOSfiles
  6. Copy and paste the content from RTOS_main.c into your main.c
  7. Modify or create the AppTask_xxx based on the project design
  8. Modify the App_TaskCreate() to create all tasks

Application Code

main.c

The startup code for the compiler will bring the CPU to the main() function, which is the entry point for the application. In the main() function, you need to initialize the operating system, create the primary application task (App_TaskStart() function), begin multitasking.

Listing 1: main.c Code

int  main (void)
{
    OS_ERR  os_err;

    BSP_PreInit();                                              // Perform BSP pre-initialization

    CPU_Init();                                                 // Initialize the uC/CPU services

    OSInit(&os_err);                                            // Init uC/OS-III

    OSTaskCreate((OS_TCB      *)&App_TaskStartTCB,              // Create the start task
                 (CPU_CHAR    *)"Start",
                 (OS_TASK_PTR  )App_TaskStart,
                 (void        *)0,
                 (OS_PRIO      )APP_CFG_TASK_START_PRIO,
                 (CPU_STK     *)&App_TaskStartStk[0],
                 (CPU_STK_SIZE )APP_CFG_TASK_START_STK_SIZE_LIMIT,
                 (CPU_STK_SIZE )APP_CFG_TASK_START_STK_SIZE,
                 (OS_MSG_QTY   )0u,
                 (OS_TICK      )0u,
                 (void        *)0,
                 (OS_OPT       )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
                 (OS_ERR      *)&os_err);

    OSStart(&os_err);                                            // Start multitasking (i.e. give control to uC/OS-III)
}
  • Line 5: BSP_PreInit()
    It is a Board Support Package pre-initialization function. This function will perform all the hardware initialization required before the OS is initialized. Most of the time, this function will disable all interrupts to make sure the application does not get interrupted until it is fully initialized.
  • Line 7: CPU_Init()
    Initializes μC/CPU. μC/CPU contains CPU-related code to enable/disable interrupts and enter/exit to/from critical sections, among other functions.
  • Line 9: OSInit()
    Initialize the μC/OS-III. This function must be called before creating a task or any other kernel object.
  • Line 11: OSTaskCreate()
    At least one task must be created. OSTaskCreate() will create the startup task.
  • Line 25: OSStart()
    You need to call this function to let μC/OS-III start the multitasking and give control to μC/OS-III. At this point, there should be either 4 to 6 tasks created depending on configuration option: OS_IdleTask(), OS_TickTask(), OS_StatTask(), OS_TmrTask() (optional), OS_IntQTask() (optional) and now AppTaskStart().

You can create as many tasks as you want before calling OSStart(). However, it is recommended to only create one task, AppTaskStart(). Because having a single application task allows μC/OS-III to determine the relative speed of the CPU. This allows μC/OS-III to calculate the percentage of CPU usage at run-time.

If the application needs other kernel objects, such as semaphores and message queues, then it is recommended that these be created prior to calling OSStart().

Finally, notice that interrupts are not enabled.

q

Your text...

© 2024 Air Supply Information Center (Air Supply BBS)