Common Memory Partition Areas in Microcontrollers

Follow+Star public number, don’t miss wonderful content

Common Memory Partition Areas in Microcontrollers

Author | Xing Mo

Editor | strongerHuang
Seeing some friends discussing the topic of microcontroller memory, today I will describe the common partition areas in conjunction with STM32.
In an STM32 program code, from high memory address to low memory address, the stack area, heap area, global area (static area), constant area, and code area are sequentially distributed. Among them, the global area has the .bss section distributed at high addresses and the .data section distributed at low addresses.
The overall distribution is as follows:
High Memory Address
Stack Area
Heap Area
.bss Section
.data Section
Constant Area
Low Memory Address
Code Area
1. Stack Area (stack)
  • Temporarily created local variables are stored in the stack area.
  • When a function is called, its input parameters are stored in the stack area.
  • When a function returns, its return value is stored in the stack area.
  • const defined local variables are stored in the stack area.
2. Heap Area (heap)
The heap area is used to store memory segments that are dynamically allocated during program execution, which can increase or decrease.
Functions like malloc can be used to implement dynamic memory allocation.
Memory allocated by the malloc function must be released with free; otherwise, it will cause memory leaks.
3. Global Area (Static Area)
The global area consists of .bss and .data sections, which are readable and writable.
4. .bss Section
Uninitialized global variables are stored in the .bss section.
Global variables initialized to 0 and static variables initialized to 0 are stored in the .bss section.
The .bss section does not occupy executable file space; its content is initialized by the operating system.
5. .data Section
Initialized global variables are stored in the .data section.
Static variables are stored in the .data section.
The .data section occupies executable file space; its content is initialized by the program.
Global variables defined with const are stored in the .rodata section.
6. Constant Area
Strings are stored in the constant area.
The content of the constant area cannot be modified.
7. Code Area
Executable program code is stored in the code area.
String constants may also be stored in the code area.
Through the introduction above, you may still be confused about the storage locations of various data. Let’s understand it again through a simple program.
Through the introduction above, you may still be confused about the storage locations of various data. Let’s understand it again through a simple program【Extra paragraph】
#include <stdio.h>
static unsigned int val1 = 1;        //val1 is stored in .data section
unsigned int val2 = 1;               //initialized global variable is stored in .data section
unsigned int val3 ;                  //uninitialized global variable is stored in .bss section
const unsigned int val4 = 1;         //val4 is stored in .rodata (read-only data section)

unsigned char Demo(unsigned int num) //num is stored in stack area
{
    char var = "123456";               //var is stored in stack area, "123456" is stored in constant area
    unsigned int num1 = 1 ;            //num1 is stored in stack area
    static unsigned int num2 = 0;      //num2 is stored in .data section
    const unsigned int num3 = 7;       //num3 is stored in stack area
    void *p;
    p = malloc(8);                     //p is stored in heap area
    free(p);
    return 1;
}

void main(){
    unsigned int num = 0 ;
    num = Demo(num);                   //The return value of Demo() function is stored in stack area.
}
We have comprehensively analyzed the heap, stack, global area, constant area, and code area above, and provided examples for explanation. Next, we will discuss the media on which these areas are stored.
8. Physical Characteristics of RAM, ROM, and Flash Memory
First, we need to understand the physical characteristics of RAM, ROM, and Flash Memory.
9. RAM
RAM, also known as Random Access Memory, allows the stored content to be randomly read and written through instructions. The data stored in RAM will be lost when power is turned off, so it can only store data during operation. RAM can be divided into two types: Dynamic RAM (DRAM) and Static RAM (SRAM).
10. ROM
ROM, also known as Read-Only Memory, can only read data from it and cannot be written arbitrarily. Compared to RAM, ROM has the disadvantage of slower read and write speeds. However, due to its advantage of retaining data after power loss, it is often used to store programs and data that are written once, such as the BIOS program chip on the motherboard is a ROM storage.
11. Flash Memory
Due to the difficulty of modifying ROM, Flash Memory was developed later. Flash Memory not only has the characteristic of not losing data when powered off like ROM, but it can also be modified when needed, although it is more expensive than ROM.
12. Different Data Storage Locations
From the previous analysis, we know that the contents of the code area and constant area cannot be modified, and ROM (STM32 is Flash Memory) is also not modifiable, so the contents of the code area and constant area are stored in ROM after compilation.
On the other hand, the stack, heap, and global area (.bss section, .data section) are all stored in RAM.
Thus, the introduction to which area different data is stored is complete. Next, we will introduce the Build Output window of Keil.
13. Build Output Window of Keil
Common Memory Partition Areas in Microcontrollers
As shown in the figure, there are four code segment sizes: Code, RO-data, RW-data, and ZI-data.
Among them, Code is the size occupied by the code, RO-data is read-only constants, RW-data is initialized readable and writable variables, and ZI-data is uninitialized readable and writable variables.
Sometimes, we need to know the usage of RAM and ROM, so we can use the following formulas to calculate.
RAM = RW-data + ZI-data
ROM = Code + RO-data + RW-data
This article is originally written by the author “Xing Mo” and authorized for publication. Source address:
https://blog.csdn.net/lin_duo/article/details/103019390
———— END ————
Common Memory Partition Areas in Microcontrollers
● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected tutorials from the Embedded Column
Follow the public account reply “Join Group” to join the technical exchange group according to the rules, reply “1024” for more content.
Common Memory Partition Areas in Microcontrollers
Common Memory Partition Areas in Microcontrollers
Click “Read the original text” for more sharing.

Leave a Comment

×