当前位置:网站首页>STM32 and FreeRTOS stack parsing
STM32 and FreeRTOS stack parsing
2022-04-23 09:09:00 【NULL_ one thousand nine hundred and sixty-nine】
before mcu I didn't pay much attention to the stack when programming , Just know that you need to set the stack a little larger . Now gradually use freertos, stay freertos There are also stack settings in ,freertos What is the relationship between the stack of and the stack in the startup file ? For future use , Make these clear at one time this time .
1、 Definition
A stack is a specific storage area or register . Generally, an area is always opened up in memory as a stack , It's called the software stack ; A stack of registers , It's called hardware stack . Most of the time , We all use software stacks .

stay stm perhaps gd32 The stack in the startup file is the software stack .
The storage of data in the stack , We should follow the principle of first in and last out , It can be compared to a barrel , The data put in first is at the bottom of the bucket , The data put in after is on the top of the bucket . When fetching data, first fetch data at the top of the bucket , Take the data from the bottom of the bucket .
Single chip microcomputer application , The stack is a special storage area , The stack belongs to RAM Part of the space , The stack is used for function calls 、 Save and restore field data when switching is interrupted . Objects in the stack have a property : The first object put into the stack is always taken out last , This feature is often referred to as first in and last out (FILO—First-In/Last-Out). Some operations are defined in the stack , The two most important things are PUSH and POP. PUSH( Push ) operation : Stack pointer (SP) Add 1, Then add a... At the top of the stack Elements .POP( Out of the stack ) The operation is the opposite , When you get out of the stack, first put SP Indicated internal ram The contents of the cell are sent to the directly addressable cell ( Destination location ), Then put the stack pointer (SP) reduce 1. These two operations realize the insertion and deletion of data items .
So much theoretical knowledge about stack , Part of it comes from Baidu Encyclopedia . stay mcu In, we only need to know that the stack set in the above figure is actually located in mcu Of ram It's OK . stay mcu Programming , Seems right ram I don't pay much attention to , It's more about flash The capacity of ,falsh Can the size be stored? My firmware and so on . Yes ram I didn't know there was this thing , But I really didn't pay close attention .
2、 Space allocation of stack
stay mcu in ,heap and stack Users are different .
stack( Stack ): Automatically allocated and released by the system , Parameter values of stored functions , The value of a local variable . This space cannot be operated by users .
heap( Pile up ): Assigned and released by users , That is to call malloc and free, The operation space is heap space .
On the Atomic Forum , Suggest , If you don't call the system malloc and free function ,heap Space can be set to 0.
In terms of space allocation , The best understanding is heap, Completely user operated . What's more difficult to understand is stack, So how does the system use stack space .
3、stm32 Stack space and compiled firmware size
use keil After the compilation project is completed ,output The window will display the following information :

In the picture above code ro-data rw-data zi-data What does that mean? , Which are the stations ram Space , Which are the stations flash Space ? In the above, we said stack It is automatically assigned by the system , Then I'll put the... In the startup file stack_size Change it , Take another look at the compilation output .

take stack_size from 0x2000 Change it to 0x1000, Yes 8k Byte to 4k byte . The compiled result is shown in the figure below .

Compared with the last compilation, it is found that , Only zi-data The paragraph changed by 10508 Turned into 6412. The difference between the two is just 4096, Namely stack_size The reduced value of .
Will be heap_size Set to 0x1000, It is found that there is no change in the compiled .
3.1 Storing data segments
Code Is the space occupied by the code ;
RO-data yes Read Only The size of the read-only constant , Such as const type ;
RW-data yes (Read Write) The size of the initialized read-write variable ;
ZI-data yes (Zero Initialize) The size of uninitialized read-write variables .
Final , When burning flash The occupied space is :
falsh = Code + RO-Data + RW-Data
Program runtime ram The occupied space is
** ram = RW-Data + ZI-Data**
Calculate falsh Occupy space = 12568+ 368+ 180 = 13116 byte

Look at the final generated bin The file happens to be 13116 Byte size .
eureka flash Occupied space , Let's see ram Occupied space .ram Take up space in the generated .map In file .

map The file shows ram The amount of size by 0x19c0, Convert to 10 Into the system for 6592, And generate RW-Data + ZI-Data=180+6412=6592. This has also been verified .
According to the above figure, we can also get :
** RW-Data= .data** data The space accumulation of is RW-Data
** ZI-Data= .bss+STACK** .bss Space accumulation +STACK That is to say ZI-Data
It can also be seen in the figure above STACK=0x1000 Just the value set in the startup file . It can also be seen that the current chip ram The total space is 0x18000=96kB.
Here we basically sort out ram and falsh The composition of space . So let's see freertos What is your stack .
3.2freertos Stack
I'm using freertos yes v10 edition , Memory allocation is based on heap4.c
stay freertos Of FreeRTOSConfig.h in , You need to set the size of the heap
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 1 * 1024 ) )
For the test here, I only set it to 1024 byte . Global search configTOTAL_HEAP_SIZE , The discovery is in heap_4.c Used in .

According to the current settings, you can see ,freertos Is to directly define a static array hcHeap The size is configTOTAL_HEAP_SIZE.
configAPPLICATION_ALLOCATED_HEAP This macro definition generally sets the heap to external sram Will be used . I won't talk more here for the time being .
It can be concluded from the above figure , By default ,freertos The heap is a large custom array , And the settings in the startup file heap_size It doesn't matter . So as long as there is no call to the system malloc function , Startup file heap_size It can really be set to 0.
Next , take configTOTAL_HEAP_SIZE Change to 20Kb Compile . give the result as follows

zi-data from 6142 Turned into 25868, The difference between the two is just 19kB. The compiled map The contents of the document are as follows :

Compared with the last time ,.bss Part of the heap_4.o The space occupied by 0x400(1kB) Turned into 0x5000(20kB), just configTOTAL_HEAP_SIZE Macro definition sets the size .
4、 Conclusion
After the above experimental analysis, we can draw the following conclusions :
- When freertos use heap_4 Memory allocation scheme ,stm32 In the startup file stack_size and heap_size And freertos The heap size set in has nothing to do with .
- As long as the system is not used in the code malloc function , Startup file heap_size It can be set to 0
- mcu Runtime ram Space = RW-Data+ZI-Data+ In the startup file heap_size, So you can set according to this formula freertos The size of the heap .freertos The heap should be set as large as possible .
5、freertos Heap common functions heap_4

The picture above is from freertos Official documents of , It can be seen that freertos The stack is actually put into heap space . Task control block TCB、queue 、pvPortMalloc And other uses are heap space , The size of the space has configTOTAL_HEAP_SIZE decision .
5.1 Commonly used heap Correlation function
Get the rest heap The size .
size_t xPortGetFreeHeapSize( void );
Get the minimum unallocated space size
size_t xPortGetMinimumEverFreeHeapSize( void );
Dynamic memory allocation and release function
void * pvPortMalloc( size_t xWantedSize )
void vPortFree( void * pv )
5.2 The amount of space occupied by the task stack .
freertos Stack space should be set when creating a new task in . Stack space can be in FreeRTOSConfig.h Set a minimum value in
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 128 ) // Set the minimum stack space to 256 byte
Remember that the size unit set here and that set when the task is created is not bytes , But words , Word occupation 4 Bytes , Therefore, the minimum stack space set above is 256 byte . As can be seen from the above ,freertos The stack space is actually in the heap .
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
const configSTACK_DEPTH_TYPE usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t * const pxCreatedTask )
pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) )
#define pvPortMallocStack pvPortMalloc
In the new task function , You can see , Actually use pvPortMalloc The allocated space is used as stack space , The size is usStackDepth *sizeof( StackType_t ) . and StackType_t The size is actually uint32_t by 4 byte .

版权声明
本文为[NULL_ one thousand nine hundred and sixty-nine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230717262964.html
边栏推荐
- Pctp test experience sharing
- Idea package jar file
- Research purpose, construction goal, construction significance, technological innovation, technological effect
- Concave hull acquisition method based on convex hull of point cloud
- MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
- The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
- Trc20 fund collection solution based on thinkphp5 version
- Wechat: get the owner of a single tag
- [original] use system Text. JSON formats the JSON string
- MYCAT configuration
猜你喜欢

First principle mind map

Multi view depth estimation by fusing single view depth probability with multi view geometry

资源打包关系依赖树

Cadence process angle simulation, Monte Carlo simulation, PSRR

npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’

Download and install bashdb

Matlab draw five-star red flag

2021 Li Hongyi's adaptive learning rate of machine learning

Leetcode-199 - right view of binary tree

Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
随机推荐
[SQL Server fast track] view and cursor of database
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
The most concerned occupations after 00: civil servants ranked second. What was the first?
Valgrind and kcache grind use run analysis
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
Complete binary search tree (30 points)
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
valgrind和kcachegrind使用運行分析
valgrind和kcachegrind使用运行分析
112. 路径总和
BK3633 规格书
小女孩行走
SAP 101K 411k inventory change
Non duplicate data values of two MySQL query tables
npm ERR! network
Group Backpack
Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
资源打包关系依赖树
Concave hull acquisition method based on convex hull of point cloud
共享办公室,提升入驻体验