STM32 memory management and understanding of heap and stack

Publisher:平和梦想Latest update time:2018-06-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Today I read the memory management code carefully, and then looked at the relevant knowledge of the stack. I figured out some things that I didn’t understand before, and wrote them down for easy reference later. I also hope that everyone can point out what’s wrong and then modify it.    

[Reprint] STM32 memory management and stack-related knowledge




First, let's take a look at the memory structure of stm32.

Flash, SRAM registers and I/O ports are organized in the same 4GB linear address space. The accessible memory space is divided into 8 major blocks of 512MB each.

FLASH stores the downloaded program.

SRAM is used to store data in running programs.

Therefore, as long as you do not expand the memory externally, everything in the written program will appear in these two memories.

This is a premise!

 

 







Stack Cognition

1. Stack in STM32.

I have been confused about this, which has led to a lot of logical confusion. First of all, it should be explained that the microcontroller is an integrated circuit chip that integrates CPU, RAM, ROM, multiple I/O ports and interrupt systems, timers/counters and other functions. The CPU includes various bus circuits, calculation circuits, logic circuits, and various registers. Stm32 has general registers R0-R15 and some special function registers, including the stack pointer register. When stm32 runs the program normally, an interrupt comes, the CPU needs to push the value in the register into the RAM, and then store the address of the data in the stack register. When the interrupt processing is completed and exited, the data is popped to the previous register, which is done automatically in C language.

2. Stack in programming.

In programming, we often mention the stack. To be more precise, it is an area in RAM. Let's first understand a few explanations:

(1) All the contents in the program will eventually appear only in flash and RAM (not expanded externally).

(2) Segmentation is the process of storing similar data types in one area for easier management. However, as mentioned above, data in any segment will eventually end up in flash and RAM.

In C language, there are stack, heap, bss, data and code segments. You can search Baidu to find out what data is stored in each segment. Let's focus on analyzing STM32 and the division of segments in MDK.

Code, RO-data, RW-data, ZI-data segments under MDK:

Code is used to store program code.

RO-data stores const constants and instructions.

RW-data is a global variable that stores a non-zero initialization value.

ZI-data is used to store uninitialized global variables or global variables whose initialization value is 0.

Flash=Code + RO Data + RW Data;

RAM= RW-data+ZI-data;

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

Heap: is the memory area that the compiler calls dynamic memory allocation.

Stack: It is the place where local variables are stored when the program is running, so if the array of local variables is too large, it may cause stack overflow.

The size of the stack is unknown after the compiler compiles the program. It is known only at runtime, so you need to be careful not to cause a stack overflow. Otherwise, you will get a hard fault.

3. Stack and its memory management in OS.

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

(1) Use a large global variable array to enclose a piece of memory, and then use this memory for memory management and allocation. In this case, the memory occupied by the stack is as mentioned above: if the array is not initialized, or the array initialization value is 0, the stack occupies the ZI-data part of the RAM; if the array initialization value is not 0, the stack occupies the RW-data part of the RAM. The advantage of this method is that it is easy to logically know the origin and destination of the data.

(2) Use the RAM that is not used by the compiler for memory allocation, that is, manage and allocate part or all of the remaining RAM after removing RW-data+ZI-data+compiler heap+compiler stack. In this case, you only need to know the first address and the last address of the remaining memory, and then start digging with the first address to determine how much memory to use. Make a linked list, link the memory acquisition and release related information, and you can manage the memory in time. There are many different algorithms for memory management, which I will not explain in detail. In this case: the memory allocation of the OS does not conflict with its own local variables or global variables. I struggled with this for a long time before, thinking that the variables in the function were also obtained from the system's dynamic memory. This method makes it easier to understand the beginning and end of your own address.

I feel that neither of these two methods is better, because it is just a way to obtain memory. The clever part lies in the management and allocation of memory.


Keywords:STM32 Reference address:STM32 memory management and understanding of heap and stack

Previous article:Embedded Learning--step4 STM32F4 bus architecture
Next article:STM32 heap area memory allocation -- the use of malloc

Recommended ReadingLatest update time:2024-11-16 13:41

STM32 interrupt usage
STM32 interrupts are sometimes used too much and can be confusing. I would like to record this because they have been used interchangeably with M3 and M0 before. Some differences in the firmware libraries can easily confuse them. Here is the configuration of M3 external interrupts. step: 1. Configure the corresponding
[Microcontroller]
Considerations for configuring multiple timers in STM32 for photoelectric encoding mode at the same time
I am working on a car project and I have a problem when I use 3 timers in STM32f103 configured as encoder mode at the same time. As a beginner of STM32, you may encounter the following situation when using encoder mode: there will be no big problem when you configure a timer as encoder mode according to some program
[Microcontroller]
Driver for DS18B20 under STM32
After a night of hard work, I finally ported the DS18B20 driver to STM32. I have used single and multiple DS18B20 on 51 before, and I have a ready-made program. I thought I could get it done quickly, but I was still stuck. Here are a few key points:     The first is the delay problem. If you use software delay on STM3
[Microcontroller]
STM32 study notes: FSMC details
FSMC (Flexible Static Memory Controller) is a new memory expansion technology used by the STM32 series. It has unique advantages in external memory expansion and can easily expand different types of large-capacity static memories according to system application needs . After using the FSMC controller, the FSMC_A provi
[Microcontroller]
STM32 study notes: FSMC details
STM32 study notes: standby wake-up
Open the Chinese reference manual of STM32, which contains detailed annotations on the low power mode of STM32.        1. Sleep mode (Cortex™-M3 core stops, all peripherals including the peripherals of the Cortex-M3 core, such as NVIC, system clock (SysTick), etc. are still running)        2. Stop mode (all clocks are
[Microcontroller]
STM32 study notes: standby wake-up
STM32 sleep mode low power consumption (stop mode)
At present, stm32 is very popular, so this article discusses the low power mode of stm32. Let's go to the manual first!  This is an English document that is not easy to understand, right? Let's look at the Chinese document below!  I compared STM32F0 and STM32F1. Both enter low power consumption in the same way. The
[Microcontroller]
STM32 sleep mode low power consumption (stop mode)
STM32IAP upgrade-----Summary of problems encountered in writing IAP upgrade
I uploaded the source code and other materials of IAP. There are 12 files in the compressed package. http://download.csdn.net/detail/f907279313/7524849 (If you want points, please give some points for your hard work in collecting them) There is another blog post summarizing IAP: http://blog.csdn.net/super_demo/articl
[Microcontroller]
STM32IAP upgrade-----Summary of problems encountered in writing IAP upgrade
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号