Follow+Star Public Number, don’t miss exciting content
Source | Embedded Technology Development
Last week, I shared the design of the Bootloader Program Security Mechanism, today I will share the design ideas for the remote upgrade of microcontrollers.
In IoT applications, the remote IAP upgrade of MCU programs is a very useful feature. When there are program issues or updates needed, the upgrade can be performed on the server without the need for specialized personnel to be on-site, saving manpower and resources.
Basic Functions of Remote Upgrade
To achieve remote upgrades, the following basic functions must first be implemented:
Whether local IAP or remote IAP, this is the most basic function.
2. Wireless Communication
This can be achieved through various means such as WIFI, 4G, Ethernet, etc., depending on the actual project requirements.
3. Communication Protocol
Common communication protocols include TCP, HTTP, FTP, MQTT, etc. Among these, HTTP, FTP, and MQTT are application layer protocols based on TCP (transport layer). Users can also write simple communication protocols based on TCP themselves.
The design ideas for remote IAP and local IAP are the same; both require the design of the BOOT program and APP program. However, there are some considerations to keep in mind. The main issue is that remote upgrades need to consider network latency and even network interruptions. This is reflected in the following aspects:
Design Program Cache Area
Open a block of space in internal or external memory to store the program data received in packets.Once all data is received, it can be written to Flash for the upgrade.
This approach has the following two benefits:
1. Direct upgrade time may be relatively long, affecting normal user usage; increasing the cache area design can greatly reduce upgrade time.
2. During direct upgrades, if there are network issues, it may cause the device to fail to start properly and become a brick. After increasing the cache area design, even if the upgrade data transmission fails, it does not affect the normal use of the device.
Robust Communication Protocol
During local upgrades, the probability of data transmission errors is relatively low, but during remote design, more considerations are needed, such as two packets of data possibly arriving simultaneously.
Therefore, when designing the communication protocol, it must be more rigorous; the commands and data sent by the server need to be confirmed by the device.
At the same time, it is best to have some verification information, such as checksums, CRC checks, etc.
Even with the various safeguards mentioned above, there is no guarantee that the upgrade process will not fail. It is best to design a program backup area to run the backup program in case the program upgrade fails.
Below is a simple introduction to the steps for remote upgrades:
-
The server sends an upgrade request command.
-
The device replies that it has received the upgrade request command.
-
The server sends the program upgrade data. (Generally includes header, packet number, total number of packets, packet program data, checksum)
-
After the device receives the program data, it confirms that the checksum is correct, stores it in the program cache area, and replies to the server with the packet number to prevent packet transmission confusion.
-
After the server receives the reply, it sends the next packet of data until completion.
-
Once the program data transmission is complete, the server sends an upgrade completion command.
-
After the device receives the completion command, it replies to the server and writes an upgrade flag in a specified position in Flash, then reboots to enter the BOOT program.
-
The BOOT program reads the upgrade flag; if an upgrade is needed, it reads the program cache area data and writes it to Flash.
-
After the upgrade is complete, it jumps to the specified address in Flash to run.
● Column “Embedded Tools”
● Column “Embedded Development”
● Selected Tutorials from the Embedded Column
Follow the public accountReply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Click “Read the Original” to see more shares.