Highly reliable IAP online programming design based on STM32

Implementation principle of IAP online programming

The STM32 microcontroller based on the Cortex-M core responds to interrupts through an "interrupt vector table" internally. The starting address of the "interrupt vector table" is 0x8000000, and the starting addresses of different series of microcontrollers may be different.

When an interrupt comes, the internal hardware mechanism of STM32 will automatically locate the program counter PC pointer to the "interrupt vector table", and take out the corresponding interrupt vector to execute the interrupt service program according to the interrupt source.

After the STM32 starts, it enters the power-on reset, and first latches the value of the BOOT pin to determine the boot mode after reset. To realize online programming, the boot mode needs to be configured to start from the main flash memory, and then the reset is taken out from the "interrupt vector table" The interrupt vector executes the reset interrupt program to initialize the system, and guides into the main function to complete the startup.

The core of realizing online programming technology is a section of programming boot program that is pre-programmed inside the microcontroller.

insert image description here
First, the STM32 on-chip Flash main flash memory storage area is divided into 3 areas,

  • The starting address of the first block area is 0x08000000. This address must be the starting address of the on-chip Flash, which is specially used to store the boot program (BootLoader), which includes a vector table and main function;
  • The starting address of the second area is 0x08000000+m, which is specially used to store user application programs (APP), and also includes a vector table and main function;
  • The starting address of the third area is 0x08000000+n, which is specially used to store the programming state of the program.

The programming method is:

  • The bootloader in the first area can only be burned in through traditional JTAG or ICP.
  • The application programs in the second area are burned in through IAP online programming.
  • The programming state of the third area is read and written by the programming bootloader and the application program to confirm whether the application program needs to be programmed. It is divided into three states: pending programming, programmed and unprogrammed.

When allocating the size of the area space, it is necessary to combine the actual size of each area code to prevent overlapping areas, that is, the blank area in the figure must be greater than or equal to 0.

By modifying the link script of the STM32 project, the starting address of the program file written into the memory can be modified, so that the application program can be programmed to the designed fixed address according to the actual size of each program.

After the microcontroller is powered on, it first runs the programming bootloader. If there is a programming instruction, it will upgrade the program in the application storage area and change the programming status.
If no programming instruction is received, the program pointer jumps to the application program storage area and starts to execute the application program in the storage area.
When the programming instruction is received during the execution of the application program, it will jump to the programming boot program to perform the upgrade operation.

software process

A complete online programming system includes the upper computer sending end and the embedded terminal, which are connected by a communication bus. The embedded terminal includes the programming boot program BootLoader and the user application program APP.

insert image description here
The sending end of the upper computer mainly completes the reading, verification and sending of the Hex file.
First, send the programming instruction to the embedded terminal. If the embedded terminal feedbacks that the programming instruction is received successfully, it will send the verified programming data frame by frame. data frame.
insert image description here
The main function of BootLoader is to determine whether a software update is required. If an update is required, it will enter the program programming process. If not, it will guide and jump to the user program APP for execution.

After starting to run, first write the Flag as 3, indicating that it has not been programmed, and then check the programming status flag Flag cyclically.
If the Flag is 1, it means that the programming instruction has been received, and it will enter the process of data receiving, verification and programming. After the programming is completed, rewrite the Flag to 2, and jump into the APP for execution.
If the Flag is 2, it means that the programming has been completed, and it will directly jump to the APP for execution.
If Flag is 3, it means that there is no application program, and it will always run in BootLoader and wait for the program to be programmed.
insert image description here
The main function of the APP is to determine whether the programming instruction is received during the normal execution of the application. If it is received, rewrite the Flag to 1 and jump to the BootLoader for execution, directly entering the programming process. After the programming is completed, the Flag is changed to 2, and jumps into the APP execution, thus completing the online upgrade of the program.

Hex file generation and single frame message definition

Programming files generally have several formats such as ELF, HEX, BIN and AXF.
This project adopts the KEIL development environment, and the AXF file generated by default, in addition to the content of the BIN file, the AXF file also includes other debugging information, which can be directly used for debugging.
BIN files are pure binary codes, and AXF files can be converted into BIN files.
Compared with AXF and BIN files, HEX files are ASCII text files that record text lines, including address information and verification information, etc., which are more reliable and convenient for online burning. For writing development, related options can be configured in the KEIL environment to directly generate HEX format programming files.

insert image description here
Among them, TT is the type of data DD..., 0x00 indicates the data, 0x01 indicates the end of the file, 0x02 indicates the extended segment address, and 0x04 indicates the extended linear address. When the embedded terminal receives the message frame, it can know whether the programming is completed by judging the value of TT .

Relocation of the vector table

There are 2 independent codes BootLoader and APP in the internal storage space of STM32, which contain 2 different interrupt vector tables.
insert image description here
During the execution of the APP program, when an interrupt request comes, the PC pointer will still return to the interrupt vector table at address 0x8000004, not the interrupt vector table of the new program, such as ⑩, which is determined by the hardware mechanism of STM32.
Therefore, the interrupt vector table of the APP application needs to be offset. The offset is equal to the offset of the application start address relative to 0x08000000, and the value of m must be an integer multiple of 0x200.

There is a special vector table offset register VTOR in the SCB system control block of STM32

SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;

FLASH_BASE indicates that the vector table is located in the internal FLASH, and VECT_TAB_OFFSET is the offset address.

After the configuration is complete, when an interrupt request comes, it will execute 4, 5, 6.

Scatter-loading file configuration

The scatter-loading file can describe the location of the code and data in the load file and the runtime memory in a text file, which is used for linking by setting in KEIL. The scatter-loading area is divided into a loading area and an execution area.

BootLoader and APP have their own scatter-loading files. By configuring the corresponding start address start_address and length max_size, the BootLoader and APP2 codes can be stored in different locations.

Jump function implementation

During the running of the system, the program may jump from BootLoader to APP execution, or jump from APP to BootLoader execution.

When jumping from one program to another program, first check the validity of the jump address, then re-initialize the top address of the stack, and finally jump to the specified address for execution. Note that it cannot be executed in the interrupt processing function. jump.

insert image description here

High reliability design

During the online programming process, there may be various unexpected situations that cause the programming to fail, and it is also possible that the programming is successful, but the wrong data is burned.

insert image description here

Guess you like

Origin blog.csdn.net/Caramel_biscuit/article/details/131902463