ST AN4065 Application note

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 STM320518­EVAL board push-button used to enter IAP mode.
The IAP flowchart is shown in Figure 1.
4/13 Doc ID 022905 Rev 1
AN4065 IAP overview
.O
9E S
$OWNLOAD
2ECEIVEABINARYFILE
3UCCESS
9E S
0ROGRAMTHE&LASH
,AUNCHOPTION
BYTELOADING
!LLDATAPROGRAMMED
SUCCESSFULLY
.O
$ISPLAYTHENAME
ANDSIZEOF
9E S
$ISPLAYTHEERRORMESSAGE
6ERIFICATIONFAILED
$ISPLAYTHEERROR
MESSAGE&AILEDTO
RECEIVETHEFILE
)NITIALIZE53!24
0USHBUTTON
ISPRESSED
3TART
$ISPLAYTHE)!0MAINMENU
AIC
.O
3WITCHTO
USER
3WITCHTO
USERPROGRAM
5SER
APPLICATION
SOFTWARE
$ISABLETHE
WRITEPROTECTION
&LASH
PROTECTION
5PLOAD
4RANSMITIMAGE
OFTHEDEVICEINTERNAL&LASH
3UCCESS
$ISPLAYTHEERRORMESSAGE
%RROROCCURED
RECEIVEDFILE
.O
9E S
PROGRAM
$OWNLOADUPLOAD
SWITCHORDISABLE
WRITEPROTECTION

Figure 1. Flowchart of the IAP driver

Doc ID 022905 Rev 1 5/13
Running the IAP driver AN4065

2 Running the IAP driver

2.1 HyperTerminal configuration

To use the IAP, the user must have a PC running HyperTerminal or another terminal program that supports ymodem protocol. In this document, the HyperTerminal is used.
Figure 2 shows the HyperTerminal configuration.

Figure 2. COM port properties

Note: The baud rate value of 115200 bps is used as an example.
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.

2.2 Executing the IAP driver

As an example in this application note, pressing the pin connected to the push-button allows the IAP driver to run.
By pressing the push-button at reset, the user can run the IAP driver to reprogram the STM32F0xx’s internal Flash memory. It is not mandatory to use the push-button; the user can apply a signal to this pin with respect to its active level. Refer to
IAP implementation.
6/13 Doc ID 022905 Rev 1
Ta bl e 1: STM32F0xx
AN4065 IAP driver menu

3 IAP driver menu

Running the IAP driver displays the following menu in the HyperTerminal window.

Figure 3. IAP driver menu when the STM32F0xx Flash memory is not protected

3.1 Download image to the internal Flash memory

Follow steps 1 to 5 below to download a binary file via the HyperTerminal to the STM32F0xx’s internal Flash memory.
1. Press 1 on the keyboard to select the Download Image to the STM32F0xx Internal Flash menu.
2. Select Send File in the Transfer menu.
3. In the Filename field, type the name and the path of the binary file to be downloaded.
4. From the protocol list, select the Ymodem protocol.
5. Click on the Send button.
When these steps are executed, the IAP driver loads the binary file into the STM32F0xx’s internal Flash memory from the defined base address and displays the binary file name and size in the HyperTerminal window.
Doc ID 022905 Rev 1 7/13
IAP driver menu AN4065

3.2 Upload image from the internal Flash memory

Follow steps 1 to 5 below to upload a copy of the internal Flash memory started from the user application address.
1. Press 2 on the keyboard to select Upload image from the STM32F0xx internal Flash menu.
2. Select Receive File in the Transfer menu.
3. Select the directory to save the binary file.
4. From the protocol list, select the Ymodem protocol.
5. Click on the Receive button.

3.3 Execute the new program

Once the new program has been loaded, press 3 on the keyboard to select Execute the New Program menu and execute the code.

3.4 Disable the write protection

When IAP starts, it checks the Flash memory pages where the user program is to be loaded to see if any of them are write protected. If this is the case, the menu shown in appears.
Figure 4

Figure 4. IAP driver menu when the STM32F0xx Flash memory is write protected

8/13 Doc ID 022905 Rev 1
AN4065 IAP driver menu
Prior to downloading the new program, the write protection function must be disabled. To do so, press 4 (Disable the write protection) on the keyboard. Write protection is disabled and a system reset is generated to reload the new option byte values. After resuming from reset, the menu shown in
Figure 3 is displayed if the key push-button is pressed.
Note: In the current application, read protection is not supported, so the user has to verify that the
Flash memory is not read-protected.
Doc ID 022905 Rev 1 9/13
STM32F0xx IAP implementation summary AN4065

4 STM32F0xx IAP implementation summary

Ta bl e 1 provides a summary of the STM320518-EVAL IAP implementation.

Table 1. STM32F0xx IAP implementation

Firmware Hardware
IAP program is located at 0x8000000. Flash routines (program/erase) are executed from the Flash memory. Size of IAP program program is about 6 Kbytes.
User application (image to be downloaded with the IAP) is programmed starting from address 0x8003000
(1)
Image is uploaded with IAP from STM32F0xx internal Flash
Push-button (active level: high)
USART used
STM32F0xx devices (STM320518-EVAL)
1. The user application location address is defined in the flash_if.h file as: #define APPLICATION_ADDRESS 0x8003000.To modify it, change the default value to the desired one.
Sector 0 - Sector 1
52 Kbytes (Sector 3 - Sector 15)
52 Kbytes (Sector 3 - Sector 15)
Push-button connected to pin PB8
The STM32F0xx IAP package comes with:
Source files and preconfigured projects for the IAP program (see:
Project\STM32F0xx_IAP directory)
Source files and preconfigured projects that build the application to be loaded into
Flash memory using the IAP (see: Project\STM32F0xx_IAP\binary_template directory).
The readme.txt files provided within this package describe, in a step by step fashion, how to execute the current IAP application.
USART1
10/13 Doc ID 022905 Rev 1
AN4065 User program conditions
5SERCODE
6ECTORTABLE
)!0CODE
X
X&&&
X
-36
&LASHMEMORY
4OP&LASHMEMORY ADDRESS


5 User program conditions

The user application to be loaded into the Flash memory using IAP should be built with the following configuration settings:
1. Set the program load address at 0x08003000, using the toolchain linker file.
2. Relocate the vector table at address 0x20000000 (reserve 48 word starting in RAM @ 0x20000000 ).
An example application program to be loaded with the IAP application is provided with preconfigured projects.

Figure 5. Flash memory usage

1. The top Flash memory address is 0x0800 7FFF for STM32F0xx devices
Doc ID 022905 Rev 1 11/13
Revision history AN4065

6 Revision history

Table 2. Document revision history

Date Revision Changes
03-May-2012 1 Initial release.
12/13 Doc ID 022905 Rev 1
AN4065
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY TWO AUTHORIZED ST REPRESENTATIVES, ST PRODUCTS ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
© 2012 STMicroelectronics - All rights reserved
STMicroelectronics group of companies
Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan -
Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com
Doc ID 022905 Rev 1 13/13
Loading...