A text parsing and understanding of STM32 memory management and stack

This article mainly introduces the understanding and understanding of STM32 memory management and stack. First of all, it introduces the principle of memory management and the principle of allocation and release. Secondly, it introduces the memory structure of stm32. Finally, it explains the understanding and understanding of the stack. Follow Xiaobian to find out about it.

Detailed STM32 memory management

Memory management refers to the technology that allocates and uses computer memory resources while the software is running. Its main purpose is how to efficiently and quickly allocate and free and reclaim memory resources at the appropriate time. There are many ways to implement memory management. In fact, they ultimately have to implement two functions: malloc and free; malloc functions are used for memory applications, and free functions are used for memory release.

The principle of memory management

A text parsing and understanding of STM32 memory management and stack

From the above figure, it can be seen that the partitioned memory management consists of two parts: the memory pool and the memory management table. The memory pool is divided into n blocks, and the corresponding memory management table is also of size n. Each entry of the memory management table corresponds to a memory of the memory pool. The meaning of the item value of the memory management table represents that when the value of the item is 0, the corresponding memory block is not occupied. When the value of the item is non-zero, the memory block corresponding to the item is already occupied. Values ​​represent the number of memory blocks that are continuously occupied. For example, a value of 10 indicates that a total of 10 memory blocks are allocated to an external pointer including the corresponding memory block. The inner-inch distribution direction is shown in the figure, which is the distribution direction from the top bottom. (that is, from the high address to the low address) that is to start emptying the memory from the very end. When the memory management has just been initialized, the memory table is all cleared to indicate that no memory block is occupied.

Distribution principle

When the pointer p calls malloc to apply for memory, it first judges the number of memory blocks (m) to be allocated by p, then starts from the nth item and searches downwards until it finds m blocks of consecutive empty memory blocks (that is, corresponding memory management entries. 0), then set the values ​​of m memory management entries to m (the mark is occupied). Finally, return the address of the last empty memory block to p and complete the allocation. Note that if the memory is not enough (find no contiguous m blocks of free memory was found), NULL is returned to p, indicating that the allocation failed.

Release principle

When the p application's memory is used up and needs to be released, call the free function. The free function first determines the memory block corresponding to the memory address pointed to by p, and then finds the corresponding memory management table entry, and obtains the number of memory blocks occupied by p (the value of the memory management table entry is the number of allocated memory blocks). The values ​​of these m memory management table entries are all cleared, the flags are released, and a memory release is completed.

Stm32 memory structure

A text parsing and understanding of STM32 memory management and stack

Flash, SRAM registers and input and output ports are organized in the same 4GB linear address space. The accessible memory space is divided into 8 main blocks, each of which is 512 MB.

FLASH store download program.

SRAM is the data stored in the running program.

So, as long as you do not expand the memory, everything in the finished program will also appear in these two memories.

Stack awareness and understanding

1. Stack in STM32

This caused confusion and led to a lot of logical confusion. The first thing to note is that the SCM is an integrated circuit chip that integrates CPU, RAM, ROM, various I/O ports and interrupt systems, and timers/counters. The CPU includes various bus circuits, calculation circuits, logic circuits, and various registers. The Stm32 has general-purpose registers R0-R15 and some special function registers, including the stack pointer register. When the stm32 is running the program normally, an interrupt is generated and the CPU needs to push the value in the register into RAM and then store the address where the data is located in the stack register. When the interrupt processing completes the exit, the data is pushed out to the previous register. This is done automatically in the C language.

2, the stack in programming

Many times in the programming will mention the stack this thing, to be precise, this is an area in RAM. Let us first understand a few instructions:

(1) All the contents of the program will eventually appear only in flash, ram (no expansion).

(2) The division of segments is to store similar data types in a single area for ease of management, but as mentioned above, no matter what segment of data, it is ultimately in flash and ram.

C language is divided into stack, heap, bss, data, code segment. Each specific segment specifically stores what data, directly Baidu. Focus on the analysis of STM32 and the segmentation in the MDK segment.

The MDK Code, RO-data, RW-data, ZI-data sections:

Code is stored program code.

RO-data is a storage const constant and instruction.

RW-data is a global variable that stores an initialization value other than 0.

ZI-data is a global variable that stores an uninitialized global variable or an initialization value of 0.

Flash=Code + RO Data + RW Data;

RAM= RW-data+ZI-data;

This is the size of each segment that can be obtained after the MDK is compiled, and it can also be used to occupy the corresponding size of the FLASH and RAM, but there are also two data segments that also occupy the RAM, but it will be occupied when the program is running. , That is the heap and stack. In the startup file .s file of stm32, there is a stack setting. In fact, the memory usage of this stack is allocated at the address after the above RAM is allocated to RW-data+ZI-data.

Heap: A memory area where the compiler calls dynamic memory allocation.

Stack: Local variables are the place where the program is running, so local arrays with arrays that are too big can cause stack overflows.

The size of the stack isn't known after the compiler is compiled. It is known only when it is run, so it's important to note that you don't cause the stack to overflow. Otherwise waiting for hardfault to find you.

A text parsing and understanding of STM32 memory management and stack

3, stack and memory management in the OS.

The stack of the embedded system, no matter what method is used to get the memory, feels his way is similar to the heap in the programming. Currently I know two ways to get memory:

(1) Enclose a block of memory with a huge array of global variables, and then use this memory for memory management and allocation. In this case, the memory occupied by the stack is the above: if the array is not initialized, or the array's initialization value is 0, the stack is the ZI-data portion of the RAM; if the array initialization value is not 0, the stack will occupy the The RW-data portion of RAM. The advantage of this approach is that it is easy to logically know the origin and destination of data.

(2) It is to allocate the RAM unused by the compiler for memory allocation, that is, to remove some or all of the RAM memory remaining after removing the RW-data+ZI-data+ compiler heap + compiler stack. Memory management and distribution. In this case, you only need to know the first address of the remaining part of the memory and the end address of the memory. Then how much memory is used, start digging with the first address, make a linked list, and link the memory acquisition and release related information. Timely memory management. The memory management algorithm is various and unknown. In this case, the memory allocation of the OS does not conflict with its own local or global variables. I have been tangled in this for a long time, thinking that the variables in the function are also dynamic from the system. In memory. This way feels more able to understand the beginning and end of your address.

These two methods I feel no one is more clear, because it is only a memory acquisition, smart memory management and distribution. The

ZGAR AZ Mesh Vape Pods 1.0

ZGAR AZ Mesh Vape Pods 1.0


ZGAR electronic cigarette uses high-tech R&D, food grade disposable pods and high-quality raw material. A new design of gradient our disposable vape is impressive.We equip with breathing lights in the vape pen and pods.


Our team has very high requirements for product quality, taste allocation and packaging design. Designers only use Hong Kong designers, e-cigarette liquid only imports from the United States, materials are food grade, and assembly factory wants medical grade without ground workshop.


We offer best price, high quality Mesh Pods,Pod System Vape,Pods Systems Touch Screen,Empty Pod System, Pod Vape System,Disposable Pod device,Vape Pods to all over the world.



Pod Systems Vape And Smoke,Vape Pod System Device,ZGAR AZ Mesh Vape Pods 1.0 Pod System Vape Kit,Pod System Mini Vape Pod

ZGAR INTERNATIONAL(HK)CO., LIMITED , https://www.szvape-pen.com