Understanding the MCU Boot Process

1 MCU Boot Methods

The boot methods of microcontrollers, taking STM32 as an example, are as follows:

Different download methods correspond to different boot methods. STM32 mainly has three boot methods: flash memory, system memory, and embedded SRAM.

Understanding the MCU Boot Process

Flash Boot (Most Common): STM32’s flash can be erased and written tens of thousands of times. Users can download programs to this via JTAG or SWD mode and restart from this location.

System Memory Boot: The system memory is a specific area within the chip, preloaded with a bootloader. The bootloader downloads the program to the flash area for flash booting.

Embedded SRAM Boot: Directly booting code from memory avoids repeated erasing of flash memory due to minor modifications, generally used for high-speed debugging.

2 MCU Program Execution Process

When the MCU powers on, the boot address is determined by the levels of the BOOT pins (BOOT0, BOOT1). The general address is 0x0000000; for some, like STM32, the boot address is 0x8000000 (flash area boot).

The starting address of the internal flash of STM32 is 0x8000000, and the program is written from this point. The interrupt vector table is located at this position. First, the MSP stack pointer is fetched from 0x8000000 (setting the stack), and then the reset interrupt vector is fetched from the interrupt vector table to execute the reset interrupt program to complete the boot process.

The program execution process is shown in the diagram:

1. After reset, STM32 fetches the address of the reset interrupt vector from address 0x8000004 and jumps to execute the reset interrupt service program, as shown in Figure 1, marked as 1️⃣.

2. The final result of executing the reset interrupt service program is to jump to the main function of the C program, as indicated by 2️⃣ in the above figure, where the main function should be an infinite loop, a function that never returns.

3. During the execution of the main function, an interrupt request occurs. At this time, STM32’s hardware mechanism will force the PC pointer back to the interrupt vector table, as shown by 3️⃣ in the figure.

4. Enter the corresponding interrupt service program based on the interrupt source, as shown by 5️⃣ in Figure 1.

5. After the interrupt service program is completed, the program returns to execute in the main function again, as shown by 6️⃣ in the figure.

Understanding the MCU Boot Process

3 Execution Work of the Boot Process

The boot process mainly completes two parts of work: hardware environment and software environment.

Hardware Environment Operations:

Initialize Clock: Initialize the core clock, main clock, and clocks for various peripherals.

Disable Watchdog: The watchdog is used to detect abnormal application behavior and reset the CPU. During initialization, there is no action to “feed the dog,” which may cause the CPU to reset continuously. Therefore, the watchdog needs to be disabled here.

Establish Interrupt Vector Table: The interrupt vector table serves as a recognition marker for interrupt sources, forming the corresponding interrupt entry addresses or offsets and segment base values for the interrupt service program. The CPU uses the interrupt vector table to enter the interrupt service program to handle related transactions.

Initialize Stack: The stack’s role is to protect the context. When a function call or interrupt occurs, the current execution address is pushed onto the stack, and after the call is completed, it returns to the execution address of this call. During the boot phase, the stack registers, stack size, starting address, etc., are initialized.

Memory Initialization: Choose internal or external RAM.

Software Environment Operations:

Copy RO, RW from their loading domains to running domains.

Initialize ZI domain.

Initialize stack pointer: This includes memory space required by the C library, resources needed for program execution, and C library initialization.

4 Keil Debugging Process Verification

Enter debug mode, first access a startup file called starup_ARMCM4.s, and the running cursor will stop at SystemInit:

Understanding the MCU Boot Process

The beginning of the starup_ARMCM4.s code includes: stack size settings, heap size settings, and a series of interrupt vector tables.

The current execution point is at Reset_Handle. Before executing main, SystemInit must be executed, which is located in the system_ARMCM4.c file, as shown by the jump to the SystemInit function in system_ARMCM4.c.

Understanding the MCU Boot Process

After executing SystemInit, the __main function will be executed.

Understanding the MCU Boot Process

5 Debug File Map

The xxx.map file is a type of debug output information file generated by the compilation process. By examining the map file, one can understand the size of functions, entry addresses, variable sizes, parameter sizes, and other related information to resolve issues like memory overflow and data overflow.

The map file is configured in the Linker Listing tab on the configuration page:

Understanding the MCU Boot Process

Map file analysis: The map file is divided into five major categories, as follows:

Understanding the MCU Boot Process

Section Cross References: Module and segment (entry) cross-references.

Configuration checkbox -> Cross Reference

Understanding the MCU Boot Process

Regarding the fourth point:

startup_armcm4.o(RESET) refers to startup_armcm4.o(STACK) for __initial_sp

This indicates that the RESET segment in startup_armcm4.o refers to a global variable (or function) __initial_sp in the STACK segment of startup_armcm4.o.

Removing Unused Input Sections from the Image: This removes unused modules.

Configuration checkbox -> Unused Section Info

This displays the unused modules that were removed from the image during compilation, along with their sizes and overall statistics.

Understanding the MCU Boot Process

Image Symbol Table: Symbol mapping table.

Configuration checkbox -> Symbols

This section displays the symbol mapping table, including two parts: Local Symbol (local variables) and Global Symbol (global variables).

Understanding the MCU Boot Process
Understanding the MCU Boot Process

Memory Map of the Image: Memory (mapping) distribution.

Configuration checkbox -> Memory Map

Understanding the MCU Boot Process

Image Entry Point: 0x000000c1 Mapping entry address.

Load Region LR_IROM1 (Base: 0x00000000, Size: 0x0000026c, Max: 0x00040000, ABSOLUTE): Indicates that the loading region is located at LR_IROM1 starting address 0x00000000, with a size of 0x0000026c, and a maximum of 0x00040000.

Execution Region ER_IROM1 (Base: 0x08000000, Size: 0x00001188, Max: 0x00080000, ABSOLUTE)

Understanding the MCU Boot Process

Execution Region RW_IRAM1 (Exec base: 0x20000000, Load base: 0x00000268, Size: 0x00001068, Max: 0x00020000, ABSOLUTE)

Understanding the MCU Boot Process

The image’s entry address is 0x000000c1, then the starting address, size of the loading domain, and finally the starting addresses and sizes of execution domains ROM and RAM can be known, where 0x20000000 is the starting address of RAM, and 0x00000000 is the starting address of ROM, here it is flash. Some discarded data, such as unused variables, will not appear in the table after being optimized.

Image Component Sizes: Storage composition sizes.

Configuration checkbox -> Size Info

Understanding the MCU Boot Process

Code: Code data.

RO-data: Refers to read-only data, constant data other than inline data.

RW-data: Refers to readable, writable, initialized variables.

ZI-data: Refers to uninitialized variables.

Understanding the MCU Boot Process

The overall size occupied by the several regions displayed above, where:

Code, Ro-data: Located in FLASH.

Rw-data, ZI-data: Located in RAM.

RW-data initialized data will be stored in Flash and moved to RAM upon power-up.

Total ROM Size is the size of the program downloaded to flash.

Leave a Comment

Your email address will not be published. Required fields are marked *