USART Protocol Used in The STM32 Bootloader
USART Protocol Used in The STM32 Bootloader
Application note
USART protocol used in the STM32 bootloader
Introduction
This application note describes the USART protocol used in the STM32 microcontroller
bootloader, providing details on each supported command.
This document applies to STM32 products embedding any bootloader version, as specified
in the application note AN2606 “STM32 system memory boot mode”, available on
www.st.com. These products are listed in Table 1, and are referred to as STM32 throughout
the document.
For more information about the USART hardware resources and requirements for your
device bootloader, refer to the already mentioned AN2606.
STM32L0 Series
STM32L1 Series
STM32L4 Series
STM32F0 Series
STM32F1 Series
Microcontrollers STM32F2 Series
STM32F3 Series
STM32F4 Series
STM32F7 Series
STM32H7 Series
STM32WB Series
Contents
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
List of tables
List of figures
53!24X SELECTED
7AIT FOR A
COMMAND
#OMMAND
'%4 CMD RECEIVED '/ CMD
*0 TO?!DDRESS
AI
Once the system memory boot mode is entered and the STM32 microcontroller (based on
on Arm®(a) cores) has been configured (for more details refer to AN2606) the bootloader
code begins to scan the USARTx_RX line pin, waiting to receive the 0x7F data frame: a
start bit, 0x7F data bits, even parity bit and a stop bit.
The duration of this data frame is measured using the Systick timer. The count value of the
timer is then used to calculate the corresponding baud rate factor with respect to the current
system clock.
Next, the code initializes the serial interface accordingly. Using this calculated baud rate, an
acknowledge byte (0x79) is returned to the host, which signals that the STM32 is ready to
receive commands.
a. Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
The calculation of the serial baud rate for USARTx, from the length of the first received byte,
is used to operate the bootloader within a wide range of baud rates. However, the upper and
lower limits have to be kept, to ensure proper data transfer.
For a correct data transfer from the host to the microcontroller, the maximum deviation
between the internal initialized baud rate for USARTx and the real baud rate of the host
must be below 2.5%. The deviation (fB, in percent) between the host baud rate and the
microcontroller baud rate can be calculated using the formula below:
This baud rate deviation is a nonlinear function, depending upon the CPU clock and the
baud rate of the host. The maximum of the function (fB) increases with the host baud rate.
This is due to the smaller baud rate prescale factors, and the implied higher quantization
error.
The supported commands are listed in Table 2. Each command is described in this section.
Communication safety
All communication from the programming tool (PC) to the device is verified by:
1. checksum: received blocks of data bytes are XOR-ed. A byte containing the computed
XOR of all previous bytes is added to the end of each communication (checksum byte).
By XOR-ing all received bytes, data plus 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: parity check active (even parity).
6WDUW*HW
6HQG[[))
:DLWIRU$&. 1$&.
RU1$&.
$&.
5HFHLYHWKHQXPEHURIE\WHV
YHUVLRQFRPPDQGV
5HFHLYHWKHERRWORDGHUYHUVLRQ
5HFHLYHWKHVXSSRUWHGFRPPDQGV
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI*HW
DL
6WDUW*HW
5HFHLYHG
1R
E\WH [[))" 6HQG1$&.E\WH
<HV
6HQG$&.E\WH
6HQGWKHQXPEHURIE\WHV
YHUVLRQFRPPDQGV
6HQGWKHERRWORDGHUYHUVLRQ
6HQGWKHVXSSRUWHGFRPPDQGV
6HQG$&.E\WH
(QGRI*HW
DL
Figure 4. Get Version & Read Protection Status command: host side
6WDUW*9;ϭͿ
6HQG[[)(
1$&.
:DLWIRU
$&.RU1$&.
$&.
5HFHLYHWKHERRWORDGHUYHUVLRQ
2SWLRQE\WH
2SWLRQE\WH
1$&.
:DLWIRU
$&.RU1$&.
$&.
(QGRI*9;ϭͿ
069
Figure 5. Get Version & Read Protection Status command: device side
6WDUW*9
5HFHLYHG
1R
E\WH [[)(" 6HQG1$&.E\WH
<HV
6HQG$&.E\WH
6HQGWKHERRWORDGHUYHUVLRQ
2SWLRQE\WH
2SWLRQE\WH
6HQG$&.E\WH
(QGRI*9
069
6WDUW*,'
6HQG[[)'
:DLWIRU$&. 1$&.
RU1$&.
$&.
5HFHLYH1 QXPEHURIE\WHV±
5HFHLYH3,'
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI*,'
DL
3TART ')$
2ECEIVED .O
BYTE X X&$ 3END .!#+ BYTE
9ES
3END PRODUCT )$
%ND OF ')$
AI
6WDUW50
6HQG[[((
:DLWIRU$&. 1$&.
RU1$&.
$&.
6HQGWKHVWDUWDGGUHVV E\WHV ZLWK
FKHFNVXP
:DLWIRU$&. 1$&.
RU1$&.
$&.
6HQGWKHQXPEHURIE\WHVWREHUHDG E\WH
DQGDFKHFNVXP E\WH
:DLWIRU$&. 1$&.
RU1$&.
$&.
5HFHLYHGDWDIURPWKH%/
(QGRI50
DL
1. RM = Read Memory.
Note: Some products may return two NACKs instead of a single NACK when Read protection
(RDP) is active (or Read protection level 1 is active). To know if a given product returns one
or two NACKs in this situation, refer to the known limitations section relative to that product
in AN2606.
3TART 2-
2ECEIVED BYTE .O
X X%%
9ES
9ES
2$0 ACTIVE
.O
!DDRESS VALID .O
CHECKSUM /+
9ES
.O
#HECKSUM /+
9ES
%ND OF 2-
AIB
1. RM = Read Memory.
3.5 Go command
The Go command is used to execute the downloaded code or any other code by branching
to an address specified by the application. When the bootloader receives the Go command,
it transmits the ACK byte to the application. After the transmission of the ACK byte, the
bootloader waits for an address (four bytes, byte 1 is the address MSB and byte 4 is LSB)
and a checksum byte, then it checks the received address. If the address is valid and the
checksum is correct, the bootloader transmits an ACK byte, otherwise it transmits a NACK
byte and aborts the command.
When the address is valid and the checksum is correct, the bootloader firmware performs
the following:
• initializes the registers of the peripherals used by the bootloader to their default reset
values
• initializes the user application’s main stack pointer
• jumps to the memory location programmed in the received ‘address + 4’
(corresponding to the address of the application reset handler).
For example if the received address is 0x0800 0000, the bootloader will jump to the
memory location programmed at address 0x0800 0004.
In general, the host must send the base address where the application to jump to is
programmed
6WDUW*R
6HQG[['(
:DLWIRU$&. 1$&.
RU1$&.
$&.
6HQGWKH6WDUW$GGUHVV
E\WHVFKHFNVXP
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI*R
DLG
Note: Valid addresses for the Go command are in RAM or Flash memory (refer to STM32 product
datasheets and to AN2606 for more details about the valid memory addresses for the used
device). All other addresses are considered not valid and are NACK-ed by the device.
When an application is loaded into RAM and then a jump is made to it, the program must be
configured to run with an offset to avoid overlapping with the first RAM used by the
bootloader firmware (refer to STM32 product datasheets and to AN2606 for more details
about the RAM offset for the used device).
The Jump to the application works only if the user application sets the vector table correctly
to point to the application address.
Some products may return two NACKs instead of a single NACK when Read protection
(RDP) is active (or Read protection level 1 is active). To know if a given product returns one
or two NACKs in this situation, refer to the known limitations section relative to that product
in AN2606.
6WDUW*R
5HFHLYHGE\WHV 1R
[['("
<HV
<HV
5'3DFWLYH
1R
6HQG$&.E\WH
5HFHLYHWKHVWDUWDGGUHVV E\WHV
FKHFNVXP
$GGUHVVYDOLG 1R
6HQG1$&.E\WH
FKHFNVXP2."
6HQG$&.E\WH
(QGRI*R
-XPSWRXVHUDSSOLFDWLRQ
DLG
3TART 7-
!#+
!#+
3END THE NUMBER OF BYTES TO BE WRITTEN
BYTE THE DATA . BYTES AND CHECKSUM
!#+
%ND OF 7-
AIB
1. WM = Write Memory.
2. N+1 must be a multiple of 4.
Note: Some products may return two NACKs instead of one when Read protection (RDP) is active
(or Read protection level 1 is active). To know if a given product returns a single or two
NACKs in this situation, refer to the known limitations section relative to that product in
AN2606.
^ƚĂƌƚtD;ϭͿ
ZĞĐĞŝǀĞĚďLJƚĞс EŽ
ϬdžϯϭнϬdž͍
zĞƐ
EŽ
ZWŝŶĂĐƚŝǀĞ͍
zĞƐ
^ĞŶĚ<ďLJƚĞ
ZĞĐĞŝǀĞƚŚĞƐƚĂƌƚĂĚĚƌĞƐƐ;ϰďLJƚĞƐͿΘ
ĐŚĞĐŬƐƵŵ
EŽ
ŚĞĐŬƐƵŵK<͍
zĞƐ
^ĞŶĚ<ďLJƚĞ
ZĞĐĞŝǀĞƚŚĞŶƵŵďĞƌŽĨďLJƚĞƐƚŽďĞǁƌŝƚƚĞŶ
;ϭďLJƚĞͿ͕ƚŚĞĚĂƚĂ;EнϭďLJƚĞƐͿ;ϮͿĂŶĚĐŚĞĐŬƐƵŵ
EŽ
ŚĞĐŬƐƵŵK<͍
zĞƐ
^ƵƉƉŽƌƚĞĚ zĞƐ
ŵĞŵŽƌLJΘŶŽƚ tƌŝƚĞƚŽĚĞƐƚŝŶĂƚŝŽŶŵĞŵŽƌLJ
KƉƚŝŽŶďLJƚĞƐ͍
EŽ
tƌŝƚĞƚŚĞƌĞĐĞŝǀĞĚĚĂƚĂƚŽ
KƉƚŝŽŶďLJƚĞĂƌĞĂĨƌŽŵƐƚĂƌƚĂĚĚƌĞƐƐ
EŽ ^ĞŶĚ ^ĞŶĚ
< E<
^ĞŶĚ<ďLJƚĞ ďLJƚĞ ďLJƚĞ
'ĞŶĞƌĂƚĞƐLJƐƚĞŵƌĞƐĞƚ
ŶĚŽĨtD;ϭͿ
ĂŝϭϰϲϰϮĞ
1. WM = Write Memory.
2. N+1 must be a multiple of 4.
6WDUW(5
6HQG[[%&
:DLWIRU$&. 1$&.
RU1$&.
$&.
<HV *OREDO 1R
(UDVH"
6HQG[)) 6HQGWKHQXPEHURISDJHV
WREHHUDVHG E\WH
6HQG[
6HQGWKHSDJHQXPEHUV
6HQGFKHFNVXP
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI(5
DLE
1. ER = Erase Memory.
3TART %2
2ECEIVED BYTES .O
X X"#
9ES
9ES
2$0 ACTIVE
.O
9ES
X&& RECEIVED
.O
#HECKSUM .O
/+
9ES
%RASE THE CORRESPONDING PAGES
%ND OF %2
AIC
1. ER = Erase Memory.
Note: After sending the erase memory command and its checksum, if the host sends 0xFF
followed by data different from 0x00, the mass erase is not performed but an ACK is sent by
the device.
The host sends bytes to the STM32 as follows:
Byte 1: 0x43
Byte 2: 0xBC
Wait for ACK
6WDUW((5
6HQG[[%%
1$&.
:DLWIRU$&.RU1$&.
$&.
<(6 12
6SHFLDO(UDVH"
6HQGWKHQXPEHURISDJHV
WREHHUDVHG1 RQWZR
6HQG6SHFLDO(UDVHFPG E\WHV 06%ILUVW
[)))) 0DVVHUDVH
[)))( %DQNHUDVH
6HQGWKHSDJHQXPEHUV
[)))' %DQNHUDVH
[)))WR[)))&DUHUHVHUYHG
HDFKRQWZRE\WHV06%
ILUVW
6HQGWKHFKHFNVXPRIWKHWZR 6HQGE\WHFKHFNVXPRIDOO
E\WHV VHQWE\WHV 1 E\WHV
[ 1 E\WHV
1$&.
:DLWIRU$&.RU1$&.
$&.
(QGRI((5
AI
^ƚĂƌƚZ
EŽ
ZĞĐĞŝǀĞĚďLJƚĞƐсϬdžϰϰнϬdž͍
zĞƐ
zĞƐ
ZWĐƚŝǀĞ͍
EŽ
^ĞŶĚ<LJƚĞ
ZĞĐĞŝǀĞEƵŵďĞƌŽĨWĂŐĞƐƚŽďĞ
ĞƌĂƐĞĚE;ϮďLJƚĞƐͿD^ĨŝƌƐƚ
zĞƐ Ϭdž&&&zƌĞĐĞŝǀĞĚ͍
tŚĞƌĞzĐĂŶďĞ
&͕Žƌ
^ƚĂƌƚ^ƉĞĐŝĂůƌĂƐĞ͗ EŽ
Ϭdž&&&&сDĂƐƐĞƌĂƐĞ
Ϭdž&&&сĂŶŬϭĞƌĂƐĞ ZĞĐĞŝǀĞƚŚĞƉĂŐĞĐŽĚĞƐ;ŽŶϮ
Ϭdž&&&сĂŶŬϮĞƌĂƐĞ ďLJƚĞƐĞĂĐŚ͕D^ĨŝƌƐƚͿ
;Ϭdž&&&ϬƚŽϬdž&&&ĂƌĞƌĞƐĞƌǀĞĚͿ
ZĞĐĞŝǀĞƚŚĞĐŚĞĐŬƐƵŵŽĨĂůů
ZĞĐĞŝǀĞŚĞĐŬƐƵŵŽĨƚŚĞϮďLJƚĞƐ ƌĞĐĞŝǀĞĚďLJƚĞƐ;E;ŽŶϮ
;ϬdžϬϬ͕ϬdžϬϭŽƌϬdžϬϮͿ Ϯ
ďLJƚĞƐͿ͕dž;EнϭͿďLJƚĞƐͿ
ŚĞĐŬƐƵŵŽĨϮ ŚĞĐŬƐƵŵŽĨĂůů EŽ
EŽ ƌĞĐĞŝǀĞĚďLJƚĞƐK<
͍
ďLJƚĞƐK<ĂŶĚĐŽŵŵĂŶĚ
^ƵƉƉŽƌƚĞĚ͍
zĞƐ
zĞƐ
WĞƌĨŽƌŵƚŚĞ ƌĂƐĞƚŚĞĐŽƌƌĞƐƉŽŶĚŝŶŐƉĂŐĞƐ
ƌĞƋƵĞƐƚĞĚĞƌĂƐĞ
ŶĚŽĨZ
ĂŝϭϳϰϲϭĐ
6WDUW:3
6HQG[[&
:DLWIRU$&. 1$&.
RU1$&.
$&.
6HQGWKHQXPEHURIVHFWRUV
WREHSURWHFWHG E\WH
6HQGWKHVHFWRUFRGHV
6HQGFKHFNVXP
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI:3
DLE
1. WP = Write Protect.
3TART 70
2ECEIVED BYTES .O
X X#
9ES
9ES
2$0 ACTIVE
.O
#HECKSUM .O
/+
9ES
7RITE PROTECT THE REQUESTED SECTORS
%ND OF 70
AIC
1. WP = Write Protect.
6WDUW:381
6HQG[[&
:DLWIRU$&. 1$&.
RU1$&.
$&.
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI:381
DL
3TART 705.
2ECEIVED BYTES .O
X X#
9ES
9ES
2$0 ACTIVE
.O
%ND OF 705.
AIC
6WDUW5'3B350
6HQG[['
:DLWIRU$&. 1$&.
RU1$&.
$&.
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI5'3B350
DL
3TART 2$0?02-
2ECEIVED BYTES .O
X X$
9ES
9ES
2$0 ACTIVE
.O
%ND OF 2$0?02-
AIC
6WDUW5'8B350
6HQG[['
:DLWIRU$&. 1$&.
RU1$&.
$&.
:DLWIRU$&. 1$&.
RU1$&.
$&.
(QGRI5'8B350
AI
3TART 2$5?02-
2ECEIVED BYTES .O
X X$
9ES
$ISABLE 2$0
5 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.