【STM32】IAP upgrade 00 prerequisite knowledge

Introduction to IAP (In Application Programming)

If the Flash is large enough, the program after power-on can have multiple programs with different functions on one Flash by modifying the MSP.

IAP is a program that is run in advance to upgrade functions before executing normal functions. This program does not perform normal functional operations, but only receives programs or data through a certain communication method (such as USB, USART) and performs updates to the second part of the code; 1) Check whether the second part of the code needs to be updated
2
) If no update is required, go to 4)
3) Perform the update operation
4) Jump to the second part of code execution

The first part is called the bootloader program, and the second part is called the APP. There can be multiple APPs, and STM APPs can run in Flash or SRAM.

At the address 0x1FFF F000, a BootLoader section is officially written for user use. We can also write a BootLoader program ourselves for our own use. We divide the program into the BootLoader part and the application part. The general meaning is as shown in the figure below:
Insert image description here

Why use user BootLoader:

In some projects, the program may need to be changed frequently for some reasons. If it is particularly troublesome to re-burn every time, then we can design a BootLoader ourselves and upgrade it through the SD card: run the BootLoader first after powering on
. The main job of BootLoader is to detect whether there is an SD card and whether there is the required BIn file in the SD card.
If it is detected, it will be copied to the application area so that the program can be updated. After the update is completed, it will jump to the application execution;
if it is not detected, The corresponding SD card means that the program does not need to be updated and will jump to the application for execution;

STM32 startup mode

Microcontroller reset

Start from 0x0000 0000

There are three reset methods for the microcontroller: 上电复位, 硬件复位, 软件复位.

And after leaving the reset state, the first thing the CM4 core does is to read the following two 32-bit integer values
Insert image description here
​​1. Take out the initial value of the stack pointer MSP from the address 0x0000 0000, which is 栈顶地址.
2. Get the initial value of the program counter pointer PC from address 0x0000 0004. This value points to the first instruction executed after reset.

Since it is said here that stm32 starts from 0x0000 0000, why does the startup mode we see start from 0x0800 0000, 0x2000 0000, 0x1FFF F000?

Remapping the addresses 0x0000 0000 and 0x0000 0004 to other address spaces is the startup mode selection. In this way, accessing 0x0000 0000 is equivalent to accessing 0x0800 0000, 0x2000 0000 or 0x1FFF F000

Remapping is also "boot mode selection"

There are three startup modes (bootstrap mode) of STM32: 内部 FLASH, 内部 SRAM, 系统存储器.
Insert image description here
Note: The level of the start pin: 0: low level; 1: high level; x: any level, that is, both high and low levels can be

FLASH 启动方式: Then the kernel will take out the initial value of the stack pointer MSP from the address 0x0800 0000, and take out the initial value of the program counter pointer PC from the address 0x0800 0004. The CPU will start executing the program from the first instruction fetched from the address space pointed to by the PC register, which is to start executing the reset interrupt service routine Reset_Handler. That is, the execution of the interrupt vector table begins.

内部 SRAM 启动方式:Addresses 0x00000000 and 0x00000004 are mapped to the first addresses 0x20000000 and 0x20000004 of the internal SRAM, and the kernel obtains the contents from the SRAM space for bootstrapping. In actual applications, the startup file startup_stm32f407xx.s determines what content is stored at addresses 0x00000000 and 0x00000004. When linking, the scattered loading file (sct) determines the absolute address of these contents, that is, whether it is allocated to internal FLASH or internal SRAM.

系统存储器启动方式:The kernel will obtain the MSP and PC values ​​from 0x1FFFF000 and 0x1FFFF004 of the system memory for bootstrapping. The system memory is a special space that cannot be accessed by users. ST has solidified a piece of code in the system memory before the chip leaves the factory. Therefore, when using the system memory startup mode, the kernel will execute this code. When this code is running, it will provide support for ISP (In System Program). The most common thing on STM32F4 is to detect the information transmitted by USART1 and update itself based on this information. The content of internal FLASH achieves the purpose of upgrading product applications, so this startup method is also called ISP startup method.

Interrupt vector table

Insert image description here
The flash memory of stm32 starts at 0x0800 0000. When the microcontroller is based on the Cortex-M3/M4 core, it responds to interrupts internally through an "interrupt vector table".

The interrupt vector is 地址(指向中断服务函数). After the program is started, the address of the reset interrupt vector will be taken out from the "interrupt vector table" first, and the reset interrupt program will be executed to complete the startup. The starting address of this "interrupt vector table" is 0x0800 0004. When an interrupt occurs during operation When the time comes, the internal hardware mechanism of STM32 will automatically locate the PC pointer to the "interrupt vector table" and 中断源execute the interrupt service routine according to the corresponding interrupt vector.

After joining the IAP program, the program running process

Insert image description here

After reset, execute Reset_Handler and enter the IAP upgrade program.

After adding the IAP upgrade program, after the microcontroller is reset, the address of the reset interrupt vector is still taken from the 0x08000004 address, and jumps to the reset interrupt service program (Reset_Handler initializes the clock system, etc. in this function). After running the reset interrupt service routine, jump to the main function of IAP:

①After reset, take the address of the reset interrupt vector from the address 0x08000004 and jump to the reset interrupt service routine. After running the reset interrupt service routine, jump to the main function of the IAP.

Execute IAP function

Write the APP program to the specified flash address.

Execute Reset_Handler of APP program

After executing the IAP program, enter the interrupt vector table of the APP function . Get the address of the reset interrupt vector of the new program . And jump to execute the reset interrupt service routine of the APP program, and then jump to the main function of the APP program .

At this time, the FLASH of STM32F407 has two interrupt vector tables at different locations.

During the execution of the APP function, if the CPU receives an interrupt request, the PC pointer will still be forced to jump to the interrupt vector table at address 0x08000004, instead of the interrupt vector table of the new program , such as icon number ④

The program then jumps to the new interrupt service program corresponding to the interrupt source according to the interrupt vector table offset we set, as shown in the figure ⑤.

IAP program setting conditions

The new program must start at an address offset x after the IAP program; the
new program must start at an address offset x after the IAP program;

Guess you like

Origin blog.csdn.net/apythonlearner/article/details/133090271