0% found this document useful (0 votes)
59 views

DM00036049 - STM32F41x In-Application Programming Using The USART

DM00036049 - STM32F41x in-application programming using the USART

Uploaded by

Khánh Lê Quang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

DM00036049 - STM32F41x In-Application Programming Using The USART

DM00036049 - STM32F41x in-application programming using the USART

Uploaded by

Khánh Lê Quang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

AN3965

Application note
STM32F40x/STM32F41x in-application programming
using the USART

1 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 STM32F4xx 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.

October 2011 Doc ID 022104 Rev 1 1/13


www.st.com
Contents AN3965

Contents

1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 IAP overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 IAP driver description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3 Running the IAP driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6


3.1 HyperTerminal configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Executing the IAP driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

4 IAP driver menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7


4.1 Download image to the internal Flash memory . . . . . . . . . . . . . . . . . . . . . 7
4.2 Upload image from the internal Flash memory . . . . . . . . . . . . . . . . . . . . . 8
4.3 Execute the new program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.4 Disabling the write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

5 STM32F4xx IAP implementation summary . . . . . . . . . . . . . . . . . . . . . . 10

6 User program conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

7 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2/13 Doc ID 022104 Rev 1


AN3965 IAP overview

2 IAP overview

STM32F4xx devices are implemented in the STMicroelectronics STM324xG-EVAL


evaluation board.

2.1 Principle
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 STM32F4xx internal Flash
memory.
● Upload the STM32F4xx internal Flash memory content (starting from the defined user
application address) into a binary file.
● Execute the user program.

2.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 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 functions.
● common.c: Contains functions related to read/write from/to USART peripheral
● ymodem.c: It is used to receive/send the data from/to the HyperTerminal application
using the YMODEM protocol(a). In case of a failure when receiving the data, the “Failed
to receive the file” error message is displayed. If the data is successfully received, 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.
● STM32F4xx Standard Peripherals Library.

a. The Ymodem protocol sends data in 1024-byte blocks. An error check is performed in the data blocks
transmitted to the STM32F4xx 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 the existing documentation.

Doc ID 022104 Rev 1 3/13


IAP overview AN3965

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: STM32F4xx IAP implementation for more details about the STM324xG-
EVAL board push-button used to enter the IAP mode.
The IAP flowchart is shown in Figure 1.

4/13 Doc ID 022104 Rev 1


AN3965 IAP overview

Figure 1. Flowchart of the IAP driver

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
Transmit image Disable the Switch to
Program the Flash
of the device internal Flash write protection user program

No No Yes Launch option User


Yes
All data programmed Success byte loading application
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"

ai14402c

Doc ID 022104 Rev 1 5/13


Running the IAP driver AN3965

3 Running the IAP driver

3.1 HyperTerminal configuration


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. 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.

3.2 Executing the IAP driver


As an example, in this application note, pressing the push-button connected to the pin
launches the IAP driver.
By pressing the push-button at reset, the user can run the IAP driver to reprogram the
STM32F4xx 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 Table 1: STM32F4xx IAP
implementation.

6/13 Doc ID 022104 Rev 1


AN3965 IAP driver menu

4 IAP driver menu

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

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

4.1 Download image to the internal Flash memory


To download a binary file via HyperTerminal to the STM32F4xx’s internal Flash memory, do
as follows:
1. Press 1 on the keyboard to select the Download Image To the STM32F4xx 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 you want to
download.
4. From the protocol list, select the Ymodem protocol.
5. Click on the Send button.
As a result, the IAP driver loads the binary file into the STM32F4xx’s internal Flash memory
from the defined base address and displays the binary file name and size in the
HyperTerminal window. for further information on base address settings, refer to Chapter 6:
User program conditions.

Doc ID 022104 Rev 1 7/13


IAP driver menu AN3965

4.2 Upload image from the internal Flash memory


To upload a copy of the internal Flash memory started from the user application address, do
as follows:
1. Press 2 on the keyboard to select Upload image from the STM32F4xx 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.

4.3 Execute the new program


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

4.4 Disabling the write protection


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

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

8/13 Doc ID 022104 Rev 1


AN3965 IAP driver menu

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 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 this application, the read protection is not supported, so the user has to verify that the
Flash memory is not read-protected.

Doc ID 022104 Rev 1 9/13


STM32F4xx IAP implementation summary AN3965

5 STM32F4xx IAP implementation summary

Table 1 provides a summary of the STM32F4xx IAP implementation.

Table 1. STM32F4xx IAP implementation


Platform Implementation Configuration

The IAP program is located at 0x8000000. The Flash


routines (program/erase) are executed from the Flash
memory. Sector 0
The size of this program is about 8 Kbytes and programmed
on:
Firmware
The user application (image to be downloaded with the IAP)
1008 Kbytes
will be programmed starting from address 0x8004000(1)
(Sector 1 - Sector 11)
The maximum size of the image to be loaded is:
The image is uploaded with the IAP from the STM32F4xx 1008 Kbytes
internal Flash. The size of the image to be uploaded is: (Sector 1 - Sector 11)
Key push-button
Push-button (active level: high)
Hardware connected to pin PG15
USART used USART3
1. User application location address is defined in the flash_if.h file as: #define APPLICATION_ADDRESS
0x8004000.To modify it, change the default value to the desired one.

The STM32F4xx IAP package comes with:


● Source files and pre-configured projects for the IAP program (under
Project\STM32F4xx_IAP directory)
● Source files and pre-configured projects that build the application to be loaded into
Flash memory using the IAP (under Project\STM32F4xx_IAP\binary_template
directory).
The readme.txt files provided within this package describes step by step how to execute
this IAP application.

10/13 Doc ID 022104 Rev 1


AN3965 User program conditions

6 User program conditions

The user application to be loaded into the Flash memory using IAP should be built with
these configuration settings:
1. Set the program load address at 0x08004000, using your toolchain linker file
2. Relocate the vector table at address 0x08004000, using the “NVIC_SetVectorTable”
function from the misc.h/.c driver (under STM32F4xx_StdPeriph_Driver\inc) or by
modifying the value of the constant “VECT_TAB_OFFSET” defined in the
system_stm32f4xx.c file.”
An example application program to be loaded with the IAP application is provided with
preconfigured projects.

Figure 5. Flash memory usage

Top Flash memory Flash memory


Top Flash memory Flash memory
address (1)
address (1)

User code
User code

Vector table
0x800 4000 Vector table
0x800 4000
0x800 3FFF
0x800 3FFF

IAP code
IAP code

Vector table
0x800 0000 Vector table
0x800 0000

MM18906V1
MM18906V1

1. Top Flash memory address is equal to 0x080F FFFF for STM32F4xx devices

Doc ID 022104 Rev 1 11/13


Revision history AN3965

7 Revision history

Table 2. Document revision history


Date Revision Changes

12-Oct-2011 1 Initial release.

12/13 Doc ID 022104 Rev 1


AN3965

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.

© 2011 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 022104 Rev 1 13/13

You might also like