STM32 In-Application Programming (IAP) Using The USART PDF
STM32 In-Application Programming (IAP) Using The USART PDF
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).
The purpose of this application note is to provide general guidelines for creating an IAP
application on STM32 microcontrollers.
An STM32 microcontroller can run user-specific firmware to perform IAP of the Flash
memory embedded in the microcontroller. This feature can use 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 should 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. In the example given in this application
note, the IAP is performed through the USART rather than a more advanced communication
interface, thus allowing to minimize memory footprint.
The user application is likely to have independent stack and interrupt vector (both are
recommended but neither is mandatory) (see 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.
)ODVKPHPRU\HQGDGGUHVV
8VHUDSSOLFDWLRQFRGH
9HFWRUWDEOH
$33/,&$7,21B$''5(66
,$3FRGH
9HFWRUWDEOH
)ODVKPHPRU\VWDUWDGGUHVV
06Y9
a. The Ymodem protocol sends data by 1024-byte blocks. An error check is performed in the data blocks
transmitted 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.
6WDUW
-XPSWR
.H\EXWWRQLV
XVHU QR
SUHVVHG"
SURJUDP
\HV
,QLWLDOL]H,$3
\HV
7RJJOHWKH
0DLQ :ULWHWKHGDWD
ZULWH 6XFFHVV"
PHQX WR)ODVK
SURWHFWLRQ
\HV
QR
5HFHLYH 'LVSOD\
0HQXLWHP
ELQDU\ 6XFFHVV" QR HUURU
FKRLFH
ILOH PHVVDJH
7UDQVPLW
ELQDU\
ILOH
\HV 6XFFHVV" QR
06Y9
The IAP driver must be programmed via the JTAG/SWD interface, starting from the Flash
memory base address. This can be performed either through the development toolchain of
your choice 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 be
such that a baud rate equal to 115200 bps can be generated.
The COM port number depends on the user PC configuration. If no physical COM port is
available in your development environment, use a virtual COM port such as the one
provided by the ST-LINK/V2.1 interface.
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 can be enabled 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 should 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 STs 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. 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.