AN3310
Application note
Updating firmware in STM32L1xx microcontrollers through in-application programming using the USART
Introduction
An important requirement for most Flash-memory-based systems is the ability to update the 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 STM32L1xx 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.
January 2012 |
Doc ID 18201 Rev 2 |
1/19 |
www.st.com
Contents |
AN3310 |
|
|
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
7 |
3 |
IAP driver menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
8 |
|
|
3.1 |
Downloading an image to the internal Flash memory . . . . . . . . . . . . . . . . |
8 |
|
3.2 |
Uploading an image from the internal Flash memory . . . . . . . . . . . . . . . . |
9 |
|
3.3 |
Executing the new program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
9 |
|
3.4 |
Disabling the write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
9 |
4 |
STM32L1xx IAP implementation summary . . . . . . . . . . . . . . . . . . . . . . |
10 |
|
5 |
User program conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
12 |
|
6 |
Firmware updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
13 |
7How to place FLASH programming functions into internal SRAM/FLASH 14
8 |
Memory write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
15 |
9 |
Dual bank feature for high-density devices . . . . . . . . . . . . . . . . . . . . . |
16 |
10 |
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
18 |
2/19 |
Doc ID 18201 Rev 2 |
AN3310 |
IAP overview |
|
|
Ultra low power medium-density devices are STM32L151xx and STM32L152xx microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes. Ultra low power medium-density devices are implemented in the STMicroelectronics STM32L152-EVAL evaluation board.
Ultra low power medium-density plus devices are STM32L151xx, STM32L152xx and STM32L162xx microcontrollers where the Flash memory density is 256 Kbytes.
Ultra low power high-density devices are STM32L151xx, STM32L152xx and STM32L162xx microcontrollers where the Flash memory density is 384 Kbytes. Ultra low power highdensity devices are implemented in the STMicroelectronics STM32L152D-EVAL evaluation board.
You should program the IAP driver to the Flash memory base address via the JTAG/SWD interface using the development toolchain of your choice or the factory-embedded boot loader in the System memory area.
The IAP driver uses the USART to:
●Download a binary file from the HyperTerminal to the STM32L1xx's internal Flash memory.
●Upload the STM32L1xx's internal Flash memory content (starting from the defined user application address) into a binary file.
●Execute the user program.
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 internal Flash memory, executing the binary file already loaded and disabling the write protection of the pages where the user loads his/her 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(a). In the event of a failure when receiving the data, the “Failed to receive the file” error message is displayed. If the data is received
a.The Ymodem protocol sends data in 1024-byte blocks. An error check is performed in data blocks transmitted to the STM32L1xx’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 18201 Rev 2 |
3/19 |
IAP overview |
AN3310 |
|
|
successfully, it is programmed into the internal Flash memory from the appropriate address. A comparison between 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 when the image file size is greater than the allowed memory space and when the user aborts the task.
●STM32L1xx Standard Peripherals Library.
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 Table 1. STM32L1xx IAP implementation for more details about the STM32L15xxEVAL board push-button used to enter the IAP mode.
The IAP flowchart is shown in Figure 1: Flowchart of the IAP driver.
4/19 |
Doc ID 18201 Rev 2 |
AN3310 |
IAP overview |
|
|
|
|
Start |
|
|
|
|
|
|
Push-button |
No |
|
|
|
|
|
is pressed? |
|
|
|
|
|
|
Yes |
|
|
|
|
|
Initialize USART |
|
|
|
||
|
Display the IAP main menu |
|
|
|
||
|
Download, upload, |
|
|
|
||
|
switch or disable |
|
|
|
||
|
write-protection |
|
|
|
||
Download |
|
Upload |
Flash |
Switch to |
||
|
|
|
|
protection |
user |
|
Receive a binary file |
|
|
|
|
program |
|
No |
|
|
|
|
|
|
Success |
|
|
|
|
|
|
Yes |
|
|
|
|
|
|
Program the Flash |
|
Transmit image |
|
Disable the |
Switch to |
|
|
of the device internal Flash |
write protection |
user program |
|||
|
|
|||||
No |
Yes |
No |
Yes |
Launch option |
User |
|
byte loading |
application |
|||||
All data programmed |
|
Success |
|
|||
successfully? |
|
|
|
|
software |
|
Display the name |
Display the error message |
|
||||
and size of |
|
"Error occured..." |
|
|
||
received file |
|
|
|
|
|
|
Display the error message |
|
|
|
|
|
|
"Verification failed" |
|
|
|
|
|
|
Display the error |
|
|
|
|
|
|
message "Failed to |
|
|
|
|
|
|
receive the file" |
|
|
|
|
|
|
|
|
|
|
|
MS19702V1 |
Doc ID 18201 Rev 2 |
5/19 |
Running the IAP driver |
AN3310 |
|
|
For ultra low power medium-density and medium-density plus devices, the IAP driver is programmed in the Flash memory from Page 0 to Page 47 and the user application occupies the remaining memory space.
For ultra low power high-density devices, the Flash memory implements the dual bank feature (Refer to Chapter 9: Dual bank feature for high-density devices).
The IAP driver can be programmed in Bank1 (from Page 0 to Page 47) and the user application occupies the rest of Flash memory or Bank2.
The IAP driver can be programmed in Bank2 (from Page 768 to Page 814) and the user application occupies the rest of Bank2 or Bank1.
To use the IAP, the user must have a PC running HyperTerminal or other Terminal program that supports ymodem protocol. In this document, the HyperTerminal is used. The following figure shows the HyperTerminal configuration.
Note: |
The baud rate value of 115,200 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 115,200 bps can be generated. |
6/19 |
Doc ID 18201 Rev 2 |