当前位置:网站首页>MCU realizes OTA online upgrade process through UART

MCU realizes OTA online upgrade process through UART

2022-07-06 00:41:00 strongerHuang

Focus on + Star sign public Number , Don't miss the highlights

0a074c2ae834ce3f1b05b000844bf307.jpeg

Material source | The Internet

OTA Upgrading is nothing new , Now most IOT terminal devices , Basically have this function .

Today with AT32 As an example, let me share with you OTA The detailed process of upgrading .

summary

Over the air technology OTA(Over-the-Air Technology) Is the user's own program in the process of running User Flash Part of the area to burn , The purpose is to conveniently pass through the reserved communication port after the product is released , Update and upgrade the firmware program in the product . Usually achieve OTA When the function , That is to say, the user program updates itself during operation , You need to write two project codes when you design the firmware program , The first program is Bootloader Area , Second project procedure App The code is the real function code , Perform applications and upgrades . These two parts of project code are burned at the same time User Flash in .

chart 1. OTA Code execution process

5e59b0c866eb831f9d3c75130fb7b4c5.jpeg

In the process shown above ,MCU After reset , from 0x08000004 Address take out the address of reset interrupt vector , And jump to reset interrupt service program , After running the reset interrupt service program, jump to Bootloader Of main function , As shown in the figure ① Shown ; The execution of the Bootloader in the future (App The code is in the figure FLASH Gray bottom part App The starting address of the reset interrupt vector of the program is 0x08000004+N+M), Jump to App Reset vector table of program , Take out App The address of the reset interrupt vector of the program , And jump to execution App Program reset interrupt service program , Then jump to App programmatic main function , As shown in the figure ② and ③ Shown , Again main The function is a loop , And notice that at this point AT32 Of FLASH, In different places , There are two interrupt vector tables .

stay main During function execution , If CPU Get an interrupt request ,PC Pointer is still forced to jump to address 0x08000004 At the interrupt vector table , instead of App Interrupt vector table of program , As shown in the figure ④ Shown ; Program according to our set interrupt vector table offset , Jump to the new interrupt service program corresponding to the interrupt source , As shown in the figure ⑤ Shown ; After executing the interrupt service program , Program return main The function continues to run , As shown in the figure ⑥ Shown .

Through the analysis of the above two processes , We know OTA The program must meet two requirements :

1) App The procedure must be in Bootloader Some offset after the program is x The address begins .

2) Must be App The interrupt vector table of the program moves accordingly , The offset of the movement is x.

AT32 USART OTA Quick ways to use

Hardware resources

Is used in the document AT-START-AT32F403A Take the hardware condition of the experimental board as an example ,OTA demo The source code also includes AT32 Other models , Users only need to compile the corresponding model project and burn it in AT-START Just run the test board .

1) Indicator light LED2/LED3/LED4

2) USART1(PA9/PA10)

3) AT-START Experiment board

Software resources

1) tool_release

● IAP_Programmer.exe,PC machine tool, For demonstration OTA Upgrade process

2) source_code

● Bootloader,Bootloader Source program , function LED2 flashing

● App_led3_toggle,App1 Source program , function LED3 flashing

● App_led4_toggle,App2 Source program , function LED4 flashing

notes : The project is based on keil v5 establish , If the user needs to use on other compilation environments , Please refer to the corresponding BSP Catalog AT32F403A_407_Firmware_Library_V2.x.x\project\at_start_f403a\templates Various compilation environments in ( for example IAR6/7/8,keil 4/5,eclipse_gcc) Make corresponding modifications .

OTA Demo Use

This document describes two commonly used OTA application demo,template app and dual app, Later chapters will introduce .

1) open Bootloader Engineering source program , Select the corresponding MCU Model number target Download to the experimental board after compilation

2) open IAP_Programmer.exe

3) Choose the right serial port 、APP Download address and bin file , Click on Download download , Here's the picture

4) Observe LED2/3/4 flashing ,LED2 flashing -Bootloader Work ,LED3 flashing -App1 Work ,LED4 flashing -App2 Work

chart 2. IAP demo Upper computer

96b8e1b8487d8213b4632a6aa48c7f96.jpeg

Template app OTA Program settings

Address distribution

chart 3. Flash Address assignment

a9b9104ec9adc5deb76d471b2eb22312.jpeg

notes :Bootloader The last sector of the region , Used to store and prevent errors in the upgrade process ( Power failure and other abnormal conditions ) Of flag, Users compile and modify Bootloader when , Make sure not to cover flag The address of .

Execute the process

OTA It is divided into Bootloader、App and Template In the third part of , Apply to App In the implementation of ,Template As new only App Temporary storage space for firmware data . The overall flow chart of program execution is as follows :

chart 4. Program execution flow

b41928b82274e49711330dadff0a9cd7.jpeg

Bootloader project Set up

1) Keil Set up

chart 5. Bootloader project in address 1 stay Keil Set up

1d66ed699c3ecf5720a4b62ebe3a7165.jpeg

2) Bootloader Source program modification ota.h In file

chart 6. Bootloader project in address 2 Set... In the program

22d143d271bb6f1e6c8ced8fea80d52d.jpeg

App project Set up

OTA demo Provides 2 individual App The program is for testing , All with address 2(0x800 4000) Is the starting address .App1 LED3 flashing ,App2 LED4 flashing . With App1 For example , The design steps are as follows :

1) Keil Engineering setup

chart 7. App project in address 2 stay Keil Set up

29ff36c876961a590d5e8f1b533cb52c.jpeg

2) App1 Source program settings

chart 8. App project The vector table offset is set in the program

57f9eacd2322d3e19460aa4ca98a49da.jpeg

3) Compile the generated bin file

adopt User tab , Set call after compilation fromelf.exe, according to .axf File generation .bin file , be used for OTA to update . Through the above 3 A step , We can get one .bin Of APP Program , adopt Bootloader The program can be updated .

4) Turn on debug app code function

If you're designing App code In the process, we need to App project Conduct separate debugging , Please follow the steps below .

●  To download Bootloader engineering

●  Re commissioning App engineering

Dual app OTA And program settings

Address distribution

chart 9. Flash Address assignment

f81684b922b43895b5f4a4fd78ba7619.jpeg

notes :Bootloader At the end of the zone 2 Sectors , To hold App Is it normal flag, Users compile and modify Bootloader when , Make sure not to cover flag The address of .

Execute the process

OTA It is divided into Bootloader、App1 and App2 In the third part of , Apply to App1 or App2 In the implementation of . The overall flow chart of program execution is as follows :

chart 10. Program execution flow

77e12a398a2368568e1ca8ec1740d608.jpeg

Bootloader project Set up

3) Keil Set up

chart 11. Bootloader project in address 1 stay Keil Set up

185c7bad4dd2c715e729e0bc52b1328c.jpeg

4) Bootloader Source program modification ota.h In file

chart 12. Bootloader project in address 2 Set... In the program

0cfd89f4e548c48182cf4b28e593b581.jpeg

App project Set up

OTA demo Provides 2 individual App The program is for testing ,app_led3_toggle With 0x800 4000 Is the starting address ,app_led4_toggle With 0x8080000 Is the starting address .App1 LED3 flashing ,App2 LED4 flashing . With App1 For example , The design steps are as follows :

5) Keil Engineering setup

chart 13. App project in address 2 stay Keil Set up

96b92ae8689d3fc9529539f851b48c4b.jpeg

6) App1 Source program settings

chart 14. App project The vector table offset is set in the program

7d935f5ed7699925ad10d591dd3c4b1e.jpeg

7) Compile the generated bin file

adopt User tab , Set call after compilation fromelf.exe, according to .axf File generation .bin file , be used for OTA to update . Through the above 3 A step , We can get one .bin Of APP Program , adopt Bootloader The program can be updated .

8) Turn on debug App code function

If you're designing App code In the process, we need to App project Conduct separate debugging , Please follow the steps below .

● To download Bootloader engineering

● Re commissioning App engineering

Bootloader/App Serial communication protocol with upper computer

The program communicates with the upper computer , Receive firmware upgrade data , The communication protocols between the upper computer and the embedded end are as follows :

1) Upper computer communication protocol

chart 15. Upper computer communication protocol

5fb673fbb9f60f66c5d98536a1414755.png

2)  Embedded end lower computer communication protocol

chart 16. Lower computer communication protocol

7352458622bcf023ce98e04930e30c0a.png

notes ACK0xCCDD

NACK0xEEFF

Data0x31+Addr+ data +chenksum(1byte)

Addr:4bytes, High in the former

Kbytes, Download data , Insufficient 2K Content filling 0xFF

Checksum:1byte,4bytes Of Addr+2KBytes The lower eight bits of the checksum of the data

------------ END ------------

b4cc6758d6af56fc631c88e9357dd4d4.gif

● special column 《 Embedded tools

● special column 《 Embedded development 》

● special column 《Keil course 》

● Embedded column selection tutorial

Pay attention to the reply of the official account “ Add group ” Join the technical exchange group according to the rules , reply “1024” See more .

efcf232a13bd29bafbbcbf96367a489d.jpeg

8412fe00d8babb21f902c0e21a8ce79c.png

Click on “ Read the original ” See more sharing .

原网站

版权声明
本文为[strongerHuang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060033406931.html