Follow the + star public account and never miss exciting content
Reprinted from | Embedded technology development
Last week I shared with you the " Bootloader Program Security Mechanism Design ". Today I will share with you the design ideas for remote upgrade of the microcontroller.
In IoT applications, remote IAP upgrade MCU program is a very useful function. When a program problem occurs on the device or a program needs to be updated, the device only needs to be upgraded on the server. There is no need for dedicated personnel to go to the site to perform the upgrade, saving manpower and material resources.
Basic functions of remote upgrade
To achieve remote upgrade, you must first implement the following basic functions:
1.Flash reading and writing
Whether it is local IAP or remote IAP, this is the most basic function.
2. Wireless communication
It can be implemented through WIFI, 4G, Ethernet and other methods, and can be selected according to actual project requirements.
3. Communication protocol
Commonly used communication protocols include TCP, HTTP, FTP, MQTT, etc. Among them, HTTP, FTP and MQTT are all application layer protocols and are implemented based on TCP (transport layer). Users can also write a simple communication protocol based on TCP to implement it.
The design ideas of remote IAP and local IAP are the same, and both require the design of BOOT program and APP program. But there are some things to note. Mainly, remote upgrades need to consider network delays or even network interruptions. This is reflected in the following aspects:
Designer cache
Create an area in internal or external memory for storing program data received in subpackages. Wait until all data is received and then write it to Flash at once for upgrade.
This has the following two advantages:
1. Direct upgrade may take a long time, affecting the normal use of users. Increasing the cache area design can greatly reduce the upgrade time.
2. When upgrading directly, if there is a problem with the network, the device may not start normally and become bricked. After adding the cache area design, even if the upgrade data transmission fails, the normal use of the device will not be affected.
Robust communication protocol
The probability of data transmission errors during local upgrade is relatively small, but when designing remotely, more considerations need to be made, for example, the two packets of data may arrive at the same time.
Therefore, when designing the communication protocol, it must be more rigorous. The instructions and data sent by the server require a confirmation reply from the device.
At the same time, it is best to have certain verification information. , compare checksum , CRC check , etc.
Program backup design
Even with the above various design guarantees, there is no guarantee that the upgrade process will not fail. It is best to design a program backup area and run the backup area program when the program upgrade fails.
The following introduces the steps for a simple remote upgrade:
The server sends an upgrade request command.
The device responds that a request to upgrade command has been received.
The server sends program upgrade data. (Generally including package header, package number, total package number, subcontracted program data, and verification)
After receiving the program data, the device confirms that the verification is correct, stores it in the program cache area, and replies to the server according to the packet number to prevent transmission packet confusion.
After the server receives the reply, it sends the next packet of data until the end.
After the program data is sent, the server sends an upgrade end command.
After receiving the end command, the device replies to the server. And write the upgrade mark in the designated location of Flash, and restart to enter the BOOT program.
The BOOT program reads the upgrade flag. If an upgrade is required, the program cache data is read and written to Flash.
After the upgrade is completed, jump to the specified address of Flash to run.
------------ END ------------
●Column "Embedded Development"
●Embedded column selected tutorials
Follow the official account and reply " Add Group " to join the technical exchange group according to the rules, and reply " 1024 " to view more content.
Click " Read the original text " to view more sharing.