AN3154 Application Note: CAN Protocol Used in The STM32 Bootloader
AN3154 Application Note: CAN Protocol Used in The STM32 Bootloader
AN3154 Application Note: CAN Protocol Used in The STM32 Bootloader
Application note
CAN protocol used in the STM32 bootloader
Introduction
This application note describes the CAN protocol used in the STM32 microcontroller
bootloader. It details each supported command.
This document applies to the STM32 products embedding bootloader versions V3.x, V7.x
and V9.x, as specified in STM32 microcontroller system memory boot mode (AN2606)
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 CAN hardware resources and requirements for the
bootloader of the used device, refer to the already mentioned AN2606.
STM32F1 Series
STM32F2 Series
Microcontrollers STM32F4 Series
STM32F7 Series
STM32L4 Series
Contents
2 CAN settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
List of tables
List of figures
Frame detected(1)
on CANx_Rx pin
Wait for a
command
Command
GET cmd received GO cmd
JP to_Address
ai17754b
Once the system memory boot mode is entered and the STM32 device (based on Arm®(a)
cores) has been configured (for more details refer to AN2606), the bootloader code waits for
a frame on the CANx_Rx pin. When a detection occurs the CAN bootloader firmware starts
to check the external clock frequency.
Figure 2 shows the flowchart of the frequency check.
a. Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
Message Message
received with stdID=0x79 Yes Yes
received with stdID=0x79
and without frame error? and without frame error
?
No No
No No
Decrement timeout Decrement timeout
Timeout = 0x0 ? Timeout=0x0?
Yes Yes
Configure CAN baudrate at 125Kbps
Assuming that HSE = 25MHz Generate System Reset
Initialize timeout at ~1.5 ms
Message
Yes
received with stdID=0x79
and without frame error
?
No
No
Decrement timeout
Timeout = 0x0 ?
Yes
Configure CAN baudrate at 125Kbps
Assuming that HSE = 14.7456MHz
Initialize timeout at ~1.5 ms
Message
received with stdID=0x79 Yes
and without frame error
?
No
No
Decrement timeout
Timeout = 0x0 ?
Yes
1. For some devices the HSE frequency is calculated using HSI oscillator connected to a timer. For other
devices this measurement is not implemented. For the devices without HSE frequency measurement, only
the flow represented on the left is executed, while for the devices with HSE frequency measurement only
the flow on the right is executed. To know the flow for the used device refer to AN2606.
Next, the code initializes the serial interface accordingly. Using this calculated baud rate, an
acknowledge byte (0x79) is returned to the host, indicating that the STM32 is ready to
receive commands.
2 CAN settings
The STM32 CAN is compliant with the 2.0A and B (active) specifications with a bit rate up to
1 Mbit/s. It can receive and transmit standard frames with 11-bit identifiers as well as
extended frames with 29-bit identifiers.
Figure 3 shows a CAN frame that uses the standard identifier only.
ACK
SOF
ai15001
The supported commands are listed in Table 2, each of them is described in this section.
Communication safety
Each packet is either accepted (ACK answer) or discarded (NACK answer):
• ACK message = 0x79
• NACK message = 0x1F
ACK
Wait for
ACK or NACK
Received message No
with ID = 0x00?
Yes
Message 14: Std ID = 0x00, DLC = 1, data = 82h - Readout Protect command
Message 15: Std ID = 0x00, DLC = 1, data = 92h - Readout Unprotect command
Message 16: Std ID = 0x00, DLC = 1, data = 0x79 - ACK
Figure 6. Get Version & Read Protection Status command: host side
Start GV(1)
ACK
Receive 1 message:
2 dummy bytes having value 0x00
ACK
End of GV(1)
ai15707V2
Figure 7. Get Version & Read Protection Status command: device side
Start GV (1)
Received message No
Send NACK message
with std ID =
0x01?
Yes
Option message
ACK
Receive message 1:
data field contains the PID
ACK
End of GID(1)
ai15710
No
Received message Send NACK message
with std ID =
0x02?
Yes
Message 1: Std ID = 0x02, DLC = 1, data = ACK with DLC except for current message
and ACKs.
Message 2: Std ID = 0x02, DLC = N (the number of bytes – 1. For STM32, N = 1),
data = PID with byte 0 is MSB and byte N is the LSB of the product ID
Message 3: Std ID = 0x02, DLC = 1, data = ACK = 0x79
NACK
Wait for ACK
or NACK
ACK
1. After setting the new baud rate, the bootloader sends the ACK message. Therefore, the host sets its baud
rate while waiting for the ACK.
Received a message No
with std ID = 0x03
and with valid
data?
Send NACK message
Yes (old baud rate)
No
Baudrate set correctly?
ai15713
Message 1: Std ID = 0x03, DLC = 1, data[0] = ACK= 0x79: with old baud rate if the
receive message is correct else data[0] = NACK= 0x1F
Message 2: Std ID = 0x03, DLC = 1, data[0] = ACK = 0x79 with new baud rate
NACK
Wait for ACK or NACK
ACK
Send NACK message
Receive (N+1)/8 messages from bootloader
Received message No
with std ID = 0x11?
Yes
ROP inactive? No
Address valid?
Yes
3.6 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 starts if the message contains the following valid information:
• ID of the command is correct or not
• ReadOutProtection is disabled or enabled
• branch destination address is valid or not(data[0] is the address MSB and data[3] is
LSB
If the message content is correct it transmits an ACK message, otherwise it transmits a
NACK message.
Start Go command
End of Go
ai15716
Start Go command
Received message No
with std ID = 0x21?
Yes
ROP inactive? No
Address valid?
Yes
ai15717b
Note: 1 When writing to the RAM, user must not overlap the memory used by the bootloader
firmware.
2 No error is returned when performing write operations on write protected sectors.
ACK
If NACK message received
Send the data messages. At same
time, the host checks whether it
received a NACK message
Note: If the start address is invalid, the command is NACK-ed by the device.
The host sends the messages as follows:
Command message: Std ID = 0x31, DLC = 0x05, data[0] = 0xXX: MSB address,..., data[3] =
0xYY: LSB address, data[4] = N-1 (number of bytes to be written - 1), 0 < N ≤ 255).
Then the host sends N/8 message
Data message: Std ID = 0x31, DLC_1 = to 8, data = byte_11, … byte_18…
Data message_M: Std ID = 0x04, DLC_M = 1 to 8, data = byte_m1, …, byte_M8
Note: 1 DLC_1 + DLC_2 + ... DLC_M = 256 maximum
2 After each message the host receives the ACK or NACK message from the device
3 The bootloader does not check the standard ID of the data, so any ID from 0h to 0xFF can
be used. It is recommended to use 0x04.
Received message No
with stdID=0x31?
Yes
ROP inactive? No
Address valid?
Yes
Yes
Address in Flash ?
Yes
Address in RAM ?
Yes
Address=0x1FFF F800 ?
MS53180V1
In other terms, after sending the N/8 messages, host will receive two successive ACK; the
first will be sent by the device if the last message of the N/8 is correctly received, and the
second ACK will be sent after writing correctly the N/8 message at the requested address
Yes Global No
Erase?
ACK
Received message
No
with stdID = 0x43
and ROP?
Yes
Start WP(1)
ACK
Send message with the sector codes and
the number of sectors to be protected
ACK
End of WP(1)
ai15722
1. WP = Write Protect.
Start WP(1)
Received message No
with std ID = 0x63?
ROP inacttive
Yes
Send ACK message
Yes
Write-protect the requested sectors
End of WP(1)
ai15723
1. WP = Write Protect
ACK
ACK
Received No
message with
ID = 73h?
No
ROP inactive?
End of WPUN(1)
ai15725
ACK
ACK
End of RDP_PRM(1)
ai15726
Received
No
message with
ID = 0x82?
Yes
No
ROP inactive?
Yes
End of RDP_PRM(1)
ai15727V2
ACK
ACK
Start RDU_PRM(1)
Received No
message with
ID = 0x92?
Yes
Send ACK message
Disable ROP
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. For additional information about ST trademarks, please refer to www.st.com/trademarks. 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.