ST AN2475 Application note

AN2475
Application note
STR91xFAxxx in-application programming using UART
Introduction
An important requirement for most Flash-based systems is the ability to update firmware while the system is installed in the end product. This is referred to as In-Application Programming (IAP).
This application note outlines general guidelines for creating an IAP application using two different methods: the first one is based on the Ymodem protocol and the second one is based on a customized UART protocol using the free Flash Loader utility provided by ST.
The STR91x evaluation boards have been used to validate the IAP driver.
The IAP Firmware is available on www.st.com.
September 2008 Rev 2 1/37
www.st.com
Table of contents AN2475
Table of contents
1 IAP overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Triggering execution of the IAP driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 IAP using the Ymodem protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1 Driver description and code sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Running the IAP driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.1 HyperTerminal configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.2 IAP driver menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.3 Download image to internal Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.4 Execute the new program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 IAP using the ST Flash loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.1 IAP code sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 Command set description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2.1 Get command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2.2 Get ID command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2.3 Go command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2.4 Write command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2.5 Read command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.6 Erase command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.2.7 Write Unprotect command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.3 Running the IAP driver firmware using the Flash loader . . . . . . . . . . . . . 28
3.3.1 Serial communication set-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.3.2 STR91x FA target device selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3.3 Download image to internal Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4 How to use the IAP drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5 Software remapping and user application interrupts . . . . . . . . . . . . . 34
6 STR9 IAP implementation summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2/37
AN2475 IAP overview

1 IAP overview

1.1 Principle

The IAP driver must be programmed by the user in the Flash Bank1 after being remapped in hardware to address 0x00 (Bank0 is hardware remapped at address 0x80000 for 256 or 512 Kbytes devices and at address 0x200000 for 1 or 2 Mbytes devices).
Note: For 256K, Bank 0 must be at a 256K boundary (Bank 0 base addresses can be: 0x00,
ox40000, ox80000, etc.).
For 512K, Bank 0 must be at a 512K boundary (Bank 0 base addresses can be: 0x00, ox80000, ox100000, etc.).
For 1M, Bank 0 must be at a 1M boundary (Bank 0 base addresses can be: 0x00, 0x100000, 0x200000, etc.).
For 2M, Bank 0 must be at a 2M boundary (Bank 0 base addresses can be: 0x00, 0x200000, 0x400000, etc.).
Programming is performed via JTAG using a Flasher toolset.

1.2 Triggering execution of the IAP driver

In this application note, the pin 4 of port 7 connected to the push button (PB3) on the STR91xFA evaluation board is used as an example. This configuration allows the execution of the IAP driver. By pressing the push button at reset, the user is able to run the IAP driver to reprogram the STR91x internal Flash.
It is not mandatory to use the push button; the user can simply enter a signal on this pin with respect to its corresponding active level.
3/37
IAP using the Ymodem protocol AN2475

2 IAP using the Ymodem protocol

Using the UART, the IAP driver loads a binary file from HyperTerminal to the STR91xFA internal Flash bank0, and then jumps to execute it.

2.1 Driver description and code sequence

The IAP driver code size is 7.5K approximately.
It contains a set of source files:
main.c: where the UART initialization and PLL configuration are set. Then, a main
menu is executed from the common.c file.
common.c: contains display functions and the main menu routine. The main menu
gives the choice between loading a new binary file and executing the binary file already loaded.
ymodem.c and download.c: they allow all data to be received from the HyperTerminal
(using the YMODEM protocol (see note 1 below) then loaded into STR9 internal RAM. In the event of a failure in data reception, the error message ‘Failed to receive the file’ is displayed in the HyperTerminal window. Otherwise, after successful reception, the data is then programmed into internal Flash at the appropriate address. A comparison between internal RAM and internal Flash contents is performed to check the data integrity. If there is a mismatch, the error message ‘Verification failed’ is displayed in the HyperTerminal window. Other error messages are also displayed when the image size is higher than the allowed memory space and when there is an abort by the user.
Note: The Ymodem protocol sends data in 1024-byte blocks. Error detection is applied to data
blocks transmitted to the STR91xFA internal RAM. This is done through a comparison between the transmitted and received data. Blocks received unsuccessfully are acknowledged with a NAK (Negative Acknowledgement). For more details about the Ymodem protocol, please refer to the existing literature.
Some STR91xFA firmware library source files and header files
91x_fmi.c and 91x_fmi.h
91x_gpio.c and 91x_gpio.h
91x_scu.c and 91x_scu.h
91x_uart.c and 91x_uart.h
91x_lib.h
91x_map.h
91x_type.h
91x_it.c
91x_it.h
A pin connected to a push button is used to select between jumping to the user application and executing the IAP for reprogramming purposes:
At reset, the push button is pressed: the IAP main menu is displayed
At reset, the push button is not pressed: Jump to the user application.
Figure 1 shows the IAP flowchart.
4/37
AN2475 IAP using the Ymodem protocol
Figure 1. Flowchart of the IAP using the Ymodem protocol driver
Start
Download
Receive a binary file
No
Success
Ye s
Program the internal Flash
No
All data programmed
successfully?
Push button
pressed
Ye s
Initialize the STR9 UART & PLL
Display the IAP main menu
Download or
jump ?
Disable the write-
protection
System reset
generation
Ye s
No
Jump to user
program
Jump to user
program
User application
firmware
Display the name and size of
received file
Display the error message
"Verification failed"
Display the error
message "Failed to
receive the file"

2.2 Running the IAP driver

On STR91xFA devices, the IAP driver is programmed in Bank1 which is remapped in hardware to address 0x00.
ai15300
5/37
IAP using the Ymodem protocol AN2475

2.2.1 HyperTerminal configuration

The IAP requires a PC running HyperTerminal with the following settings:
Figure 2. COM Port Properties
Note: A baud rate value of 115200 bps is used as an example.
Care should be taken when selecting the system clock frequency. Ensure that with the system clock frequency used in the application, a baud rate equal to 115200 bps can be generated in order to guarantee successful communication via UART.

2.2.2 IAP driver menu

The execution of the IAP firmware results in the following menu displayed in the HyperTerminal window.
Figure 3. IAP driver menu
6/37
AN2475 IAP using the Ymodem protocol

2.2.3 Download image to internal Flash

To download a binary file via HyperTerminal to the STR91xFA internal Flash, follow the procedure below:
Press “1” on the keyboard to choose the menu “Download image to internal Flash”
Then, in the Transfer menu, select Send file”:
In the filename field, type the name and the path of the binary file to be sent.
In the protocol list, choose the Ymodem protocol,
Click the “Send” button.
Following these steps, the IAP driver loads the binary file into the STR91xFA internal Flash from Bank0 sector0 base address and displays the binary file name and file size in the HyperTerminal window.

2.2.4 Execute the new program

After loading the new program from HyperTerminal by selecting the “Download image to internal Flash” menu, the code can be executed by selecting the “Execute the new program” menu by pressing “2” from the keyboard.
7/37
IAP using the ST Flash loader AN2475

3 IAP using the ST Flash loader

ST provides a free Flash loader utility allowing the program of the STR91xFA internal Flash Bank0 using an IAP driver to be loaded in the Flash Bank1.

3.1 IAP code sequence

The IAP driver code size is approximately 6 Kbytes.
It contains a set of source files:
main.c
iap.c
Some STR91xFA firmware library source files and header files
91x_fmi.c and 91x_fmi.h
91x_gpio.c and 91x_gpio.h
91x_scu.c and 91x_scu.h
91x_uart.c and 91x_uart.h
91x_lib.h
91x_map.h
91x_type.h
91x_it.c and 91x_it.h
A pin connected to a push button is used to select between jumping to the user application and executing the IAP for reprogramming purposes:
At reset, the push button is pressed: connection to the ST Flash Loader.
At reset, the push button is not pressed: Jump to the user application.
8/37
AN2475 IAP using the ST Flash loader
Send ACK byte
No
Ye s
Wait for 0x7F from
the host
Jump to user
program
Initialize the STR9 UART & PLL
Start
ai15301
Wait for a command
GET command GET ID command WRITE command
GO command
Push button
pressed
The figure below shows the IAP flowchart.
Figure 4. Flowchart of the IAP using the bootloader protocol driver
Once the push button is pressed, the IAP code begins to scan the UART0_RX line, waiting to receive 0x7F data from the host: one start bit, 0x7F data bits, even parity bit and one stop bit.
Next, the code initializes the serial interface (115200 bps, Even Parity, One Stop bit). Then, the STR91xFA returns an acknowledge byte (0x79) to the host, signaling that it received the 0x7F byte and it is ready to receive user commands.
9/37
IAP using the ST Flash loader AN2475

3.2 Command set description

The supported commands are listed in the table below. Each command is further described in this section.
Table 1. Command list
Command Command code Command description
GET 0x00 Get the version and the allowed commands.
GET ID 0x02 Get the Chip ID.
WRITE 0x31
Write maximum 256 bytes to the Flash Bank0 starting from an address specified by the user.
READ 0x11
ERASE 0x43 Erase from one to all the Bank0 sectors.
WRITE UNPROTECT 0x71 Write Unprotect all Bank0 sectors.
GO 0x21
Communication safety
All communication from the programming tool (PC) to the device is verified in the following ways:
1. Checksum: all received bytes are XORed. A byte containing the computed XOR of all previous bytes is added at the end of each communication (Checksum byte). By XORing all received bytes (data + Checksum), the result, at the end of the packet, must be 0x00.
2. For each command, the host sends a byte and its complement (XOR = 0x00).
3. UART: a parity check is active (even parity).
Each packet is either accepted (ACK answer) or discarded (NACK answer).
ACK = 0x79
NACK = 0x1F
Read up to 256 bytes of Flash Bank0 starting from an address specified by the user.
Jump to Bank0 base address specified by the user to execute a loaded code.

3.2.1 Get command

The Get command allows the user to get the version of the IAP and the supported commands. When the STR91xFA receives the Get command, it transmits the version and the supported command codes to the host, as described in Figure 5.
10/37
AN2475 IAP using the ST Flash loader
Receive the supported commands
NACK
ACK
Receive the version
Receive the number of bytes
Wait for
ACK or NACK
Send 0x00 + 0xFF
ai15302
Wait for ACK or NACK
Start Get
(version + commands)
End Get
Figure 5. Get command host side
11/37
IAP using the ST Flash loader AN2475
Start Get
Is 0x00 + 0xFF
received ?
Send the ACK byte
Send the number of bytes -1 (version + commands)
Send the version
Send the supported commands
Send the ACK byte
End Get
Send NACK
No
ai15303
Figure 6. Get command STR91xFA side
12/37
Loading...
+ 25 hidden pages