Combine Keil MDK to analyze MCU stack space

Follow + star official account , don't miss exciting content

7ae31126f232451547d17e837715bca2.gif

Author | strongerHuang

WeChat public account | strongerHuang

The stack is very important to the program, the program can run quickly, the stack plays a very important role, but do you understand the stack?

overview

We all know that the stack is located in RAM. Now the RAM of MCU is relatively large (tens to hundreds of K), so the allocated stack is also large enough, and many people don't pay much attention to the size of this stack.

However, in the past, the RAM of the MCU was relatively small, even less than 1K, so the previous engineers were more concerned about the size of the stack.

For small projects, maybe we don't care about the stack size.

However, if the project is large, you should pay attention to it. If your stack size setting is unreasonable, it may cause a Fault .

If you want to know how big the stack is, you need to understand the function of the stack. Let's learn more about the stack.

About the stack

Let's take a look at two classic knowledge first.

1. Program memory allocation

The memory occupied by a program compiled by C/C is divided into the following parts:

Stack area (stack): automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. It operates like a stack in a data structure.

Heap area (heap): Generally allocated and released by the programmer, if the programmer does not release it, it may be reclaimed by the OS when the program ends. Note that it is different from the heap in the data structure, and the allocation method is similar to a linked list.

Global area (static area) (static): The storage of global variables and static variables is put together, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in adjacent an area. Released by the system after the program ends.

Text constant area: the constant string is placed here, and will be released by the system after the program ends.

Program code area: store the binary code of the function body.

2. Classic example program

int a = 0;              //全局初始化区
char *p1;               //全局未初始化区
main()
{
  int b;                //栈
  char s[] = "abc";     //栈
  char *p2;             //栈
  char *p3 = "123456";  //123456\0在常量区,p3在栈上。
  static int c =0;//全局(静态)初始化区
  p1 = (char *)malloc(10);
  p2 = (char *)malloc(20);   //分配得来得10和20字节的区域就在堆区。
  strcpy(p1, "123456");      //123456\0放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。
}

microcontroller stack

From the above description, we can see how the heap and stack are occupied in the code.

Many people may still not be able to understand, here is a description of the stack-related content in the development process of STM32.

1. How to set the stack size of STM32?

This question is in the article " What is the startup process of STM32?" "It describes the method of setting the stack size in MDK-ARM, IAR EWARM , and using STM32CubeMX .

2. Stack

STM32F1 default setting value is 0x400, which is 1K size.

Stack_Size    EQU     0x400

Local variables in the function body:

void Fun(void)
{
  char i;
  int Tmp[256];
  //...
}

Local variables occupy a total of 256*4 + 1 bytes of stack space.

Therefore, when there are many local variables in the function , we need to pay attention to whether the stack size we configure is exceeded.

Function parameters:

void HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init)

Here is a point to emphasize: passing a pointer only occupies 4 bytes. If a structure is passed, it will take up space of the size of the structure .

Tip: When functions are nested and recursive, the system will still occupy stack space.

3. Heap

Heap_Size      EQU     0x200

The default setting is 0x200 (512) bytes.

Most of us should rarely use malloc to allocate heap space.

Although the data on the heap can always be accessed as long as the programmer does not release the space, if he forgets to release the heap memory, it will cause memory leaks and even fatal potential errors.

Combining with Keil to analyze the size of RAM usage

People who often debug online may analyze some underlying content. Here we combine MDK-ARM to analyze the problem of RAM occupation size.

After MDK compilation, there will be a piece of RAM size information:

307255dfb00e50ef9adbd3df77f36604.png

This size is 0x668, when debugging, it will appear:

1ce1354b4d74d1cb9c488d80522ada8a.jpeg

This MSP is the main stack pointer . Generally, the position we point to after reset is actually the top of the stack:

70986636c30771adb503ff2fb4adb6ff.jpeg

The MSP points to the address 0x20000668, which is obtained by offsetting 0x668 from 0x20000000.

For specific places that occupy RAM, you can refer to the content at [Image Symbol Table] in the map file:

52f31ec65b5c5f8ac253947c6e7f216c.jpeg

Of course, for detailed analysis of map files, you can see my series of tutorials " Comprehensive Analysis of Keil Series Tutorials 12_map Files ".

About the stack, there is actually a lot of knowledge that can be expanded, such as: stacking, popping, upward and downward growth methods, big and small endian, etc. If you are interested, you can go online to learn about it.

------------ END ------------

8420b22ae793a0924dc5d0ef5955c5aa.gif

●Column "Embedded Tools "

●Column "Embedded Development"

●Column "Keil Tutorial"

●Selected tutorials in the embedded column

Pay attention to the official account and reply " Jiagroup " to join the technical exchange group according to the rules, and reply " 1024 " to view more content.

605771746b8b539bd8e99a3c77657eee.jpeg

b4ae7a0b6a4484072678b358b4cbba68.png

Click " Read the original text " to view more sharing.

Guess you like

Origin blog.csdn.net/ybhuangfugui/article/details/132439750