STM32 + IAP + Ymodem: A Perfect Combination

Follow andstar the publicaccount, don’t miss out on great content

STM32 + IAP + Ymodem: A Perfect Combination

Author: strongerHuang

For software engineers, code upgrades (or program updates) are essential knowledge.

This article will introduce the basics of programming, along with the official STM32 demo to discuss programming content.

1. About ISP, ICP, IAP

1. ISP

ISP: In System Programming.
For example: programming STC chips using STC-ISP, programming STM32 using Flash loader, etc.
Chips that support ISP generally have a boot program embedded in the chip for upgrading.
2. ICP
ICP: In Circuit Programming.
ICSP: In-Circuit Serial Programming. For example: programming EEPROM, etc.
ICP programming methods have various interpretations online; literally (in circuit), all chips being programmed need to be powered on and are in the circuit. Strictly speaking, programming with tools like J-Link, ST-Link, e-Link32 also falls under in-circuit programming (ICP).
On Wikipedia, in-system programming (ISP) is also referred to as in-circuit serial programming (ICSP).
3. IAP
IAP: In Application Programming.
This is the focus of this article, which can be simply understood as programming (upgrading the program, updating firmware) during program execution.
IAP is when the user’s own program writes to a portion of User Flash during its execution, aiming to conveniently update the firmware program in the product through a reserved communication interface after product release. [Source: Baidu Encyclopedia]
IAP Communication Interfaces
There are many types of IAP communication interfaces: UART, ETH, I2C, SPI, etc. In principle, any communication interface capable of data transmission can implement IAP functionality.
This article uses the example provided by STM32’s official website, using UART for application programming (IAP).
2. About Ymodem Protocol
YModem is a file transfer protocol evolved from the XModem protocol, allowing for data packets of up to 1024 bytes, making it a very efficient file transfer protocol.
More information can be found on Baidu Encyclopedia:

https://baike.baidu.com/item/Ymodem

Protocol Transmission Process:

STM32 + IAP + Ymodem: A Perfect Combination

Some ASCII codes for certain characters

Character ASCII Code (Hex)
<span>SOH</span> <span>0x01</span>
<span>STX</span> <span>0x02</span>
<span>ACK</span> <span>0x06</span>
<span>NAK</span> <span>0x15</span>
<span>EOT</span> <span>0x04</span>
<span>C</span> <span>0x43</span>
1. Start Frame
SOH + 00 + FF + filename + filesize + NULL + CRCH + CRCL
The start frame is the first important message sent by the file transfer sender.
filename indicates the name of the file being transferred.
filesize indicates the size of the file to be transferred.
CRCH + CRCL indicates the CRC16 checksum of the entire frame (excluding the first three bytes).
2. Data Frame Format
STX/SOH + [number] + complement of the number + data[0] + data[1] + data[2] + … + CRCH + CRCL
SOH indicates 128 bytes; some also use SOH to transfer data.
STX indicates 1024 bytes.
CRCH + CRCL indicates the CRC16 checksum of the entire frame (excluding the first three bytes).
If the last byte of transmission is less than 128 bytes, it is padded with 1A.
3. End Frame Data Format
SOH + 00 + FF + NULL + NULL + … + NULL + CRCH + CRCL

The above content is authorized from:

https://blog.csdn.net/weixin_41294615/article/details/104652105

3. ST Official IAP Examples
ST’s official website provides many IAP examples, such as:
Libraries: Some use Standard Peripheral Libraries (SPL), others use Hardware Abstraction Layer (HAL) libraries;
Communication Ports: Some use USART, others use I2C, ETH, etc.
MCU Models: Almost the entire series including STM8S, STM32F1, F4, L1, etc.
The various IAP methods and principles provided by ST are actually similar: they write the program file (binary file) into FLASH.
This article uses the simplest IAP example based on (STM32F10x) using SPL library + UART to explain. (Complexity starts from the basics, and I will gradually update more advanced IAP features later.)

STM32F10xxx in-application programming using the USART Official address:

https://www.stmicroelectronics.com.cn/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-library-expansion/stsw-stm32008.html

Note: The public account does not support external links, please copy the link to the browser to download

STM32 + IAP + Ymodem: A Perfect Combination

Related reference documents and code examples can be downloaded from this website. The next chapter will discuss some important points about this IAP.

I uploaded it to Baidu Cloud after downloading:

https://pan.baidu.com/s/1umdwlShsJpL8rLdeb9n6Cw

Password: 2t3j

Note: This link may expire later, it is recommended to download from the official website

4. Key Points of IAP Examples

1. Overview of the Example

This code project is based on STM32F1, using UART communication, utilizing a super terminal (host computer) to transfer and write program data to Flash via YModem protocol.

2. IAP Software Project

We unzip the downloaded software package and enter the directory, using MDK-ARM (as an example), as shown below;

STM32 + IAP + Ymodem: A Perfect Combination

This article uses MDK-ARM as an example; you can also open it with EWARM, TrueSTUDIO, and other tools.
Since this project was previously created with MDK-ARM version 4, opening it with version 5 will prompt the following message:

STM32 + IAP + Ymodem: A Perfect Combination

Simply click “Migrate to Device Pack” to proceed; version 5 uses a “Device Support Package,” meaning it is compatible with older version projects.
Select the corresponding target (we take F103ZE as an example, selecting large and small capacities depends on their FLASH page sizes), choose 10E-EVAL, as shown below:

STM32 + IAP + Ymodem: A Perfect Combination

Directly compile without errors or warnings, and if you have the corresponding board, you can download it directly.

Note:

1. Prerequisite: Download and install MDK

2. What is Target? Briefly describe workspace, project, and target

3. Explanation

This program is only the IAP program, placed at the starting address of 0x0800 0000. The role of the IAP program is to download the application program (binary file) to the corresponding address of the application program.
Therefore, we will divide FLASH into two areas: IAP program area and APP program area.

STM32 + IAP + Ymodem: A Perfect Combination

The IAP program has two functions: 1. Programming the APP program; 2. Guiding the program to jump to the application program. Therefore, this APP program is also called BootLoader.
To prevent the IAP program from being accidentally corrupted (the first part of the code), it is generally recommended to “write protect” the IAP program.

How did this APP address 0x0800 3000 come about?

See the IAP program code:

STM32 + IAP + Ymodem: A Perfect Combination

Thus, the starting address of our APP application program must correspond to 0x0800 3000. Otherwise, if it jumps and there is no application program, it will cause an error.

Note:

It is suggested to choose execution of IAP or APP through a key press; generally, it is recommended to use a countdown (e.g., 5s): choose to download via serial port within 5s, otherwise automatically jump to APP program execution. You will need to modify the code based on your situation.
The demo can jump to the APP program by selecting 3; beginners can directly test without using key judgments, as shown below:

STM32 + IAP + Ymodem: A Perfect Combination

5. APP Code and Address Issues

The previous chapter discussed the IAP program, this section discusses the issue of modifying the address of the APP program.

1. Adding Code

Set the vector table offset at the beginning of the code:

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x3000);

0x3000 represents the offset

2. Modify Configuration

project -> Options

STM32 + IAP + Ymodem: A Perfect Combination

3. Generate Bin

For details, you can refer to my other article:

How to Generate Bin Files in MDK-ARM

4. APP Program Code

To facilitate everyone’s learning, I provide the APP code project modified according to the above for download, the address is the same as above:

https://pan.baidu.com/s/1umdwlShsJpL8rLdeb9n6Cw

Password: 2t3j

6. Super Terminal Installation and Program Download

1. Download and Install Super Terminal

The official document provides the HyperTerminal super terminal download program, which can be searched and downloaded on Baidu or Google. I have been using SecureCRT, which is commercial software but can be found with many cracking methods online.
For convenience in debugging, I provide a download link here

https://pan.baidu.com/s/13yjA9q0Eltfer4oLPbGceQ

Password: 5r5m

Note: This link may expire later, it is recommended to download from the official website

2. Program Download

After connecting the hardware, place the generated program file (Demo.bin) in the specified location (for testing convenience, I copied it to the desktop) -> open the terminal -> press 1 on the keyboard -> select the file -> download complete -> press 3 on the keyboard to execute the program.

STM32 + IAP + Ymodem: A Perfect Combination

The above content is about the basic knowledge and example explanation of STM32 + IAP + Ymodem, hoping it is helpful to you.
Recommended Reading:
Why is the AC frequency of the power grid in China 50Hz?
What causes the noise of electronic devices?
How to monitor variables in STM32CubeMonitor
Follow the WeChat public account 『strongerHuang』, reply “1024” to see more content, reply “join group” to join the technical exchange group according to the rules.

STM32 + IAP + Ymodem: A Perfect Combination

Long pressto go to the public account included in the imageto follow

Leave a Comment

×