没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Determining the required stack sizes for a software project is a crucial part of the development process. The developer aims to create a stable application, while not wasting resources. This application note explains methods that help finding the optimal setting while looking specifically on the stack load caused by interrupt service routines (ISRs) in RTOS applications running on an Arm Cortex-M based processor. Stacks are memory regions where data is added or removed in a last-in-first-out (LIFO) manner. In an RTOS, each thread has a separate memory region for its stack. During function execution, data may be added on top of the stack; when the function exits, it removes that data from the stack.
资源推荐
资源详情
资源评论



























Determining the stack usage of applications
AN 316, Spring 2019, V 1.1
AN316 – Determining the stack usage of applications Copyright © 2019 Arm Ltd. All rights reserved
www.keil.com/appnotes/docs/apnt_316.asp
1
Abstract
Determining the required stack sizes for a software project is a crucial part of the development process. The
developer aims to create a stable application, while not wasting resources. This application note explains
methods that help finding the optimal setting while looking specifically on the stack load caused by interrupt
service routines (ISRs) in RTOS applications running on an Arm Cortex-M based processor.
Contents
Abstract ......................................................................................................................................................................1
Introduction ................................................................................................................................................................2
Usage of Stack Memory ..............................................................................................................................................2
Stack usage of Interrupt Service Routines ..............................................................................................................3
Memory requirement for automatic register stacking .......................................................................................3
Stack usage of the RTX5 Kernel ..............................................................................................................................4
Analysis of Stack Usage ..............................................................................................................................................5
Static analysis ..........................................................................................................................................................5
Dynamic analysis .....................................................................................................................................................6
Thread stack watermarking.................................................................................................................................6
Main stack watermarking ....................................................................................................................................7
Calculate and configure stack usage ..........................................................................................................................9
Thread stacks ..........................................................................................................................................................9
Main stack ............................................................................................................................................................ 10
Example: AN316.uvprojx ......................................................................................................................................... 11
Thread stack usage .............................................................................................................................................. 11
Dynamic stack analysis ..................................................................................................................................... 11
Static analysis ................................................................................................................................................... 11
Configure thread stacks ................................................................................................................................... 12
Main stack usage ................................................................................................................................................. 12
Static analysis ................................................................................................................................................... 12
Calculate main stack size .................................................................................................................................. 13
Summary .................................................................................................................................................................. 13
References ............................................................................................................................................................... 14

AN316 – Determining the stack usage of applications Copyright © 2019 Arm Ltd. All rights reserved
www.keil.com/appnotes/docs/apnt_318.asp
2
Introduction
Stacks are memory regions where data is added or removed in a last-in-first-out (LIFO) manner. In an RTOS,
each thread has a separate memory region for its stack. During function execution, data may be added on top of
the stack; when the function exits, it removes that data from the stack.
In a Cortex-M processor system, two stack memory regions need to be considered:
• The system stack is used before the RTOS kernel starts and by interrupt service routines (ISRs). It is
addressed via the Main Stack Pointer (MSP).
• The thread stack(s) are used by running RTOS threads and are addressed via the Process Stack Pointer
(PSP).
As the memory region for stack is constrained in size, allocating more memory on the stack than is available, can
result in a program crash or stack overflow. In embedded systems, the timing of external program events
influences the program flow and a stack size issue may create infrequent, sporadic program errors. It is
therefore critical to understand the stack memory requirements of an application.
For calculating (and therefore optimizing) the required stack memory size, the following methods may be used:
• Static analysis (using call tree analysis) is performed at build time (by a linker for example).
• Dynamic analysis (using stack watermarking) is performed at run-time (in a debug session for example).
Usage of Stack Memory
In an embedded application, the stack memory is typically used in the following constructs:
• On function calls to save register content (such as the link register (LR) for the return address)
• Local function variables are stored on the stack when no CPU registers are available.
• For interrupt service execution, the register frames are store on the stack.
The application programmer may influence the stack memory usage with for following techniques:
• For arrays, allocate space from memory pools instead of local function variables.
• Reduce the potential interrupt nesting by choosing the right number of interrupt priority levels.
• Simplify the function call nesting. However, as this impacts the program readability, there is a balance.
Also, modern compiler optimizations perform automatic function in-lining and therefore function call
nesting is less important.
The picture below shows the stack usage of an embedded application that is using an RTOS kernel. ISRs use the
main stack, a thread uses the thread stack whereby each thread has its own stack space that is managed by the
RTOS kernel. Each thread stack should reserve additional memory that is required for “thread context
switching”. The memory required for “thread context switching” depends on the usage of the floating-point
unit (FPU):
• without FPU: 64 bytes (to save R0..R12, LR, PC, xPSR)
• with FPU: 200 bytes (to save S0..S31, FPSCR, R0..R12, LR, PC, xPSR)
Optionally, an RTOS stores an “overflow protect pattern” (which is a fixed value) at the stack bottom which is
used by the kernel to check for stack overflows.

AN316 – Determining the stack usage of applications Copyright © 2019 Arm Ltd. All rights reserved
www.keil.com/appnotes/docs/apnt_318.asp
3
Note that RTX5 itself executes in handler mode and uses the main stack for kernel functions. This is different
from other RTOS kernels (i.e. FreeRTOS), where the kernel functions use the thread stack and therefore require
additional memory space on each individual thread stack.
Stack usage of Interrupt Service Routines
Interrupt service routines run when an exception has occurred and use the main stack. They are triggered by a
peripheral, hardware fault, or by software with the Service Call (SVC) instruction. For interrupt service routines,
the processor does automatic register stacking on the current active stack: when thread stack is active, PSP is
used, otherwise MSP.
Memory requirement for automatic register stacking
The memory required for automatic register stacking depends on the actual stack alignment and the usage of
the floating-point registers of the program code that is interrupted. The usage of the floating-point registers is
indicated by the processor in CONTROL register - FPCA bit (bit 2):
• When CONTROL – bit 2 = 0: automatic register stacking uses 32 bytes (+ 4 bytes aligner)
• When CONTROL – bit 2 = 1: automatic register stacking uses 104 bytes (+ 4 bytes aligner)
NOTES
• For Cortex-M processors without hardware FPU (Cortex-M0/M0+/M3/M23) always use 32 bytes for
automatic register stacking.
• For Cortex-M processors with hardware FPU, it might be complex to analyze the floating-point register
usage of the various threads and ISRs. In this case, always use 104 bytes for automatic register stacking.
Interrupt service routines can be nested due to preemption of interrupts or exceptions. Cortex-M processors
have the following configurations that influence the maximum nesting:
• Each interrupt source has a priority register, whereby lower values indicate higher priority.
• The AIRCR (Application Interrupt and Reset Control Register) contains a PRIGROUP field that defines the
split of the priority register into a group priority and sub-priority within the group. Only a lower group
priority value can preempt code execution.
• Some exceptions have a fixed priority which is typically higher than other interrupt sources.
To consider the interrupt nesting the maximum depth of the stack loads must be added.
剩余13页未读,继续阅读
资源评论


MichaelNZ
- 粉丝: 6
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 光伏并网逆变器闭环控制及MPPT技术解析:基于MatlabSimulinkPLECS的锁相环、LC滤波与MPPT控制
- 西门子S7-1200 PLC Modbus RTU 485通讯主站程序设计与实现 · Modbus RTU
- 基于COMSOL仿真平台的石蜡加热熔化多物理场耦合仿真:模拟温度和流场变化的案例分析
- Tiomehsh-datasheet-ai-analyzer-30136-1755665338791.zip
- 光伏储能微电网中Boost电路MPPT与双向Buck-Boost变换器对直流母线电压的精确调控 · SVPWM 详解
- 96三相多功能表:全套电路与源码详解,高精度实时电力参数测量与分析系统
- 电力系统中有源电力滤波器(APF)的仿真验证及其直流电压控制研究 · 电力系统
- 分别使用OpenCV、ONNXRuntime部署多任务的yolov5目标检测+语义分割,包含C++和Python两个版本的程序
- 三菱电梯主板参数详解:楼层显示、基站设置等关键功能调整 权威版
- 天鹰优化算法AO优化核极限学习机KELM参数的多输入单输出拟合预测建模的Matlab程序
- 网络空间安全导论2023学习项目-涵盖网络安全基础密码学原理恶意软件分析入侵检测技术数据保护策略安全协议设计风险评估方法应急响应流程法律法规遵循安全意识培养攻防.zip
- 基于IEEE33节点的风光储配电网优化调度仿真——经济成本最小化与潮流计算协同优化 · 分布式电源 手册
- 西门子PLC 12001500系列模拟量滤波技术解析:应对数据扰动的一阶惯性、蠕动平均值与中位值滤波
- 三相逆变器双闭环控制MATLAB Simulink模型设计:基于外环PR与内环比例控制策略的参数整定及建模原理参考
- 网络安全社区公开知识爬取工具-先知社区安全文章与漏洞分析报告自动化抓取与本地存储-实现高效知识管理与离线搜索功能-基于Selenium与BeautifulSoup4的自动化爬虫技术.zip
- YOLOv5 v6.2实例分割在LabVIEW工业应用中的高效推理与性能分析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
