current position:Home>1-stm32 + mn316 (nb-iot) remote upgrade OTA (self built Internet of things platform) - STM32 uses HTTP to download program files and upgrade programs through mn316 (MCU program rotation check and update)

1-stm32 + mn316 (nb-iot) remote upgrade OTA (self built Internet of things platform) - STM32 uses HTTP to download program files and upgrade programs through mn316 (MCU program rotation check and update)

2022-04-29 18:46:12Yang Fengwu

explain

This section tests STM32 adopt MN316(NB-IOT) Use http Remote download and upgrade MCU program

Tips : The upgrade program just puts bin The file data is written to the MCU , Which way to get bin File data is OK .

This section uses http To get the program bin file

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->STM32MN316BK

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:http://mnif.cn/ota/hardware/STM32MN316BK/user_crc.bin    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 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

GPRS Module and backplane installation instructions

test

1. Use the downloader to download BootLoader Program

Use usb Print serial port log

2. Download the user program to the development board

3. 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

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

STM32MN316BK: 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/STM32MN316BK/user_crc.bin

Then the download address of firmware information is :

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

2. modify   User programs (mcu_project)  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 STM32MN316BK)

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 :http://mnif.cn/ota/hardware/STM32MN316BK/info.txt)

5. 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

6. compile   User programs (mcu_project)

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

7. open OTA Tools Host computer software

8. Select the configuration according to the red box

9. Select the user program generated bin file

10. Click on Generate firmware

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

Two bin The difference between documents :

1.user_crc.bin File is in user.bin That's an increase from CRC Check bit

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

12. 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 ( And user programs (mcu_project) The settings inside are the same )

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

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

18. 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. open BootLoader perhaps In the user program iap_interface.h file

flash First stored BootLoader Program ,  Store and update relevant data at the end , Reserve some space for users to store other data

The rest is divided equally , A portion stores user programs , The other part is used to back up the user program

Users can adjust according to their own needs Flash Allocation size ,  Be careful :BootLoader Of programs and user programs Flash The distribution should be the same .

Users can adjust according to their own needs Flash Allocation size ,  Be careful :BootLoader Of programs and user programs Flash The distribution should be the same .

Users can adjust according to their own needs Flash Allocation size ,  Be careful :BootLoader Of programs and user programs Flash The distribution should be the same .

I set up BootLoader Occupy 50KB( Users can follow their own BootLoader programmatic bin Change the file size )

Store and update relevant data with at least 1K,  I set it as 2K( Use one page , Easy to store ) Don't change this user .

If the user also needs to use in the project flash Store the data , It can be adjusted according to the amount of data stored flash size

The starting address of data storage is :  FLASH_USERDATE_ADDR

2. After adjustment , download BootLoader Program , The serial port will print flash Specific distribution

The user needs to edit the user program according to the settings printed above (mcu_project) Operation address and occupied space

About upgrading the underlying package

1. Upgrade the underlying package into iap file and   iap_interface file

2. iap Files are mainly files that control the upgrade process

Can be in BootLoader How to download during the upgrade process is set in the program , How much cache to set

3. iap_interface It's the interface file

According to the transplanted MCU, the internal interface is realized , As long as the interface inside is completed, the upgrade program framework is completed

User programs (mcu_project) 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 = mnif.cn;

IAPStructValue.Port = 80;

IAPStructValue.Path = /ota/hardware/STM32MN316BK/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. Control module sending get Command to obtain the file recording firmware information on the server

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 Procedure details

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

network_module_extract_data Network data extraction 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

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, If no data is received for more than a period of time, it is also set to receive the data

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 the program runs , Over time, it will control the program restart

copyright notice
author[Yang Fengwu],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291651010135.html

Random recommended