CMSIS-RTOS
Version 1.03
Real-Time Operating System: API and RTX Reference Implementation.
|
The CMSIS-RTOS RTX provides several parameters for the thread configuration.
osThreadDef defines a thread function. The parameter stacksz specifies thereby the stack requirements of this thread function. CMSIS-RTOS RTX defines two methods for defining the stack requirements:
The CMSIS-RTOS RTX kernel uses a separate stack for each thread it creates. However, before the kernel is started by the osKernelInitialize() function, the main stack size that is configured in the file startup_device.s is used.
Main stack is also used when:
Name | #define | Description |
---|---|---|
Number of concurrent running user threads | OS_TASKCNT | Indicates the maximum number of threads that will run at the same time (including main). |
Default Thread stack size [bytes] | OS_STKSIZE | Specifies the default stack size (in words) for threads that are defined with osThreadDef stacksz = 0. |
Main Thread stack size [bytes] | OS_MAINSTKSIZE | Is the stack requirement (in words) for the main function that is started by default as an RTOS thread. |
Number of threads with user-provided stack size | OS_PRIVCNT | Indicates the number of threads that are defined with osThreadDef stacksz != 0 (excluding main). stacksz specifies the stack size requirement of that thread. |
Total stack size [bytes] for threads with user-provided stack size | OS_PRIVSTKSIZE | Is the combined stack requirement (in words) of all threads that are defined with with osThreadDef stacksz != 0 (excluding main). |
Stack Overflow Checking | OS_STKCHECK | If a stack overflow is detected at a thread switch, the function os_error with error code = 1 is called. By default, this function is implemented as endless loop and will practically stop code execution. |
Stack Usage Watermark | OS_STKINIT | Initializes the thread stack with a watermark pattern that can be used to determine the maximum stack usage within each thread. |
Processor Mode for Thread Execution | OS_RUNPRIV | Controls the processor mode (privileged/unprivileged) |
CMSIS-RTOS RTX implements a software stack overflow checking that traps stack overruns. Stack is used for return addresses and automatic variables and extensive usage or incorrect stack configuration may cause a stack overflow. Software stack overflow checking is controlled with the #define OS_STKCHECK.
If a stack overflow is detected, the function os_error with error code = 1 is called. By default, this function is implemented as endless loop and will practically stop code execution.
The total stack size of an application needs to be as small as possible in a memory restricted embedded system. To be able to set the smallest stack size for every thread, the developer needs to know the maximum stack usage over the runtime of the application.
The Stack Usage Watermark feature support this by initializing the 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.
Stack usage watermark is controlled with the #define OS_STKINIT. Setting this #define
increases significantly the execution time of osThreadCreate (depending on thread stack size).
CMSIS-RTOS RTX allows to execute threads in unprivileged or privileged processor mode. The processor mode is controlled with the #define OS_RUNPRIV.
In unprivileged processor mode, the software:
In privileged processor mode the software can use all the instructions and has access to all resources.