current position:Home>1-ch579m program upgrade OTA (self built Internet of things platform) - ch579m uses its own Ethernet to remotely download and upgrade MCU program through HTTP (MCU program rotation check and update)

1-ch579m program upgrade OTA (self built Internet of things platform) - ch579m uses its own Ethernet to remotely download and upgrade MCU program through HTTP (MCU program rotation check and update)

2022-04-29 17:45:08Yang Fengwu


This section tests CH579 Use through the built-in Ethernet http Remote download and upgrade MCU program

I've put the firmware file on my own server

Default to use the download path provided by me

File path :  Site root ->ota->hardware->CH579MBK

user_crc.bin:  Is the firmware program file .

The firmware program file is not a file that can be run directly

The data inside every 128 Add after byte 2 position CRC Check bit

After downloading the single chip microcomputer, every 130 Byte check data , And then put the front 128 Byte write Flash.

Join in CRC Verification makes the upgrade stable and reliable .

info.txt The contents of the document :

version:0.0.1  Cloud firmware program version

 url:    Firmware program download address

"info":"1. Solved part of BUG  2. Some programs are optimized "        Use APP Control upgrade ,APP A reminder of .

Upgrade process :

The whole is to use TCP Connect Web The server , Then send the corresponding GET Instruction to get files !

The user program controls the use of network modules at regular intervals http Of GET Command to get the cloud info.txt file

Then get the firmware program version and firmware program download address in the server from the file

If it is inconsistent with its version number , Just write the firmware program download address flash, Then set the update flag , restart ,

Restart execution BootLoader in the future ,BootLoader The program detected an update flag , Then extract the firmware download address ,

And then use http Download program files , Write program file flash, Upgrade Complete .

Backup and upgrade flowchart

State in advance  

Before learning this section, users must have personally tested and learned the following chapters

CH579 Address offset without interruption , It relies on the interrupt function of the lowest program to turn around .


1. Use a network cable to connect a router or switch

2. Open the serial port debugging assistant and print the serial port log

3. There are three programs

base It is the program responsible for switching interrupt ( This can be downloaded once )

BootLoader The program is responsible for downloading, upgrading and jumping to execute the user program

UserApp It's our own user program

4. First download base Program

5. Then download BootLoader Program

5. Serial port log

BootLoader The program is printed as follows

6. Finally download UserApp Program

7. Watch the log , Explain the execution process

Start running the user program , The program version of the user program settings provided by default is 0.0.0

The user program control module is connected to the server , Use get Command to obtain the firmware information file on the server info.txt

After the version inconsistency is detected , Take what you get url Store in flash, Set update flag , restart .

After reboot BootLoader extract url

BootLoader send out get Instruction acquisition program firmware

The data will be verified during the upgrade process

After the upgrade , Run the newly downloaded program .

The user program controls the module to connect to the server at regular intervals , Use get Command to obtain the firmware information file on the server info.txt

Now the program version is the same as that above , So there's no need to update

Apply to your own server

1, First, determine the path of the program file on the server

About my server folder explanation :( Just feel free , The last folder name is recommended to use the model of the product )

html: Site root

ota: ota upgrade

hardware : Hardware program

CH579MBK: As a product model ( Modify according to your own product model )

I put the firmware in it, so the download address of the firmware program is :

http:// The server IP Address /ota/hardware/CH579MBK/user_crc.bin

Then the download address of firmware information is :

http:// The server IP Address /ota/hardware/CH579MBK/info.txt

2. modify   User programs   Of IAP.C file

Tips : I store them in the program bin Of documents 1024 The position of byte multiples is for BootLoader It is easy to extract these data when downloading ;

1. Product model ( I set it to CH579MBK)

2. Modify firmware program version ( You can specify , I set it to 0.0.2)

3. Modify the download address of the cloud firmware information file ( My :

3. modify BootLoader Program   Of IAP.c, Set the product model and the default firmware program download address

Be careful :BootLoader The product model inside should be the same as that inside the user !!!!

BootLoader When downloading the program, it will be judged that this model is inconsistent

4. Compiling user programs

It'll be in the engineering catalog bin Folder generation user.bin file

5. open OTA Tools Host computer software

6. Select the configuration according to the red box

7. Select the user program generated bin file

8. Click on Generate firmware

9. Will be in user.bin Directory generation user_crc.bin file

10.user.bin and user_crc.bin The difference between documents

user_crc.bin File is in user.bin That's an increase from CRC Check the data

rewrite size, Fill in the actual file size to bin file

11. Generative user_crc.bin Copy the file to the server

13. One less info.txt file

I prepared a template

14. Change the version number ( It's the same as the user program )

15. Modify the firmware program download address ( Be consistent with that on the server )

16. The last message doesn't need to be changed , I can't use it right now

17. hold info.txt Files are also copied to the server , Then follow the initial steps to test

A small summary

In fact, the remote upgrade of MCU program is to use the module to TCP Way to connect Web The server ,

And then to TCP The server sends data , The data format is GET Instructions .

After receiving the instruction, the server will send the file to the module , The module sends data to MCU through serial port ,

After receiving the data, the MCU writes to flash, Finally, load and run .

About Flash Allocation adjustment

1. MCU user application program Flash It's from 0 Address starts , in total 250KB

2. Now it's the initial address 1K Give it to base Program usage

3. The rest of the space allocation can be seen BootLoader Or in the user program iap_interface The header file

FLASH_MCU_BASE 1*1024      base The program is used 1K

FLASH_MCU_SIZE 249             base The program is used 1K, in total 250K, So there's still 249K

FLASH_SECTOR_SIZE 512      Flash Minimum space per erase

FLASH_IAP_SIZE 45                  to BootLoader Allocated 45K

FLASH_UPDATE_SIZE 1           When updating, you need to use Flash Record some information , Allocated 1K, This minimum is 1K

FLASH_USERDATA_SIZE 2      If users need to store their own data (KB), You can set this according to the amount of data you need to store

After setting up the above space , The rest of the space will be divided equally between the operation area and the backup area

3. When we do the project, we just need to write it well BootLoader It won't move any more , So I usually finish writing BootLoader in the future ,

look down BootLoader How much space is used , Then set it up BootLoader Space

Like the present BootLoader Used 41KB

4. Suppose I set BootLoader Occupy 45KB( Reserve some , Prevent adding programs later ),  First of all BootLoader In the program

At the beginning 1K Space is given base Program , BootLoader It's from No 1K Address to start running , So it is 0x400

BootLoader Occupy 45KB So it is 0x5000

5. Then download again base Program and  BootLoader Program

Watch the printed log

6. modify base The user program running address inside is 0xb800

7. To configure UserApp Of the user program iap_interface.h Inside flash Distribute content , Want to be with BootLoader The inside remains the same

9. To configure UserApp User program running address and occupied space

9.flash That's all for the distribution adjustment

Be careful :

flash The assigned address is set before the product is put into use , I can't move anymore .

The big guy must give BootLoader Leave enough space . Late prevention needs to be in BootLoader Develop other program functions inside .

About upgrading the underlying package

1. Upgrade the underlying package is the following two files

iap Is the file that controls the upgrade process , Specifically, the functions controlled by this file will be described below .

iap_interface It's the interface file , In the process of upgrading, the program inside is finally called .

2, About the post maintenance of upgrading the underlying package , Optimize

Maintenance optimization is only for iap file , Users just need to put iap Just replace the file ! ( Users can change their internal bug, But it is recommended that users keep IAP The file is the latest )

3. iap Is the file that controls the upgrade process

stay BootLoader The model can be set inside and Default upgrade address

You can set the model in the user program , Program version number and Firmware information file download address

BootLoader Inside iap.h In the document You can also set how to download during the upgrade process , How much cache to set

4. iap_interface It's the interface file

When transplanting, users need to realize the internal interface according to their own MCU , As long as the interface inside is completed, the upgrade program is completed

4. Tips

In the following chapters, a separate section will be taken out to write the specific process of transplanting to other single chip computers .

Or that sentence : Code is not just for others to see , It's for others .

Only when the code is widely used can it really be used by others .

User programs (UserAPP) explain

1. Parsing info.txt Download path ( A file that records firmware information on the server )

Do this procedure is to save the user analysis of the cumbersome .

After performing the parsing :

IAPStructValue.IP =;

IAPStructValue.Port = 80;

IAPStructValue.Path = /ota/hardware/CH579MBK/info.txt

4. Processing updates ( This program needs to be executed in the user program when there is no problem with the user program )

explain :

BootLoader Some update status will be set when updating the program ; The user program needs to call this function to clear the update status .

If the user program does not clear the update status , So once the program restarts BootLoader After checking the update status, the program will roll back automatically .

5. Get the firmware information from the server

6. analysis info.txt The contents of the document

If the version number is different , Extract and store url Then set the upgrade flag , restart .

7, Add : If the following warning appears when compiling the user program

In fact, it's this place that leads to

For convenience BootLoader Program extraction user program bin The model number in the file , The model is stored in the offset 1024 Location of bytes .

The storage location of the set string affects the location of some arrays allocated by the chip itself by default . Will report the above warning .

It doesn't seem to affect anything ...

BootLoader Program description

1. see IAPInit function

2. Get the stored firmware download url, And analyze url

3. If there is an update sign , Then back up the user program

4. If there is no update flag , Check the update status

If the status is update with error , Then perform a rollback , If no backup program is detected , Just redo the upgrade

5. If the update status is 0x01, Set the update status to 0xFE

7. Control module connection TCP The server (Web The server )

8. send out get Command to get program files

8. stay TCP Write firmware data into the cache in the receive data function

MCU communicates with the module through serial port , So receive in the serial port interrupt

The bottom layer parsing is done , After automatic parsing, it is stored in the cache

9. Fetch data from cache , And write flash

11, If the corresponding number of files is received or no data is received for more than a period of time

Set up IAPStructValue.ReadDataEndFlag = 1; It is considered that the reception is complete

12, Make various judgments after receiving

If it's received , Then write 0x01 state , restart .

If there is an error , Try downloading it again .

13, If the program file is downloaded, restart it , After restart, check that the update status is 0x01 The update status will be set to 0XFE

14, Then load and run the user program

15, No data received for more than a period of time , It's a judgment made here

16, Customers can be in IAP.h Modify the default timeout

The overall run timeout is BootLoader The timing that has been accumulated as soon as it runs , Over time, it will control the program restart

copyright notice
author[Yang Fengwu],Please bring the original link to reprint, thank you.

Random recommended