SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Configuration and optimization of threads and stacks for Keil RTX and Cortex-M0+
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

Configuration and optimization of threads and stacks for Keil RTX and Cortex-M0+

Andrew Irwin
Last updated: October 5, 2023 9:58 am
Andrew Irwin 5 Min Read
Share
SHARE

Configuring and optimizing threads and stacks for the Keil RTX real-time operating system (RTOS) when using the ARM Cortex-M0+ microcontroller can improve performance and resource utilization. The key considerations are configuring the number of threads, stack size for each thread, kernel tick frequency, thread priorities, and utilizing stack watermarking.

Contents
Overview of Keil RTX and Cortex-M0+Configuring Number of ThreadsConfiguring Stack Size for ThreadsKernel Tick FrequencyThread PrioritiesOptimizing with Stack WatermarkingConclusion

Overview of Keil RTX and Cortex-M0+

The Keil RTX RTOS is designed for ARM Cortex-M microcontrollers. It provides preemptive multi-threading with configurable scheduling policies. The Cortex-M0+ is one of the smaller and more resource constrained Cortex-M variants, so optimizing RTX configuration is especially important.

Key features of RTX on Cortex-M0+:

  • Supports up to 31 threads
  • Fixed priority preemptive scheduling
  • optionally utilizes CMSIS-RTOS API
  • Small memory footprint starting around 1.5KB

Configuring Number of Threads

The maximum number of threads supported is 31. However, utilizing fewer threads can reduce memory usage and context switching overhead. The optimal number depends on the application requirements.

Considerations for number of threads:

  • Memory usage – each thread requires a stack and kernel control block
  • Context switching overhead – more threads means more frequent switching
  • Logical separation – separate threads for logically distinct tasks
  • Priority levels needed – fixed priority scheduling limits priority levels to number of threads
  • Performance goals – more threads can allow greater parallelism

For many applications, 4-8 threads provide a good balance. Critical tasks can be separated while minimizing overhead.

Configuring Stack Size for Threads

The stack size for each thread must be configured based on the thread’s requirements. The factors to consider are:

  • Local variables used by the thread
  • Nested function calls within the thread
  • Interrupt handler requirements if thread utilizes interrupt
  • OS overhead for context switching and kernel calls

In general, starting with a 512-1024 byte stack and increasing based on testing is recommended. The Cortex-M0+ has limited RAM so using larger stacks than required wastes memory.

RTX provides stack watermarking which can track the maximum stack usage for tuning. The watermark stack size in RTX config should be set to the initial stack size guesses.

Kernel Tick Frequency

The RTX kernel tick interval controls context switching between threads. It is triggered by a periodic timer or hardware interrupt. The tick frequency should be high enough to meet application responsiveness needs.

Guidelines for kernel tick frequency:

  • 10-100Hz typical range
  • Higher for faster thread switching
  • Must be higher than longest task duration
  • Higher frequency increases CPU load

Starting at 100Hz tick and reducing based on application performance is recommended. The tick frequency sets the minimum thread time slice.

Thread Priorities

RTX uses fixed priority preemptive scheduling. Higher priority threads always run before lower priority threads. Matching thread priorities to meet application requirements is important.

Guidelines for assigning thread priorities:

  • Most critical tasks get highest priority
  • Prioritize real-time tasks over non-real-time
  • Assign similar priorities to threads with similar criticality
  • Spread priorities over range for flexibility
  • Consider priority inversion avoidance

Starting with priority levels matched to logical importance and then tuning based on performance is recommended.

Optimizing with Stack Watermarking

The RTX stack watermarking feature tracks the maximum stack usage for each thread. It can be enabled in the RTX configuration.

Use stack watermarking to:

  • Identify initial stack size guesses
  • Tune stack sizes to minimize waste
  • Detect stack overflows during testing

Monitoring stack usage over typical program execution and then setting stack sizes slightly above watermarks will optimize memory usage.

Conclusion

Optimizing RTX configuration and threads is key to maximizing performance on resource constrained Cortex-M0+ MCUs. Adjusting the number of threads, stack sizes, kernel tick rate, thread priorities and utilizing stack watermarking based on profiling and testing can enable efficient utilization of the Cortex-M0+ capabilities.

With careful configuration, the RTX RTOS can provide robust multi-threading on the Cortex-M0+ to meet requirements for responsiveness, real-time performance, and logical task separation. The flexibility of RTX makes it suitable for a wide range of Cortex-M0+ applications.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Using Keil RTX RTOS on Cortex-M0+
Next Article Differences between osDelay() and osWait() calls in Keil RTX
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

Do I Need to Run a Separate Flash Programmer Software for Custom SOC with Cortex M0?

If you are working with a custom system-on-chip (SOC) that…

10 Min Read

Implementing Cortex-M0 DesignStart on an Altera FPGA

Implementing an ARM Cortex-M0 processor on an FPGA can be…

8 Min Read

What is the bus interface in the Cortex-M3 processor?

The bus interface in the Cortex-M3 processor provides the connection…

8 Min Read

Tips on implementing Cortex-M1 on Artix-7 and Spartan-7

Implementing the Cortex-M1 CPU on Artix-7 and Spartan-7 FPGAs can…

5 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account