An5405 How To Use Fdcan Bootloader Protocol On Stm32 Mcus Stmicroelectronics en
An5405 How To Use Fdcan Bootloader Protocol On Stm32 Mcus Stmicroelectronics en
Application note
Introduction
This application note describes the FDCAN 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 STM32
microcontroller system memory boot mode (AN2606), available from www.st.com. These products are listed in Table 1.
For more information about the FDCAN hardware resources and requirements for their device bootloader, users are advised to
refer to the application note AN2606.
STM32G0 series
STM32G4 series
STM32H5 series
Microcontrollers STM32H7 series
STM32L5 series
STM32U3 series
STM32U5 series
The 32-bit microcontrollers listed in Table 1. Applicable products are based on the Arm® Cortex® processor. They
are referred to as STM32 in this document.
Figure 1 presents the global sequence for the STM32 bootloader with FDCAN.
Frame detected
on FDCAN Rx
pin (1)
Wait for a
command
(Optional)
Get Read Memory Routines for Go
cmd routine cmd routine loading into cmd routine
RAM
Jump to Address
(1) Depending on the FDCAN protocol version, the frame sent to the FDCAN to start communication is different
* When protocol version is <= 2.1, any frame can be sent
* When protocol version is > 2.1, only this frame is accepted "ID = 111h, data length = 1 and data = 5Ah"
Once the system memory boot mode is entered and the STM32 device is configured (refer to the application note
AN2606 for additional details), the bootloader code waits for a frame on the FDCANx_Rx pin.
Note: Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
2 FDCAN settings
Note: 1. The CAN bootloader firmware supports only one node at a time. This means that it does not support CAN
Network Management.
2. Depending on the FDCAN protocol version, the frame acceptance is different:
– When protocol version is ≤ 2.1, filters are not enabled, so any MessageID and FilterID1 are accepted.
– When protocol version is > 2.1, MessageID and FilterID1 must match exactly.
3. In the bootloader, only the 8 LSB bits of the messaged ID are taken into account, so, any value above FFh is
ignored.
Table 2 lists the supported commands, each of them being described in the corresponding subsection.
Command
Command Subsection Description
code
Erase Memory(2) 0x44 Section 3.7 Erases from one to all the flash memory sectors.
1. Protection: When the protection is active, only this limited subset of commands is available. All other commands are NACK-
ed and have no effect on the device. Protection depends on product series. Protection is active means
• For the STM32H5 series: Trust zone (TZEN) = 0 and Product state > Provisioning, and HiDeProtection Level (HDPL)
=3
• For the other products listed in Table 1. Applicable products: read protection set.
2. Refer to the STM32 product datasheet and application note AN2606 to know about the valid memory spaces for this
command.
Communication safety
Each packet is either accepted (ACK answer) or discarded (NACK answer):
• ACK message = 0x79
• NACK message = 0x1F
DT64158V1
Received message No
with ID = 00h ?
Yes
DT64159V1
All data sent from the device on this command are sent using DataLength = 0000h (one byte).
NACK
Wait for ACK or NACK
ACK
DT64160V1
End of Get Version command
The host receives first the protocol version (one byte), and then two dummy bytes equal each to 00h.
Received message No
with ID = 01h ?
Yes
Send NACK
Send ACK message
message
NACK
Wait for ACK or NACK
ACK
NACK
Wait for ACK or NACK
ACK
The host receives the STM32 product ID (two bytes) sent by the device as shown in Figure 7. The LSB (byte) is
sent first.
Received message No
with ID = 02h ?
Yes
NACK
Wait for ACK or NACK
ACK
Together with the message ID for the Read Memory command (11h), the address, and number of bytes are sent
to the device. The message content is as follows:
1. ID = 0x11, DLC = 0x05
2. data[0] = 0xXX (MSB of the address)
3. data[1] = 0xYY
4. data[2] = 0xZZ
5. data[3] = 0xTT (LSB of the address)
6. data[4] = N (number of bytes to be read minus one; 0 < N ≤ 255)
with ID = 11h ?
ACK
Address valid ?
ACK
The data are always sent to the host by multiples of 64 bytes. The host must filter the needed data based on the
number of data requested.
3.5 Go command
The Go command is used to execute downloaded code or any other code by branching to an address specified
by the application. When the bootloader receives the Go command, it starts only if the message contains valid
information and passes the following checks:
• Is the ID of the command correct?
• Is the protection disabled?
• Is the address to jump to valid?
If the message content is correct, the Go command transmits an ACK message. Otherwise, it transmits a NACK
message.
After sending an ACK message to the application, the bootloader firmware:
• Resets the registers of the peripherals used by the bootloader to their default values
• Initializes the main stack pointer of the user application
• Jumps to the memory location programmed in the received “address + 4”, which is the address of the
application reset handler.
For example, if the received address is 0x0800 0000, the bootloader jumps to the memory location
programmed at address 0x0800 0004. The host must send the base address where the application to jump
to is programmed.
Note: 1. The jump to the application works only if the user application sets the vector table correctly to point to the
application address.
2. The valid addresses for the Go command are in RAM or flash memory. All other addresses are considered
not valid and are NACK-ed by the device.
3. Not all addresses in the RAM are considered valid. The application to jump must consider an offset to avoid
overlapping with the first RAM memory used by the bootloader firmware.
Start Go command
End of Go command
The message content sent to the device, including the Go command ID (21h), is as follows:
1. ID = 0x21, DLC = 0x04
2. data[0] = 0xXX (MSB of the address)
3. data[1] = 0xYY
4. data[2] = 0xZZ
5. data[3] = 0xTT (LSB of the address)
Start Go command
with ID = 21h ?
ACK
Address valid ?
ACK
DT64167V1
Jump to user application End of Go command
NACK
Wait for ACK or NACK
ACK
NACK
Wait for ACK or NACK
ACK
Together with the message ID for the Write Memory command (31h), the address, and number of bytes are sent
to the device. The message content is as follows:
1. ID = 0x31, DLC = 0x05
2. data[0] = 0xXX (MSB of the address)
3. data[1] = 0xYY
4. data[2] = 0xZZ
5. data[3] = 0xTT (LSB of the address)
6. data[4] = N (number of bytes to be written minus one; 0 < N ≤ 255)
The host sends N / 64 messages.
Note: 1. The ACK message sent after the erase operation only indicates that the command is finished. It does not
guarantee that the erase operation has succeeded.
2. No error is returned when performing erase operations on write-protected sectors.
NACK
Wait for ACK or NACK
ACK
PageNumber = 0xFFFF
or
No
PageNumber = 0xFFFE Wait for ACK
or
PageNumber = 0xFFFD
command
NACK
Protection active ?
Send NACK
Send ACK message
message
PageNumber = 0xFFFF
or
PageNumber = 0xFFFE
No
or
PageNumber = 0xFFFD
Send ACK message
Yes
Receive page numbers to
erase
DT64171V1
End of Erase Memory
command
NACK
Wait ACK or NACK
ACK
Send messages
(Number of sectors & sector codes to protect)
Wait ACK
The sector code is sent on one byte. The number of sectors and the codes are sent together with the command
ID of the Write Protect command.
Received message No
with ID = 63h ?
Yes
Yes
Protection active ?
No
Send NACK
Send ACK message
message
DT64173V1
End of Write Protect command
NACK
Wait ACK or NACK
ACK
Wait ACK
Received message No
with ID = 73h ?
Yes
Yes
Protection active ?
No
DT64175V1
End of Write Unprotect command
NACK
Wait ACK or NACK
ACK
Wait ACK
Received message No
with ID = 82h ?
Yes
Yes
RDP active ?
No
Wait ACK
Wait ACK
Received message No
with ID = 92h ?
Yes
No
Number of bytes
N ≠ 0?
Yes
No
Number of bytes
N ≠ 0?
Yes
Receive ACK
Received message No
with ID = 0x50
Yes
Command valid No
& checksum OK?
Yes
Yes
No Number of data
N ≠ 0?
Yes
No
Number of status data
N ≠ 0?
Yes
End of Special command (1) The internal processing depends on the project needs.
When the bootloader receives the Special command, it transmits the ACK byte to the host. Once the ACK is
transmitted, the bootloader waits for a subcommand opcode (two bytes, MSB first) and a checksum byte. If the
subcommand is supported by the STM32 bootloader and if its checksum is correct, the bootloader transmits an
ACK byte, otherwise it transmits a NACK byte and aborts the command.
To keep the Special command generic, the data packet received by the bootloader can have different sizes
depending on the subcommand needs.
Therefore, the packet is split in two parts:
• Size of the data (2 bytes, MSB first)
• N bytes of data
– If N = 0, no data is transmitted
– N must be less than 128
If all conditions are satisfied (N ≤ 128 and checksum OK), the bootloader transmits an ACK byte. Otherwise, it
transmits a NACK byte and aborts the command.
Once the subcommand is executed using the received data, the bootloader sends a response that consists of two
consecutive packets:
• Data packet
– Size of the data (2 bytes, MSB first)
– N bytes of data
◦ If N = 0, no data is transmitted
• Status packet
– Size of the status data (2 bytes, MSB first)
– N bytes of data
◦ If N = 0, no status data is transmitted
Finally, an ACK byte closes the Special command interaction between the bootloader and the host.
No
Number of bytes
N ≠ 0?
Yes
Receive ACK
Received message No
with ID = 0x51
Yes
Command valid No
& checksum OK?
Yes
Yes
Yes
No
Number of status data
N ≠ 0?
Yes
When the bootloader receives the Extended Special command, it transmits the ACK byte to the host. Once the
ACK is transmitted, the bootloader waits for a subcommand opcode (two bytes, MSB first) and a checksum byte.
If the subcommand is supported by the STM32 bootloader and if its checksum is correct, the bootloader transmits
an ACK byte. Otherwise, it transmits a NACK byte and aborts the command.
Two packets can then be received depending on the subcommand needs:
• Packet1
– Data1 packet, where the number of bytes is limited to 128 bytes
• Packet2
– Data2 packet, where the number of bytes is limited to 1024 bytes
If all conditions are satisfied for packet1 first (N ≤ 128 and checksum OK) and then for packet2 (N ≤ 1024 and
checksum OK), the bootloader transmits an ACK byte. Otherwise, it transmits a NACK byte and aborts the
command.
Once the subcommand is executed using the received data, the bootloader sends a response that consists of one
packet:
• Size of the data (2 bytes, MSB first)
• N bytes of data
– If N = 0, no data is transmitted
Finally, an ACK byte closes the Extended Special command interaction between the bootloader and the host.
Revision history
Table 4. Document revision history
Contents
1 Bootloader code sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 FDCAN settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
3 Bootloader command set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1 Get command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Get Version command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Get ID command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 Read Memory command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.5 Go command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.6 Write Memory command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.7 Erase Memory command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.8 Write Protect command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.9 Write Unprotect command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.10 Readout Protect command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.11 Readout Unprotect command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.12 Special command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.13 Extended Special command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4 Bootloader protocol version evolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
List of tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
List of figures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
List of tables
Table 1. Applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Table 2. FDCAN bootloader commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Table 3. Bootloader protocol versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Table 4. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
List of figures
Figure 1. Bootloader for STM32 with FDCAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Figure 2. Get command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Figure 3. Get command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Figure 4. Get Version command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 5. Get Version command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 6. Get ID command (host side). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 7. Get ID command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 8. Read Memory command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Figure 9. Read Memory command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 10. Go command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Figure 11. Go command (device side). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Figure 12. Write Memory command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Figure 13. Write Memory command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Figure 14. Erase Memory command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Figure 15. Erase Memory command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Figure 16. Write Protect command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Figure 17. Write Protect command (device side). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Figure 18. Write Unprotect command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Figure 19. Write Unprotect command (device side). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Figure 20. Readout Protect command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Figure 21. Readout Protect command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Figure 22. Readout Unprotect command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Figure 23. Readout Unprotect command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Figure 24. Special command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Figure 25. Special command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Figure 26. Extended Special command (host side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Figure 27. Extended Special command (device side) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28