Design of stm32 bootloader (ymodem Protocol)

Source: Internet
Author: User
Tags file transfer protocol

I believe many people want to upgrade the firmware, just like the ones on the mobile phone, if the firmware is not enabled. The following describes how to implement the bootload function.

Some time ago, I had a project on the bootload design. So I carefully studied it. I used to use the official stm32 and did not go into details. After completing this operation, I found some problems with the official version. For example, during the ymodem transmission process, the data zone is not verified at all, but the serial number is checked and then burned. WholeProgramIt is totally blocking, and a lot of CPU is wasted for useless work. Of course, this will not take much time to upgrade the program. There is an important question, officiallyCodeYou can only use a Super Terminal for transmission. In this way, if you are using 64-bit win7, there is no way to upgrade. Only XP or 32-bit Windows 7 can use super terminals. 64-bit Super Terminal in win7 cannot be used. However, the securecrt tool can be used in 64-bit win7, but the official code does not support it. Securecrt supports the original ymodem protocol. The first packet does not contain the total number of bytes. The ymodem under the Super Terminal should be in the ultimate version, so the official dome can only be transmitted under the Super Terminal. Ymodem is described in detail below. You will know the reason.

First, we provide a simple bootloader project.

Http://pan.baidu.com/share/link? Consumer id = 373630 & UK = 118334538

Supported protocols: ymodem and ymodem-G.

Supported PC software: Super Terminal, securecrt.

Http://pan.baidu.com/share/link? Consumer id = 373637 & UK = 118334538

 

Ymodem protocol:

The ymodem protocol evolved from the XMODEM protocol, with each packet containing 1024 bytes of data. It is a very efficient file transfer protocol. The following are some related documents. I would like to thank some friends who have followed me on Weibo for their materials. Saving a lot of time.

Next, let's take a look at the complete handshake process of the ymodem protocol transmission:

Sender: the sender.

Receiver: receiver.

The first step is to send a character 'C' to the receiver'

After receiving the 'C' packet, the sender sends the first packet with the following content:

Soh 00 FF Foo. c NUL [123] CRC

1stByte Soh: Indicates that the data area of this package is 128 bytes. If the header is stx, the Data Partition size of the package is 1024.

2ndByte 00: Number. The first package is 00, the second package is 01, and the third package is 02. Continue to increase from 0 after ff.

3rdByte FF: ID. 00 corresponds to FF, 01 corresponds to Fe, and so on.

 

4thByte to the last two bytes: If 1st bytes are Soh, there are 128 bytes, and STX is 1024 bytes, this is divided into data areas. "Foo. c" file name. In the Super Terminal, there is a file size after the file name. The official dome also uses this file size for comparison. This is why the ymodem protocol in securecrt cannot be correctly transmitted.

If the file name and size are less than 128 bytes after the file name and size, fill it with 0.

Last two bytes:Note that only the data part is involved in CRC verification, excluding the header and encoding part.

16-bit CRC verification, with high bytes in front and low bytes in the back.

 

After receiving the first packet, the receiver sends an ACK to respond correctly.

Then, send a character 'C '.

After receiving 'C', the sender starts sending the second frame. The second frame stores the first packet of data.

After receiving the data, the receiver sends an ACK and waits for the next packet to complete data transmission. The ack response is resumed. Until all data transmission is complete.

After the data transmission is completed, the sender sends an EOT. The first time the receiver replies with a Nak, the sender confirms the request again.

After receiving the Nak, the sender resends the EOT, and the receiver replies with Ack when receiving the terminator for the second time.

The receiver then sends a 'C' message. If the sender has no second file to transmit,

Send the following data

Soh 00 ff 00 ~ 00 (a total of 128) crch CrCl

After the recipient responds to ACK, data transmission is officially completed.

 

The above section describes the basic operation process of the ymodem protocol.

 

Stm32 bootloader software design

At the beginning, I started to use the basic idea of the software. I can use the serial port to receive data and write data to the Rom. The two can be synchronized. In this way, the CPU can be used to the maximum extent.

For example, to receive a frame of 1024 data, the time used = the time used for the current baud rate single character * (1024 + 1 header + 2 encoding + 2crc) = (1/115200*10) * (1024 + 1 + 2 + 2) = 89.323 Ms

That is, the data of the previous frame can be written to the ROM during the 90 ms or so of receipt. The data received by the serial port is interrupted, so the write and receive operations are basically synchronous. The procedure is as follows:

Stm32 bootloader usage:

This stm32 bootloader program is easy to use. If you haven't used the IAP upgrade method before, it doesn't matter. The following will be detailed descriptions.

 

Preparations:

Hardware: There is a serial port target board 1 and a serial port connection line.

Software: PC tool: Super Terminal or securecrt, stm32 target board program. Bin, stm32 bootloader.

 

1) first download the above project stm32 bootloader to the target board;

2) Open the Super Terminal or securecrt, set port 115200, stop bit 1, data bit 8, and verify the data.

3) Press 'C' to power the target board. (pay attention to the order)

 

4) select 1, and then use ymodem and ymodem-g to send the "stm32 target board program. bin" file.

5) after the transfer is completed, it runs automatically.

 

Note: The following two items are adjusted based on your needs:

Stm32 bootloaderModify:

Find the common. h file in the project:

The following three macros are defined based on the requirements of the target board:

# Define applicationaddress 0x8002000 // The first address of the program

# Define applicationsize 120000 // reserved space for the target program

# Define stm32f10x_hd // target board chip type

 

Stm32Target Board program. BinOffset address modification:

There are two places:

1. Find system_stm32f10x.c.

# Define vect_tab_offset 0x2000 /*! <Vector table base offset field.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.