Dm00161366 Stm32 Inapplication Programming Iap Using The Usart Stmicroelectronics
Dm00161366 Stm32 Inapplication Programming Iap Using The Usart Stmicroelectronics
Application note
STM32 in-application programming (IAP) using the USART
Introduction
An important requirement for most Flash-memory-based systems is the ability to update
firmware when installed in the end product. This ability is referred to as in-application
programming (IAP).
Most newer STM32 microcontrollers are supported by middleware open source library
called Open Bootloader (OpenBL).
More information on OpenBL is available for example in USART protocol used in the STM32
bootloader (AN3155).
The purpose of this application note is to provide general guidelines for creating an IAP
application on older STM32 microcontrollers not supported by the OpenBL.
An STM32 microcontroller is able to run user-specific firmware to perform an IAP of the
Flash memory embedded in the microcontroller. This feature uses any communication
interface available and supported by the product. The USART using the Ymodem protocol is
the example taken in this application note.
The X-CUBE-IAP-USART firmware package is delivered with this document and contains
the source code of IAP examples for STM32 microcontrollers. It is available from
www.st.com.
Contents
1 IAP overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 IAP driver example description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 IAP driver flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
List of tables
List of figures
1 IAP overview
1.1 Principle
When a reset occurs, the program counter is set to execute the IAP driver. It must be a
compact code that checks a specific condition, for example that a combination of keys is
pressed. When this condition is met, the IAP driver code either executes a branch that
updates the user application or directly (usually by default) executes this user application.
The user application needs to be separated from the IAP driver. The most practical solution
is to place the IAP driver code at the beginning of program memory and the user code at the
beginning of the next free Flash memory block, sector or page, which allows independent
memory protection to be configured on both areas. For the example given in this application
note, the IAP is performed through the USART rather than a more advanced communication
interface, thus allowing for a minimal memory footprint.
The user application is likely to have independent stack and interrupt vectors (both are
recommended but neither is mandatory) as illustrated in Figure 1. When the IAP driver
directly launches the user application:
1. The IAP driver sets the main stack pointer to the application address.
2. The next instruction executes the jump (unconditional branching) to the application.
3. The application then sets its own interrupt vector table as active.
Vector table
APPLICATION_ADDRESS
IAP code
Vector table
Flash memory start address
MSv37824V1
a. The Ymodem protocol sends data by 1024-byte blocks. An error check is performed in the transmitted
data blocks to compare the transmitted and received data. Blocks unsuccessfully received are
acknowledged with an NAK (negative acknowledgment). For more details about the Ymodem protocol,
refer to the existing documentation.
Start
Jump to
Key button is
user no
pressed?
program
yes
Initialize IAP
yes
Toggle the
Main Write the data
write Success?
menu to Flash
protection
yes
no
3 Receive Display
Menu item
4 1 binary Success? no error
choice
file message
Transmit
binary
file
yes Success? no
MSv37825V1
The IAP driver must be programmed via the JTAG/SWD interface, starting from the Flash
memory base address. This is performed either through a chosen development toolchain or
the factory-embedded bootloader located the System memory area.
The IAP driver uses the USART to:
• download a binary file from the terminal emulator to the STM32 internal Flash memory.
• upload the STM32 internal Flash memory content (starting from the defined user
application address) into a binary file.
Note: The baud rate value of 115200 bps is used in the firmware examples.
Care must be taken when selecting the system clock frequency. To guarantee successful
communication via the USART, the system clock frequency in the end application must
support a baud rate ofat least 115200 bps.
The COM port number depends on the user PC configuration. The examples are
configured to use the ST link virtual COM port.Executing the IAP driver
In the example given in this application note, pressing the push-button at reset launches the
IAP driver.
By pressing the push-button at reset, the user runs the IAP driver to reprogram the STM32
microcontroller internal Flash memory. Refer to the readme.txt file provided within the
firmware package and to your board documentation for implementation details.
Running the IAP displays the following menu in the Tera Term window.
Prior to downloading the new program, the write protection must be disabled. To do so,
press 4 Disable the write protection on the keyboard. The write protection is then disabled
and a system reset is generated to reload the new option byte values. After resuming from
reset, the menu shown in Figure 4 is displayed if the key push-button is pressed.
When a new user application is installed, the write protection may be enabled once again to
prevent accidental corruption of the code. To toggle the write protection, select menu option
“4” again.
Note: In this example, the read protection is not supported, so the user has to verify that the Flash
memory is not read-protected. Removing the read protection requires a mass erase of the
non-volatile memory content.
The user application to be loaded into the Flash memory using IAP must be built with these
configuration settings:
1. Using your toolchain linker settings, set the program load address as configured in the
IAP project code.
2. Relocate the vector table to APPLICATION_ADDRESS, for example by modifying the
value of the constant VECT_TAB_OFFSET defined in the project source files.
An example application program to be loaded with the IAP is provided with pre-configured
projects. Refer to the project readme.txt file for details.
6 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. For additional information about ST trademarks, please refer to www.st.com/trademarks. All other
product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.