[Embedded] Stack and microcontroller memory

stack

In on-chip RAM, a special area is often designated to store certain special data
It follows the principles of sequential access and last-in-first-out (LIFO/FILO) , this RAM area is called a stack.

In fact, the stack is some storage units in the microcontroller. These storage units are designated to store some special information, such as addresses (protection breakpoints) and data (protection sites).

Stack features

1. The contents of these storage units are some relevant parameters of the accident scene when the program execution is interrupted by an interrupt. If these parameters are not saved, the microcontroller will not be able to return to the main program to continue execution after executing the interrupt function.

2. The addresses of these storage units are recorded in a place called the stack pointer (SP).
3. The stack is allocated from high to low, and the heap is allocated from low to high.

stack classification

What we generally call a stack refers to a stack. The stack is divided into hard stack and soft stack. The hard stack is SP, which grows downward from the top of the on-chip RAM. The soft stack is the space between the hard stack and the global variable area. C51 function calls are implemented through R0-R7 and the stack.

stack effect

1) The CPU automatically pushes the current PC value onto the stack when subroutine calls and interrupt services are performed, and automatically pops the PC value onto the stack when returning. 2) Protect the site/Restore the site 3) Data transmission


Microcontroller memory

Program memory can be divided into several areas, stack area (stack), heap area (Heap), global area (static), text always-on area, and program code area.
Insert image description here

//main.cpp
int a = 0; //全局初始化区
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"优化成一个地方。
}

The first difference from the stack is the application method: the stack (the English name is stack) automatically allocates space by the system. For example, if we define a char a, the system will automatically create space for it on the stack. The heap (the English name is heap) is the space that programmers apply for according to their needs, such as malloc (10); open up ten bytes of space. Since the space on the stack is automatically allocated and reclaimed, the life cycle of the data on the stack is only during the running of the function. It is released after running and cannot be accessed again. The data on the heap can always be accessed as long as the programmer does not release the space. However, the disadvantage is that if the programmer forgets to release it, it will cause a memory leak.

STM32

In the MDK compilation environment, you can check the RAM usage and allocation of the program in the "Memory Map of the image"–>"Execution Region RW_IRAM1" content of the map file, as follows:
Insert image description here

STM32 program data classification

Code: Program code
RO-data: const constants and instructions
RW-data: Initialize global variables whose value is not 0
ZI-data: Uninitialized global variables or global variables with an initialization value of 0

RO Size = Code + RO Data indicates the size of FLASH occupied when the program is running
RW Size = RW Data + ZI Data indicates the size of RAM occupied
ROM Size = Code + RO Data + RW Data represents the FLASH size occupied after programming the program

STM32 memory (RAM) allocation

The memory occupied by a program compiled by C/C++ can be divided into the following parts:

栈(stack): Automatically allocated and released by the compiler, storing function parameter values, local variable values, etc.
堆(heap): Stores memory that is dynamically allocated while the program is running. It is generally allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS when the program ends.
bss段: Usually refers to a memory area used to store uninitialized global variables in the program, and stores ZI-data data.
data段: Usually refers to a memory area used to store the program. A memory area of ​​global variables that has been initialized in to store RW-data data

FLASH occupies roughly the following two parts:

文字常量区(const): The constant string is placed here.
程序代码区 (code): Store the binary code of the function body

STM32 stack location

Insert image description here
The STM32 stack is stored in the on-chip static SRAM.
Insert image description here
The address allocation can be found in the "Memory Map of the image" of Keil's compiled map file.
It can be seen that the address of the heap is 0x20000a08 and the size is 0x200. The address of the stack is 0x20000c08 and the size is 0x400. It can be calculated that the address of the top of the stack is: 0x20000c08 + 0x400 = 0x20001008. When the program first runs, the main stack pointer MSP points to the highest address of the memory occupied by the program, which is the top address of the stack MSP, which is the value stored in the first four bytes of the interrupt vector table.
Insert image description here

Guess you like

Origin blog.csdn.net/apythonlearner/article/details/133791607