STM32 uses IAP to update user programs

  
 

IAP, the full name is "In-Application Programming", Chinese is interpreted as "Programming in the program". IAP is a technique for updating internal programs of a microcontroller that is running a program through the external interface of the microcontroller (such as USART, IIC, CAN, USB, Ethernet interface or even wireless RF channel) (note that this is completely Different from ICP or ISP technology). ICP (In-Circuit Programming) technology is to program the MCU through the in-circuit emulator, while the ISP technology is the programming technology guided by the built-in bootloader program of the MCU. Whether it is ICP technology or ISP technology, it is necessary to have mechanical operations such as connecting the download line and setting the jumper cap. If the circuit board of the product has been sealed in the outer layer, it is difficult to update the program. If the product is installed in a difficult place such as a narrow space, it is a disaster. However, if IAP technology is introduced, the above-mentioned embarrassing situation can be completely avoided, and even if a long-distance or wireless data transmission scheme is used, remote programming and wireless programming can be realized. This is definitely not possible with ICP or ISP technology. The first prerequisite for a microcontroller to support IAP technology is that it must be a microcontroller based on reprogrammable flash. The STM32 microcontroller comes with programmable built-in flash memory, and the STM32 has a rich peripheral communication interface in both quantity and variety, so implementing IAP technology on the STM32 is completely feasible.

The core of implementing IAP technology is a pre-programmed IAP program inside the microcontroller. This program is mainly responsible for handshaking synchronization with the external host computer software, and then will receive the program data from the host computer software through the peripheral communication interface and write it to the flash area specified in the microcontroller, and then jump to execute the new write. The program eventually reached the goal of program update.

Before implementing the IAP program on the STM32 microcontroller, we must first review the internal flash organization structure of STM32 and its boot process. The internal flash address of the STM32 starts at 0x8000000. Normally, the program file is written from this address. In addition, STM32 is a microcontroller based on Cortex-M3 core. It internally responds to the interrupt through an “interrupt vector table”. After the program starts, it will first reset the interrupt vector from the “interrupt vector table”. The program is finished. The starting address of this "interrupt vector table" is 0x8000004. When the interrupt comes, the internal hardware mechanism of STM32 will automatically locate the PC pointer to the "interrupt vector table" and take the corresponding source according to the interrupt source. The interrupt vector executes the interrupt service routine. Finally, you need to know the key point. You can modify the starting address of the program file to be written to the flash by modifying the link script of the STM32 project.

IAP scheme is implemented on STM32 microcontroller. In addition to the conventional serial port receiving data and flash data writing and other conventional operations, it is also necessary to pay attention to the startup process and interrupt response mode of STM32. Figure 1 shows the normal running flow of STM32.
Figure 1

Interpretation of Figure 1 is as follows:

1. After STM32 reset, the address of the reset interrupt vector will be fetched from address 0x8000004, and the reset interrupt service routine will be executed. , as shown by the reference numeral (1) in FIG.

2. The final result of the execution of the reset interrupt service routine is to jump to the main function of the C program, as shown by the label (2) in Figure 1, and the main function should be an infinite loop, which is never The function returned.

3. During the execution of the main function, an interrupt request occurs. At this time, the hardware mechanism of STM32 will force the PC pointer back to the interrupt vector table, as shown by the label (3) in Figure 1. .

4. According to the interrupt source, enter the corresponding interrupt service program, as shown by the label (5) in Figure 1.

5. After the interrupt service program is executed, the program returns to the main function again, as shown by the label (6) in Figure 1. If the IAP program is added to the STM32, the situation will be as shown in Figure 2.
Figure 2

The interpretation of Figure 2 is as follows:

1, after STM32 reset, the address of the reset interrupt vector is fetched from the address 0x8000004, and the reset interrupt service routine is executed. Then, jump to the main function of the IAP program, as shown by the labels (1) and (2) in Figure 2. This process is consistent with the corresponding part of Figure 1.

2, after the completion of the IAP process (the new program is written inside STM32, the gray shaded square in Figure 2, the address starts at 0x8000004+N+M) and jumps to the new write. Enter the reset vector table of the program, take the address of the reset interrupt vector of the new program, and jump to execute the reset interrupt service routine of the new program, and then jump to the main function of the new program. The process is as shown in the label (3) of Figure 2. Show. The main function of the new program should also have the property of never returning. At the same time, it should be noted that two interrupt vector tables appear in different locations in the STM32 internal memory space.

3. In the process of executing the main function of the new program, an interrupt request will come, and the PC pointer will still be rotated to the interrupt vector table at address 0x8000004, instead of the interrupt vector table of the new program, as shown in Figure 2. The symbol (5) is shown in the middle. Note that this is determined by the hardware mechanism of the STM32.

4. According to the interrupt source, jump to the corresponding interrupt service, as shown by the label (6) in Figure 2. Note that this is the jump to the interrupt service routine of the new program.

5. After the interrupt service is executed, return to the main function. This is shown by the reference numeral (8) in Fig. 2.

From the analysis of the above two processes, it can be known that the program to be written using the IAP process must satisfy two requirements:

1. The new program must be from the IAP program. The address with the offset x starts;

2, the interrupt vector table of the new program must be moved accordingly, and the offset of the movement is x;

and the program start position is set. The method is (keil uvision4 integrated development environment) in the "Option" column of the "Option for Target…." interface in the "Page" column, "IROM"; "Start" is changed to the place where you want the program to start. As shown in Figure 3, the program start position is set to 0x8002000.
Figure 3

The method of moving the interrupt vector table is to add a function to the program:
void NVIC_SetVectorTable(u32 NVIC_VectTab, u32 Offset);

where the parameter NVIC_VectTab is the start of the interrupt vector table Position, and the parameter Offset is the address offset. If the interrupt vector table is moved to 0x8002000, the function should be called as follows:
void NVIC_SetVectorTable(0x8000000, 0x2000);

It is also necessary to remind the reader Yes, this function only modifies the structure variables used to store the interrupt vector in the STM32 program without substantially changing the physical location of the interrupt vector table in flash memory. For details, please study the program prototype.

With the above preparations, you can start designing an IAP solution, as follows:

1, after STM32 reset, use the state of a button to synchronize, when the button is pressed, it indicates that it will be performed. IAP process;

2, IAP process, send the program file to be updated to the STM32 USART1 device through the host computer software, STM32 receives the data and then writes the received data from the 0x8002000 address; Br>

3, STM32 uses the timer to judge whether the data is completely received, and the IAP process ends after the complete reception;

4. After resetting again, the jump 0x8002004 address starts to run the newly written program; Br>

The last few points to note:

1, the program file written by IAP is preferably a .bin format file, but not a .hex format file;

2, send program files to STM32 as slow as possible, because the write speed of STM32 to FLASH can not keep up with the speed of communication peripheral interface;

3, it is recommended to design a design between STM32 and host computer Set of handshake mechanism and error management mechanism, which can greatly improve the success rate of IAP;

Copyright © Windows knowledge All Rights Reserved