
AN4065
Application note
STM32F0xx in-application programming 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.
The STM32F0xx microcontroller can run user-specific firmware to perform IAP of the
microcontroller-embedded Flash memory. This feature allows the use of any type of
communication protocol for the reprogramming process. The USART is the example used in
this application note.
May 2012 Doc ID 022905 Rev 1 1/13
www.st.com

Contents AN4065
Contents
1 IAP overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 IAP driver description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Running the IAP driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1 HyperTerminal configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Executing the IAP driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 IAP driver menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1 Download image to the internal Flash memory . . . . . . . . . . . . . . . . . . . . . 7
3.2 Upload image from the internal Flash memory . . . . . . . . . . . . . . . . . . . . . 8
3.3 Execute the new program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Disable the write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 STM32F0xx IAP implementation summary . . . . . . . . . . . . . . . . . . . . . . 10
5 User program conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2/13 Doc ID 022905 Rev 1

AN4065 IAP overview
1 IAP overview
STM32F0xx devices are implemented in the STMicroelectronics STM320518-EVAL
evaluation board.
1.1 Principle
Program the IAP driver to the Flash memory base address via the serial wire debug (SWD)
interface using the development toolchain of your choice or the factory-embedded
bootloader in the system memory area.
The IAP driver uses the USART to:
● Download a binary file from the HyperTerminal to the STM32F0xx's internal Flash
memory.
● Upload the STM32F0xx's internal Flash memory content (starting from the defined
user application address) into a binary file.
● Execute the user program.
1.2 IAP driver description
The IAP driver contains the following set of source files:
● main.c: where the USART initialization and RCC configuration are set. A main menu is
then executed from the menu.c file.
● menu.c: contains the main menu routine. The main menu gives the options of
downloading a new binary file, uploading the internal Flash memory, executing the
binary file already loaded, and disabling the write protection of the pages where the
user loads his binary file (if they are write protected).
● flash_if.c contains write, erase and disable write protection of the internal Flash
memory.
● common.c: contains functions related to read/write from/to the USART peripheral
● ymodem.c: is used to receive/send the data from/to the HyperTerminal application
using the YMODEM protocol
receive the file” error message is displayed. If the data is received successfully, it is
programmed into the internal Flash memory from the appropriate address. A
comparison between the internal RAM contents and internal Flash memory contents is
performed to check the data integrity. If there is any data discrepancy, the “Verification
failed” error message is displayed. Other error messages are also displayed, for
example, when the image file size is greater than the allowed memory space and when
the user aborts the task.
● STM32F0xx Standard Peripherals Library.
(a)
. In the data receiving process fails, the “Failed to
a. The Ymodem protocol sends data in 1024-byte blocks. An error check is performed in data blocks transmitted
to the STM32F0xx’s internal RAM to compare the transmitted and received data. Blocks unsuccessfully
received are acknowledged with an NAK (negative acknowledgement). For more details about the Ymodem
protocol, refer to existing documentation.
Doc ID 022905 Rev 1 3/13

IAP overview AN4065
The user can choose to either go to the user application or execute the IAP for
reprogramming purposes by pressing a push-button connected to a pin.
● Not pressing the push-button at reset switches to the user application
● Pressing the push-button at reset displays the IAP main menu
Refer to Ta bl e 1: STM32F0xx IAP implementation for more details about the STM320518EVAL board push-button used to enter IAP mode.
The IAP flowchart is shown in Figure 1.
4/13 Doc ID 022905 Rev 1