The osFeature flags in cmsis_os.h allow developers to configure and customize the CMSIS-RTOS API for their specific application requirements. Similarly, Keil RTX provides configuration options to enable or disable certain RTOS features. By setting these configuration options, developers can optimize Keil RTX and reduce memory footprint for resource constrained devices.
Keil RTX Configuration
Keil RTX is configured via a header file called RTX_Config.h. This file contains various macro definitions that control the inclusion of RTX features and modules. Some key configuration options are:
- osFeature_xxx – Control RTOS feature inclusion
- osRtxConfig.thread_num – Number of concurrent threads
- osRtxConfig.timer_num – Number of concurrent timers
- osRtxConfig.event_num – Number of concurrent events
- osRtxConfig.mutex_num – Number of concurrent mutexes
- osRtxConfig.semaphore_num – Number of concurrent semaphores
- osRtxConfig.memory_pool_num – Number of memory pools
- osRtxConfig.messagequeue_num – Number of message queues
The osFeature flags allow inclusion of specific RTOS services and features in Keil RTX. These are similar to the CMSIS-RTOS osFeature flags. Some common osFeature flags are:
- osFeature_MainThread – Main thread support
- osFeature_Pool – Memory pool allocation support
- osFeature_MailQ – Mail queue support
- osFeature_MessageQ – Message queue support
- osFeature_Signals – Signal support
- osFeature_Semaphore – Semaphore support
- osFeature_Wait – Wait API support
- osFeature_SysTick – SysTick timer support
For example, to disable semaphores in Keil RTX: #define osFeature_Semaphore (0)
This removes any code related to semaphores from the RTX library, reducing code size. The developer must ensure no semaphores are used in the application when this option is disabled.
Proper configuration of Keil RTX allows optimization of the RTOS footprint for each application. Here are some techniques for optimization:
- Set thread_num, timer_num, event_num etc. to exact values needed, don’t use defaults.
- Disable any osFeature flags for RTOS services not used by the application.
- Disable osFeature_SysTick if you have a custom system timer.
- Disable osFeature_Signals if you are not using signals.
- Disable xx_num options like messagequeue_num if not using that RTOS object.
As an example, a simple application may only need 2 threads, 1 mutex, and no other objects. The configuration could be: #define osRtxConfig.thread_num 2 #define osRtxConfig.mutex_num 1 #define osFeature_Signals 0 #define osFeature_Semaphore 0 #define osFeature_MessageQ 0
This eliminates all unused components and allocators, reducing RAM usage. The developer must take care not to use any disabled RTOS services in the application code.
When memory usage is a critical constraint, additional techniques can optimize Keil RTX memory:
- Use static memory allocators rather than dynamic memory pools.
- Place RTX objects in specific memory regions using the SECTION attribute.
- Reduce thread stack sizes to exactly what is needed.
- Use the Small Memory Model configuration for limited RAM.
The Small Memory Model configures RTX for constrained devices. It disables dynamic memory allocation and configures static memory with reduced sizes for kernel objects. For example: #define OS_APP_DATA_SIZE 32 #define OS_TASK_CNT 2 #define OS_STACK_SIZE 128
This configures RTX for just 2 threads with small 128 byte stacks. The OS_APP_DATA_SIZE defines kernel object memory.
Besides memory usage, Keil RTX can also be optimized for performance:
- Increase thread_num to allow more concurrent threads.
- Use larger stacks if threads do more work.
- Increase osRtxConfig.timer_num for more active timers.
- Enable round-robin scheduling for shorter thread waits.
However, larger data structures will increase memory usage. A balance must be found between performance and memory constraints.
Advanced optimizations can configure the RTX scheduler and tick interval. But this requires in-depth knowledge of the internals. For most applications, the default scheduler offers a good balance.
Keil RTX provides extensive configuration options to customize the RTOS for your specific application requirements. Optimizing the configurations allows reducing memory usage while still maintaining the required performance. Profiling the application can identify unnecessary components that can be disabled. With prudent configuration, Keil RTX can provide an efficient RTOS even in memory constrained devices.