CMSIS-RTOS2: Configure RTX v5

| CMSIS-RTOS2 Version 2.1.1 Real-Time Operating System: API and RTX Reference Implementation

- Main Page
- Usage and Description
- Reference

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Configure RTX v5

The file "RTX_Config.h" defines the configuration parameters of CMSIS-RTOS RTX and must be part of every project that is using the CMSIS-RTOS RTX kernel. The configuration options are explained in detail in the following sections:

- System Configuration covers system-wide settings for the global memory pool, tick frequency, ISR event buffer and round-robin thread switching.
- Thread Configuration provides several parameters to configure the Thread Management functions.
- Timer Configuration provides several parameters to configure the Timer Management functions.
- Event Flags Configuration provides several parameters to configure the Event Flags functions.
- Mutex Configuration provides several parameters to configure the Mutex Management functions.
- Semaphore Configuration provides several parameters to configure the Semaphores functions.
- Memory Pool Configuration provides several parameters to configure the Memory Pool functions.
- Message Queue Configuration provides several parameters to configure the Message Queue functions.

The file "RTX_Config.c" contains default implementations of the functions osRtxIdleThread and osRtxErrorNotify. Both functions can simply be overwritten with a custimized behavior by redefining them as part of the user code.

The configuration file uses Configuration Wizard Annotations. Refer to Pack - Configuration Wizard Annotations for details. Depending on the development tool, the annotations might lead to a more user-friendly graphical representation of the settings. The screenshot below is a screenshot from the µVision Configuration Wizard view:

RTX_Config.h in Configuration Wizard View

Alternatively one can provide configuration options using the compiler command line.

For example one can customize the used tick frequency to 100us by (overwriting) the configuration using

cc -DOS_TICK_FREQ=100

# System Configuration

The system configuration covers system-wide settings for the global memory pool, tick frequency, ISR event buffer and round-robin thread switching.

System Configuration Options

RTX_Config.h: System Configuration

Name | #define | Description
--- | --- | ---
Global Dynamic Memory size [bytes] | `OS_DYNAMIC_MEM_SIZE` | Defines the combined global dynamic memory size for the Global Memory Pool. Default value is 4096. Value range is [0-1073741824] bytes, in multiples of 8 bytes.
Kernel Tick Frequency (Hz) | `OS_TICK_FREQ` | Defines base time unit for delays and timeouts in Hz. Default: 1000Hz = 1ms period.
Round-Robin Thread switching | `OS_ROBIN_ENABLE` | Enables Round-Robin Thread switching.
Round-Robin Timeout | `OS_ROBIN_TIMEOUT` | Defines how long a thread will execute before a thread switch. Default value is 5. Value range is [1-1000].
ISR FIFO Queue | `OS_ISR_FIFO_QUEUE` | RTOS Functions called from ISR store requests to this buffer. Default value is 16 entries. Value range is [4-256] entries in multiples of 4.

## Global dynamic memory

Refer to Global Memory Pool.

## Round-Robin Thread Switching

RTX5 may be configured to use round-robin multitasking thread switching. Round-robin allows quasi-parallel execution of several threads of the same priority. Threads are not really executed concurrently, but are scheduled where the available CPU time is divided into time slices and RTX5 assigns a time slice to each thread. Because the time slice is typically short (only a few milliseconds), it appears as though threads execute simultaneously.

Round-robin thread switching functions as follows:

- the tick is preloaded with the timeout value when a thread switch occurs
- the tick is decremented (if not already zero) each system tick if the same thread is still executing
- when the tick reaches 0 it indicates that a timeout has occurred. If there is another thread ready with the same priority, then the system switches to that thread and the tick is preloaded with timeout again.

In other words, threads execute for the duration of their time slice (unless a thread's time slice is given up). Then, RTX switches to the next thread that is in READY state and has the same priority. If no other thread with the same priority is ready to run, the current running thread resumes it execution.

Note
  When switching to higher priority threads, the round-robin timeout value is reset.

Round-Robin multitasking is controlled with the #define OS_ROBIN_ENABLE. The time slice period is configured (in RTX timer ticks) with the #define OS_ROBIN_TIMEOUT.

## ISR FIFO Queue

The RTX functions (Calls from Interrupt Service Routines), when called from and interrupt handler, store the request type and optional parameter to the ISR FIFO queue buffer to be processed later, after the interrupt handler exits.

The scheduler is activated immediately after the IRQ handler has finished its execution to process the requests stored to the FIFO queue buffer. The required size of this buffer depends on the number of functions that are called within the interrupt handler. An insufficient queue size will be caught by osRtxErrorNotify with error code osRtxErrorISRQueueOverflow.

# Thread Configuration

The RTX5 provides several parameters to configure the Thread Management functions.

Thread Configuration Options

RTX_Config.h: Thread Configuration

Option | #define | Description
--- | --- | ---
Object specific Memory allocation | `OS_THREAD_OBJ_MEM` | Enables object specific memory allocation. See Object-specific Memory Pools.
Number of user Threads | `OS_THREAD_NUM` | Defines maximum number of user threads that can be active at the same time. Applies to user threads with system provided memory for control blocks. Default value is 1. Value range is [1-1000].
Number of user Threads with default Stack size | `OS_THREAD_DEF_STACK_NUM` | Defines maximum number of user threads with default stack size and applies to user threads with 0 stack size specified. Value range is [0-1000].
Total Stack size [bytes] for user Threads with user-provided Stack size | `OS_THREAD_USER_STACK_SIZE` | Defines the combined stack size for user threads with user-provided stack size. Default value is 0. Value range is [0-1073741824] Bytes, in multiples of 8.
Default Thread Stack size [bytes] | `OS_STACK_SIZE` | Defines stack size for threads with zero stack size specified. Default value is 200. Value range is [96-1073741824] Bytes, in multiples of 8.
Idle Thread Stack size [bytes] | `OS_IDLE_THREAD_STACK_SIZE` | Defines stack size for Idle thread. Default value is 200. Value range is [72-1073741824] bytes, in multiples of 8.
Stack overrun checking | `OS_STACK_CHECK` | Enable stack overrun checks at thread switch.
Stack usage watermark | `OS_STACK_WATERMARK` | Initialize thread stack with watermark pattern for analyzing stack usage. Enabling this option increases significantly the execution time of thread creation.
Processor mode for Thread execution | `OS_PRIVILEGE_MODE` | Controls the processor mode. Default value is Privileged mode. Value range is [0=Unprivileged; 1=Privileged] mode.

## Configuration of Thread Count and Stack Space

The RTX5 kernel uses a separate stack space for each thread and provides two methods for defining the stack requirements:

- Static allocation: when osThreadAttr_t::stack_mem and osThreadAttr_t::stack_size specify a memory area which is used for the thread stack. Attention: The stack memory provided must be 64-bit aligned, i.e. by using uint64_t for declaration.
- Dynamic allocation: when osThreadAttr_t is NULL or osThreadAttr_t::stack_mem is NULL, the system allocates the stack memory from:
  - Object-specific Memory Pool (default Stack size) when "Object specific Memory allocation" is enabled and "Number of user Threads with default Stack size" is not 0 and osThreadAttr_t::stack_size is 0 (or osThreadAttr_t is NULL).
  - Object-specific Memory Pool (user-provided Stack size) when "Object specific Memory allocation" is enabled and "Total Stack size for user..." is not 0 and osThreadAttr_t::stack_size is not 0.
  - Global Memory Pool when "Object specific Memory allocation" is disabled or (osThreadAttr_t::stack_size is not 0 and "Total Stack size for user..." is 0) or (osThreadAttr_t::stack_size is 0 and "Number of user Threads with default Stack size" is 0).

osThreadAttr_t is a parameter of the function osThreadNew.

Note
  Before the RTX kernel is started by the osKernelStart() function, the main stack defined in startup_device.s is used. The main stack is also used for:
  - user application calls to RTX functions in thread mode using SVC calls
  - interrupt/exception handlers.

## Stack Overflow Checking

RTX5 implements a software stack overflow checking that traps stack overruns. Stack is used for return addresses and automatic variables. Extensive usage or incorrect stack configuration may cause a stack overflow. Software stack overflow checking is controlled with the define `OS_STACK_CHECK`.

If a stack overflow is detected, the function osRtxErrorNotify with error code osRtxErrorStackUnderflow is called. By default, this function is implemented as an endless loop and will practically stop code execution.

## Stack Usage Watermark

RTX5 initializes thread stack with a watermark pattern (0xCC) when a thread is created. This allows the debugger to determine the maximum stack usage for each thread. It is typically used during development but removed from the final application. Stack usage watermark is controlled with the define `OS_STACK_WATERMARK`.

Enabling this option significantly increases the execution time of osThreadNew (depends on thread stack size).

## Processor Mode for Thread Execution

RTX5 allows to execute threads in unprivileged or privileged processor mode. The processor mode is controlled with the define `OS_PRIVILEGE_MODE`.

In unprivileged processor mode, the application software:

- has limited access to the MSR and MRS instructions, and cannot use the CPS instruction.
- cannot access the system timer, NVIC, or system control block.
- might have restricted access to memory or peripherals.

In privileged processor mode, the application software can use all the instructions and has access to all resources.

# Timer Configuration

RTX5 provides several parameters to configure the Timer Management functions.

Timer Configuration Options

RTX_Config.h: Timer Configuration

Name | #define | Description
--- | --- | ---
Object specific Memory allocation | `OS_TIMER_OBJ_MEM` | Enables object specific memory allocation.
Number of Timer objects | `OS_TIMER_NUM` | Defines maximum number of objects that can be active at the same time. Applies to objects with system provided memory for control blocks. Value range is [1-1000].
Timer Thread Priority | `OS_TIMER_THREAD_PRIO` | Defines priority for timer thread. Default value is 40. Value range is [8-48], in multiples of 8. The numbers have the following priority correlation: 8=Low; 16=Below Normal; 24=Normal; 32=Above Normal; 40=High; 48=Realtime
Timer Thread Stack size [bytes] | `OS_TIMER_THREAD_STACK_SIZE` | Defines stack size for Timer thread. May be set to 0 when timers are not used. Default value is 200. Value range is [0-1073741824], in multiples of 8.
Timer Callback Queue entries | `OS_TIMER_CB_QUEUE` | Number of concurrent active timer callback functions. May be set to 0 when timers are not used. Default value is 4. Value range is [0-256].

## Object-specific memory allocation

See Object-specific Memory Pools.

## User Timer Thread

The RTX5 function osRtxTimerThread executes callback functions when a time period expires. The priority of the timer subsystem within the complete RTOS system is inherited from the priority of the osRtxTimerThread. This is configured by `OS_TIMER_THREAD_PRIO`. Stack for callback functions is supplied by osRtxTimerThread. `OS_TIMER_THREAD_STACK_SIZE` must satisfy the stack requirements of the callback function with the highest stack usage.

# Event Flags Configuration

RTX5 provides several parameters to configure the Event Flags functions.

Event Configuration Options

RTX_Config.h: Event Flags Configuration

Name | #define | Description
--- | --- | ---
Object specific Memory allocation | `OS_EVFLAGS_OBJ_MEM` | Enables object specific memory allocation. See Object-specific Memory Pools.
Number of Event Flags objects | `OS_EVFLAGS_NUM` | Defines maximum number of objects that can be active at the same time. Applies to objects with system provided memory for control blocks. Value range is [1-1000].

## Object-specific memory allocation

When object-specific memory is used, the pool size for all Event objects is specified by `OS_EVFLAGS_NUM`. Refer to Object-specific Memory Pools.

# Mutex Configuration

RTX5 provides several parameters to configure the Mutex Management functions.

Mutex Configuration Options

RTX_Config.h: Mutex Configuration

Name | #define | Description
--- | --- | ---
Object specific Memory allocation | `OS_MUTEX_OBJ_MEM` | Enables object specific memory allocation. See Object-specific Memory Pools.
Number of Mutex objects | `OS_MUTEX_NUM` | Defines maximum number of objects that can be active at the same time. Applies to objects with system provided memory for control blocks. Value range is [1-1000].

## Object-specific Memory Allocation

When object-specific memory is used, the pool size for all Mutex objects is specified by `OS_MUTEX_NUM`. Refer to Object-specific Memory Pools.

# Semaphore Configuration

RTX5 provides several parameters to configure the Semaphores functions.

Semaphore Configuration Options

RTX_Config.h: Semaphore Configuration

Name | #define | Description
--- | --- | ---
Object specific Memory allocation | `OS_SEMAPHORE_OBJ_MEM` | Enables object specific memory allocation. See Object-specific Memory Pools.
Number of Semaphore objects | `OS_SEMAPHORE_NUM` | Defines maximum number of objects that can be active at the same time. Applies to objects with system provided memory for control blocks. Value range is [1-1000].

## Object-specific memory allocation

When Object-specific Memory is used, the pool size for all Semaphore objects is specified by `OS_SEMAPHORE_NUM`. Refer to Object-specific Memory Pools.

# Memory Pool Configuration

RTX5 provides several parameters to configure the Memory Pool functions.

Memory Pool Configuration Options

RTX_Config.h: Memory Pool Configuration

Name | #define | Description
--- | --- | ---
Object specific Memory allocation | `OS_MEMPOOL_OBJ_MEM` | Enables object specific memory allocation. See Object-specific Memory Pools.
Number of Memory Pool objects | `OS_MEMPOOL_NUM` | Defines maximum number of objects that can be active at the same time. Applies to objects with system provided memory for control blocks. Value range is [1-1000].
Data Storage Memory size [bytes] | `OS_MEMPOOL_DATA_SIZE` | Defines the combined data storage memory size. Applies to objects with system provided memory for data storage. Default value is 0. Value range is [0-1073741824], in multiples of 8.

## Object-specific memory allocation

When object-specific memory is used, the number of pools for all MemoryPool objects is specified by `OS_MEMPOOL_NUM`. The total storage size reserved for all pools is configured in `OS_MEMPOOL_DATA_SIZE`. Refer to Object-specific Memory Pools.

# Message Queue Configuration

RTX5 provides several parameters to configure the Message Queue functions.

MessageQueue Configuration Options

RTX_Config.h: Message Queue Configuration

Name | #define | Description
--- | --- | ---
Object specific Memory allocation | `OS_MSGQUEUE_OBJ_MEM` | Enables object specific memory allocation. See Object-specific Memory Pools.
Number of Message Queue objects | `OS_MSGQUEUE_NUM` | Defines maximum number of objects that can be active at the same time. Applies to objects with system provided memory for control blocks. Value range is [1-1000].
Data Storage Memory size [bytes] | `OS_MSGQUEUE_DATA_SIZE` | Defines the combined data storage memory size. Applies to objects with system provided memory for data storage. Default value is 0. Value range is [0-1073741824], in multiples of 8.

## Object-specific memory allocation

When Object-specific Memory is used, the number of queues for all Message Queue objects is specified by `OS_MSGQUEUE_NUM`. The total storage size reserved for all queues is configured in `OS_MSGQUEUE_DATA_SIZE`. Refer to Object-specific Memory Pools.