An3078 stm32 Inapplication Programming Over The Ic Bus Stmicroelectronics
An3078 stm32 Inapplication Programming Over The Ic Bus Stmicroelectronics
Application note
STM32™ in-application programming
over the I²C bus
Introduction
A key requirement for most Flash-memory-based systems is the ability to update firmware
once the system is installed in the end product. This is referred to as in-application
programming (IAP). The purpose of this application note is to provide general guidelines for
creating an IAP application.
STM32F10x microcontrollers can run user-specific firmware to perform the IAP of their
embedded Flash memory. This feature allows the use of any type of communication protocol
for the reprogramming process (such as CAN, USART, USB, SPI, I2C, etc.).
This application note describes how to perform in-application programming using the
STM32F10x’s I2C peripheral. As a first step, the Aardvark tool is used as the host sending
the IAP commands to the STM32F10x. As a second step, the host is another STM32F10x
microcontroller that behaves as a bridge between the PC and the target (USB/I2C bridge).
The STM32F10x behaves as an I2C memory that can be programmed, erased and read via
the I2C/ interface.
For further information on the STM32F10x family features, pinout, electrical characteristics,
mechanical data and ordering information, please refer to the STM32F10x datasheets
available from the STMicroelectronics website: www.st.com.
The STM32 firmware libraries are available from www.st.com.
The versions of the Aardvark host adapter used during the development are listed below:
● Firmware: Aardvark I2C/SPI Control Center v3.52 -2009.0130
● Hardware: Aardvark I2C/SPI v3.0
For further information on the Aardvark host adapter and its software, please refer to its
documentation on www.st.com.
Contents
1 IAP overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 User program conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Global flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 IAP commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4.1 Read command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4.2 Write command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.3 Erase page command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4.4 Erase User Space memory command . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4.5 Go To User Code command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 IAP timings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
List of tables
List of figures
1 IAP overview
1.1 Principle
The IAP driver must be programmed from the Flash memory base address via the JTAG or
SWD interface using a development toolchain. This driver uses the I2C to load a file from
the host to the STM32F10x’s internal Flash memory, and then executes it.
User code
User space
memory
Vector table
0x0800 1000
0x0800 0FFF
IAP code
IAP space
memory
Vector table
0x0800 0000
ai17364
Start
Yes No
User button
pressed
Switch to IAP Switch to User program
No No
Reset Reset
pressed pressed
Yes Yes
ai17365
A pin connected to a push-button is used to select between jumping to the user application
and executing IAP for reprogramming purposes:
● at reset, when the push-button is pressed IAP is executed
● otherwise, a jump to the user program is performed
Start
End
ai17366
If the opcode value is OPC_READ, the Read command is performed. The target device
considers the first four bytes sent by the host master after the opcode as the Flash memory
address to be read from. The next two bytes after the Flash memory address give the size in
bytes of the data to be read. The contents of the Flash memory are buffered into
I2C1_buffer_Tx and sent by bytes via the SDA line.
Start
End
ai17367
If the opcode value is OPC_WREN, the Write command is performed. The target device
considers the first four bytes sent by host master after the opcode as the Flash memory
address to be written to. The two next bytes after the Flash memory address give the size in
bytes of the data to be written. The N bytes received from the host master are buffered into
RAM_BufferRx. The corresponding Flash memory page(s) is (are) erased before the data
are written.
Start
End
ai17368
If the opcode value is OPC_ERPG, the Erase Page command is performed. The target
device considers the first four bytes sent by host master after the opcode as the Flash
memory address. The next two bytes after the Flash memory address give the numbers of
the pages to be erased. Once the address and page numbers have been received, the
target STM32 device erases the corresponding Flash memory pages.
Start
End
ai17369
If the opcode value is OPC_ERUSM, the Erase User Space Memory command is
performed. As its name suggests, this command erases the contents of all pages from Flash
memory base address 0x0800 1000 (the user space start address) to the top Flash memory
address.
Start
End
ai17370
This section describes how to perform IAP using the STM32 I2C peripheral.
Figure 8. Hardware connection between the host master and the STM32 I2C target
Master Slave
VDD
RPU
SDA
I2C1 R/W/E Program
Host
RPU memory
SCL
VSS
S ADD_DVCE W ACK OPC_READ ACK ADD_HGH1 ACK ADD_HGH0 ACK ADD_LOW1 ACK ADD_LOW0 ACK
2nd sequence
S ADD_DVCE R ACK Data ACK Data ACK ... ACK Data ACK Data NACK P
ai17379
S ADD_DVCE W ACK OPC_WREN ACK ADD_HGH1 ACK ADD_HGH0 ACK ADD_LOW1 ACK ADD_LOW0 ACK
NUMBR_H ACK NUMBR_L ACK DUM_BYTE ACK Data ACK ... Data ACK P
ai17380
Figure 11. Erase Page packet sequence using the I2C protocol
S ADD_DVCE W ACK OPC_ERPG ACK ADD_HGH1 ACK ADD_HGH0 ACK ADD_LOW1 ACK ADD_LOW0 ACK
ai17381
Figure 12. Erase User Space Memory packet sequence using the I2C protocol
Start
Flash_config
NVIC_config
I2C_config
No
I2C_START
Yes
No No No No No
OPC_READ OPC_WREN OPC_ERPG OPC_ERUSM OPC_USRCD
Read command Write command Erase Page Erase User Space Go to User
command Memory command Application
ai17384
3.1 Sending IAP I2C commands using the Aardvark I2C host
adapter
1
3
4 5
Read command
The following steps explain how to read the contents of the user Flash memory:
● Step 1: Enter the byte sequence of the Read command in a Master Write message.
Refer to Section 2.2.2: IAP I2C Read command for a description of the sequence.
● Step 2: Enter the number of bytes to be read into the “Number of Bytes” field.
● Step 3: Press the ‘Master Write’ button.
● Step 4: Press the ‘Master Read’ button.
3
OPC_READ
ADD_HIGH1
ADD_HIGH0
ADD_LOW1
ADD_LOW0
NUMBR_H
NUMBR_L
1
2 4
ai17393
In this example, the Read command reads 16 bytes from the Flash memory address
0x0800 1000.
The figure below shows the result of the Read command transaction.
Write command
The following steps explain how to write to the user Flash memory:
● Step 1: Enter the byte sequence of the Write command. Refer to Section 2.2.3: IAP I2C
Write command for a description of the sequence.
● Step 2: Press the ‘Master Write’ button.
2
OPC_WREN
ADD_HIGH1
ADD_HIGH0
ADD_LOW1
ADD_LOW0
NUMBR_H
NUMBR_L
DUM_BYTE
1
Data to be written
Flash memory Number
address of bytes
ai17395
In this example, the Write command writes 16 bytes to the Flash memory from address
0x0800 1000.
Figure 19 gives the result of the Write command transaction.
16 bytes written
ai17396
The other IAP commands (Erase page, Erase Full User Space Memory and Go To User
Application) are issued in the same way.
This configuration has to be inserted into the <head> of all script I2C commands.
Read command
The figure below shows an example of a Read command, where 4096 bytes are read from
Flash memory address 0x0800 1000.
Write command
The figure below shows an example of a Write command, where 16 bytes are written to the
Flash memory from address 0x0800 1000.
The batch scripts of the other IAP commands (Erase Page, Erase Full User Space Memory
and Go To User Application) are written in the same way.
3.2.1 Principle
The device firmware upgrade (DFU) project is extended to support the I2C module. The
DFU process is similar to any IAP process, so, in the case of this project, instead of
upgrading the internal Flash memory of the bridge device, the commands are transferred via
I2C peripheral to upgrade the target device.
For further information on device firmware upgrade (DFU), please refer to the dedicated
user, UM0424, available from www.st.com.
Figure 23. Hardware connection between the PC, the STM32 bridge and the I2C
target
ai17501
4 IAP timings
This section gives an idea of the time required to program a file into the STM32’s internal
Flash memory.
5 Revision history
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 AN AUTHORIZED ST REPRESENTATIVE, 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.
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.