STM32 internal flash allocation

Code/ RO-data/ RW-data /ZI-data

After the project is successfully compiled in Keil, the following information will be output in the Bulid Ouput window below:
Program Size: Code=6320 RO-data=4864 RW-data=44 ZI-data=1636

Meaning:
Code: It is the byte size occupied by the code in the program
RO-data: The size of the instructions and constants defined in the program (personal understanding: Read Only)
RW-data: The size of the initialized variables in the program (personal understanding” : Read/Write)
ZI-Data : The size of uninitialized variables in the program (personal understanding: Zero Initialize)

ROM(Flash) size = Code+RO-data+RW-data;
RAM size = RW-data+ZI-data
You can view the occupied flash and ram size through .map

Relevant code example:
int a=0;              //全局初始化区
char *p1;              //全局未初始化区
main()
{
    int b;              //栈
    char s[]="abc";        //栈
    char *p3= "1234567";     //在文字常量区
    static int c =0 ;       //静态初始化区
    p1= (char *)malloc(10);   //堆区
    strcpy(p1,"123456");    //"123456"放在常量区
}
STM32 internal mapping

write picture description here

ROM(Flash) size: The flash address of stm32 starts at 0x0800 0000, and the end address is 0x0800 0000 plus the actual flash size of the chip. Different chips have different flash sizes.

RAM size: The RAM start address is 0x2000 0000, and the end address is 0x2000 0000 plus the RAM size of the chip. Different chips have different RAM.

The content in Flash is generally used to store code and some data defined as const, which is not lost when power is turned off.
RAM can be understood as memory, which is used to store data, variables, etc. when the code is running. Power failure data lost.

STM32 maps peripherals, etc. into the form of addresses, and the operation of the address is the operation of the peripheral.
The peripheral address of stm32 starts from 0x4000 0000. It can be seen that in the library file, the registers and peripherals are operated through the offset based on the address of 0x4000 0000.

In general, the program file is written from the address of 0x0800 0000, which is where STM32 starts to execute, and 0x0800 0004 is the starting address of the interrupt vector table of STM32.

settings in keil

write picture description here

The write address of the program starts from 0x08000000 (the number of zeros), and its size is 0x80000, which is a space of 128K. In other words, it tells the compiler that the flash space is from 0x08000000-0x08020000, and the RAM address starts from 0x20000000 , the size of 0x5000 is 20K of RAM. This corresponds to the memory address mapping relationship of STM32.

After M3 is reset, take out the address of the reset interrupt from 0x08000004, and jump to the reset interrupt program. After the interrupt is executed, it will jump to our main function. The main function is generally an infinite loop, and it will not exit after entering. When an interrupt occurs, M3 forces the PC pointer to jump back to the interrupt vector table, and then enters the corresponding interrupt function according to the interrupt source. After executing the interrupt function, it returns to the main function again. The general process is like this.

STM32 internal flash composition

write picture description here

  1. Main memory: Generally, when we say the internal FLASH of STM32, we refer to this main memory area, which is the space for storing user applications. The 1M FLASH and 2M FLASH in the chip model description refer to the size of this area. Like other FLASH, before writing data, it must be erased by sector.
  2. System storage area: The system storage area is an area that cannot be accessed by users. It has solidified the startup code when the chip leaves the factory. It is responsible for implementing ISP programming functions such as serial port, USB and CAN.
  3. OTP area: OTP (One Time Program), refers to a storage area that can only be written once, with a capacity of 512 bytes, and the data cannot be changed after writing. OTP is often used to store encryption keys for applications.
  4. Option byte: The option byte is used to configure FLASH read and write protection, BOR level in power management, software/hardware watchdog and other functions. This part has a total of 32 bytes. It can be modified by modifying the option control register of FLASH.
View project memory distribution

Since the internal FLASH itself stores program data, if a certain section of program code is not intentionally deleted, the content of the program space should not generally be modified. Therefore, before using the internal FLASH to store other data, it is necessary to know which spaces have written program codes and stored them. No sector of program code should be modified. By querying the "*.map" suffix file generated when the application is compiled, after opening the map file, check the area at the end of the file, and you can see a record beginning with "Memory Map of the image" (if you can't find the available search functional positioning)
write picture description here

This section is the ROM memory distribution image of a project. In the STM32 chip, the content of the ROM area refers to the code stored in the internal FLASH.
In the description of the map file above, we learned that the base address (Base) of the load and execution space is 0x08000000, which is exactly the first address of the FLASH inside the STM32, that is, the program storage space of the STM32 is directly the execution space; their size (Size) are 0x00000b50 and 0x00000b3c respectively. The reason why the ROM of the execution space is relatively small is because some RW-data type variables are copied to the RAM space; their maximum space (Max) is 0x00100000, which is 1M bytes. Refers to the maximum space of the internal FLASH.
When calculating the space occupied by the program, you need to use the size of the load area for calculation. In this example, the internal FLASH used by the application is the space area from 0x08000000 to (0x08000000+0x00000b50) address. Therefore, the storage space from sector 1 (address 0x08004000) can be used for other purposes, and the data in the application space will not be tampered with when using these storage spaces.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625569&siteId=291194637