In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of the stack in the STM32 single-chip microcomputer. I hope you will get something after reading this article. Let's discuss it together.
For those of you who know a little bit about assembly programming, a stack is a continuous storage area in memory that holds some temporary data. The stack operation is accomplished by PUSH and POP instructions. The program memory can be divided into several areas:
Stack (stack)
Stacking area (Heap)
Global area (static)
After the program is compiled, the global variables and static variables have been allocated memory space. When the function is running, the program needs to allocate stack space for local variables, and when the interrupt comes, it also needs to put the function pointer into the stack to protect the site. So that after the interrupt processing, you can return to the previously executed function.
The stack is allocated from high to low and the heap is allocated from low to high. The difference of Stack between ordinary single Chip Microcomputer and STM32 single Chip Microcomputer when the ordinary single chip microcomputer starts, it is not necessary to use bootloader to move the data from ROM to RAM. But STM32 singlechip needs. Here we can first look at the single-chip microcomputer program execution process, single-chip microcomputer execution is divided into three steps: take the execution analysis instruction to read the instruction from the program memory according to the value of PC, and send it to the instruction register. Then analyze the execution. In this way, the single-chip microcomputer codes the instructions from the internal program memory and accesses the relevant data from the RAM. The speed of fetching RAM is much higher than that of ROM, but because the running frequency of ordinary single-chip microcomputer is not high, it does not affect the slow fetching of instructions from ROM. On the other hand, STM32's CPU runs at a high frequency, much faster than the speed of reading and writing from ROM. So you need to use bootloader to move the data from ROM to RAM. Using the stack is like when we go to a restaurant to eat, just order food (send out applications), pay, and eat (use), and leave as soon as we are full, regardless of preparation work such as cutting and washing vegetables, and cleaning dishes and washing pots. His advantage is that it is fast, but the degree of freedom is small. Using heap is like making your own favorite dishes, which is more troublesome, but it is more in line with your own taste and has a lot of freedom. In fact, the stack is some memory units in the single-chip microcomputer, these storage units are designated to store some special information, such as address (protect breakpoints) and data (protect the site). If we have to add a few features to him, it is: the contents of these storage units are some relevant parameters of the accident site when the program is interrupted during execution. If these parameters are not saved, the single-chip microcomputer will not be able to return to the main program to continue execution after executing the interrupt function. The addresses of these memory units are recorded in a place called the stack pointer (SP). Describe the stack with the development of STM32
From the above description, you can see how the heap and stack are occupied in the code. Perhaps many people still do not understand, here combined with the STM32 development process and stack-related content to talk about.
How to set the stack size of STM32? At the beginning of the MDK-based startup file, there is a piece 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 (region) that allocates a stack segment. The numeric size can be modified by yourself, or you can use the STM32CubeMX numeric size configuration, as shown in the following figure.
In IAR, the stack size is configured through engineering, as shown in the following figure.
The default setting of STM32F1 is 0x400, which is 1K size.
Stack_Size EQU 0x400
Local variables in the body of function:
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 more local variables in the function, we need to pay attention to whether it exceeds the stack size we configured.
Function parameters:
Void HAL_GPIO_Init (GPIO_TypeDef * GPIOx, GPIO_InitTypeDef * GPIO_Init)
It should be emphasized here that the passing pointer takes up only 4 bytes, and if you pass a structure, it will take up the size of the structure. Tip: when functions are nested and recursive, the system will still occupy stack space.
The default setting for 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 all the time as long as the programmer does not free up space, if you forget to free the heap memory, it will cause memory leaks and even fatal potential errors.
Analysis of RAM occupancy in MDK
People who often debug online may analyze some of the underlying content. Here, combined with MDK-ARM, we analyze the size of RAM occupancy. After the MDK is compiled, there is a piece of RAM size information:
Here 4'6' 1640, converted to hexadecimal is 0x668, during debugging, it will appear:
This MSP is the main stack pointer. Usually we point to the position we reset, which actually points to the top of the stack:
The MSP points to the address 0x20000668 is derived from the 0x20000000 offset 0x668. For more information about where the RAM is occupied, please see [Image Symbol Table] in the map file:
After reading this article, I believe you have some understanding of "sample Analysis of Stack in STM32 single Chip Microcomputer". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.