SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Determining Stack Requirements When Using an RTOS on Arm Cortex-M
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

Determining Stack Requirements When Using an RTOS on Arm Cortex-M

David Moore
Last updated: October 5, 2023 9:55 am
David Moore 5 Min Read
Share
SHARE

When developing embedded systems using an RTOS (real-time operating system) on an Arm Cortex-M processor, properly determining the stack requirements for each task is crucial for avoiding stack overflows and system crashes. The key factors that influence stack usage are the RTOS itself, the Cortex-M core, and the application code. By analyzing these elements, stack sizes can be calculated to provide ample space for stack-intensive operations.

Contents
Stack Usage of the RTOS KernelImpact of Cortex-M Core ArchitectureStack Requirements of Application CodeCalculating Worst-Case Stack UsageConfiguring RTOS Task StacksStack Overflow DetectionOptimizing Stack UsageConclusion

Stack Usage of the RTOS Kernel

The RTOS kernel utilizes the stack for its own operations like context switching between tasks. The amount of stack an RTOS consumes depends on the specific RTOS and its configuration. For example, FreeRTOS recommends a minimum stack size of 128 bytes for each task when using its default configuration. However, kernel stacks can be significantly larger with components like inter-task communication and stream buffers enabled. Checking the RTOS documentation for guidelines is crucial.

Impact of Cortex-M Core Architecture

The Cortex-M core architecture influences stack usage due to exception handling and context switching mechanisms. For example, Cortex-M3 and Cortex-M4 cores push up to 16 registers onto the current task’s stack when an exception occurs. And during context switches, the core automatically saves the necessary registers of the outgoing task. Therefore, the specific Cortex-M core and how the RTOS utilizes it will impact the stack.

Stack Requirements of Application Code

Beyond the RTOS and processor, the key determinant of stack requirements comes from the application code itself. Functions that declare large arrays, strings, or other objects on the stack can quickly consume space. Calls to other functions also increase usage. Code with multiple layers of nested function calls, recursion, or complex data manipulations require larger stack allocations per task. Analyzing the call graph and complexity is important.

Calculating Worst-Case Stack Usage

With knowledge of the RTOS, processor, and application code, maximum potential per-task stack usage can be calculated. One technique is to allocate variables in each task’s context that would mimic worst-case memory usage. The stack pointer can then be sampled before and after task execution to gauge the max usage. An additional 20-30% buffer on top of this measured worst-case is recommended.

Configuring RTOS Task Stacks

With stack requirements determined, the RTOS can be configured. Many RTOSs specify stack sizes in bytes during task creation. While tasks can share a stack, isolation per task works best. The linker script can contain stack sections for each task. Total stack usage across all tasks should fit within available SRAM. If stacks overflow during runtime, increasing the allocated sizes may be necessary.

Stack Overflow Detection

To detect stack overflows during runtime, stack canaries or MPU regions can be used. A known value is placed at the end of each task stack which gets overwritten if the stack overflows. The RTOS checks this value and flags overflows. Alternatively, the MPU can set a protected region at the bottom of each stack to trigger exceptions upon overflow. These mechanisms allow early detection and diagnosis of insufficient stack space.

Optimizing Stack Usage

If stack usage is too high, optimizations can reduce requirements. Minimizing unnecessary local variables, inlining functions instead of calling them, simplifying nested code, and eliminating recursion are good ways to trim stack usage. Choosing the optimal compiler optimization settings balances code size and stack usage. RTOS features like inter-task communication may need to be disabled if stacks are overflowing. Prioritizing stack reduction can enable an application to fit into smaller Cortex-M cores.

Conclusion

Determining accurate stack size requirements is necessary to create stable RTOS-based applications on Arm Cortex-M processors. By understanding the stack usage profiles of the RTOS kernel, Cortex-M core, and application code, maximum per-task stack heights can be calculated. Adding overflow detection and optimizations can further improve stack usage. Proper stack management ensures Cortex-M applications leveraging an RTOS avoid crashes and operate reliably.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Calculating Maximum Stack Usage for Bare Metal Arm Cortex-M Apps
Next Article Utilizing Dual Stack Pointers (MSP and PSP) Without an RTOS
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

ARM Cortex-M4 Memory Map

The ARM Cortex-M4 is a 32-bit processor core used in…

8 Min Read

Alternatives to uVision5 for Developing Cortex-M23 Projects

The Cortex-M23 is part of ARM's Cortex-M processor family, designed…

6 Min Read

What Is the Difference Between Arm Cortex-M4 and M33?

The key differences between Arm Cortex-M4 and Cortex-M33 microcontrollers are…

12 Min Read

What is the debug subsystem in arm?

The debug subsystem in ARM processors provides hardware support for…

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

Sign in to your account