Explain the stack of STM32 microcontroller in detail
You are here:Home » News » Explain the stack of STM32 microcontroller in detail
Explain the stack of STM32 microcontroller in detail
Views: 9871 Author: The copyright belongs to the original author, if there is any infringement, please contact to delete it. Publish Time: 2022-01-20 Origin: internet
When learning STM32 microcontroller, you can always encounter the concept of "stack". Share this article, I hope it will help you understand the stack.
For those who know a little bit of assembly programming, you can know that the stack is a contiguous storage area in memory that is used to save some temporary data. The stack operation is completed by the PUSH and POP instructions. The program memory can be divided into several areas:
stack area
Heap
Global area (static)
Text always bright area program code area
After the program is compiled, global variables and static variables have allocated memory space. When the function is running, the program needs to allocate stack space for local variables. When the interrupt comes, it also needs to push the function pointer into the stack to protect the scene so that the interrupt is processed. Then go back to the previously executed function. The stack is allocated from high to low, and the heap is allocated from low to high. The difference between stacks in ordinary microcontrollers and STM32 microcontrollers When an ordinary microcontroller starts up, there is no need to use the bootloader to move the code from ROM to RAM.But STM32 microcontroller needs, you can refer to related articles: STM32 code startup process . Here we can first look at the process of executing the microcontroller program. The microcontroller executes in three steps:
fetch instruction
Analysis instructions
execute instruction
The instruction is read from the program memory according to the value of the PC and sent to the instruction register. Then analyze the execution execution. In this way, the single-chip microcomputer goes to the code instruction from the internal program memory and accesses the relevant data from the RAM.The speed of RAM fetching is much higher than that of ROM, but ordinary single-chip microcomputers do not affect the slowness of fetching instructions from ROM because their operating frequency is not high.The CPU of STM32 runs at a high frequency, which is much greater than the speed of reading and writing from ROM. So you need to use the bootloader to move the code from ROM to RAM.Using the stack is like going to a restaurant to eat, just order food (issue an application), pay, and eat (use), leave when you are full, and don’t care about preparatory work such as cutting vegetables, washing vegetables, washing dishes, brushing pots, etc. Finishing work, his advantage is that it is fast, but the degree of freedom is small. Using the heap is like making your own favorite dishes. It is more troublesome, but it is more in line with your own taste and has a large degree of freedom.In fact, the stack is some storage units in the microcontroller. These storage units are designated to save some special information, such as addresses (protection breakpoints) and data (protection sites). If I had to add a few features to him, it would be:
The contents of these storage units are some relevant parameters of the accident scene when the program execution process is interrupted by interruption. If these parameters are not saved, the microcontroller cannot return to the main program to continue execution after the interrupt function is executed.
The addresses of these memory locations are recorded in a place called the stack pointer (SP).
Combining the development of STM32 to tell the stack
As can be seen from the above description, how the heap and stack are occupied in the code. Many people may still not be able to understand it. Here, we will discuss the content related to the stack in the development process of STM32.How to set stack size for STM32?At the beginning of the MDK-based startup file, there is a section of assembly code that allocates the stack size. The key here is to know the size of the stack value. There is also an AREA (area), which means to allocate a stack data segment. The value size can be modified by yourself, or you can use the STM32CubeMX value size configuration, as shown in the following figure. The default setting value of STM32F1 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 take up 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 it exceeds the stack size we configure.Function parameters:
Here is a point to emphasize: the passed pointer only occupies 4 bytes, if the structure is passed, it will occupy the structure size space. Tip: When functions are nested and recursive, the system will still occupy stack space.The default setting of the heap (Heap) is 0x200 (512) bytes.
Heap_Size EQU 0x200
Most people should rarely use malloc to allocate heap space. Although the data on the heap can be accessed as long as the programmer does not release the space, if you forget to release the heap memory, it will cause memory leaks and even fatal potential errors.
Analysis of RAM usage in MDK
People who often debug online may analyze some underlying content. Here is combined with MDK-ARM to analyze the problem of RAM size. After MDK compilation, there will be a piece of RAM size information: Here 4+6=1640, converted to hexadecimal is 0x668, when debugging, it will appear: This MSP is the main stack pointer. Generally, the position we point to after reset is actually the top of the stack: The MSP points to address 0x20000668 which is derived from 0x20000000 offset by 0x668. For specific places that occupy RAM, you can refer to the content at [Image Symbol Table] in the map file: