Transcription of AN3155 Application note - STMicroelectronics
1 February 2022AN3155 Rev 151/481AN3155 Application noteUSART protocol used in the STM32 bootloader IntroductionThis Application note describes the USART protocol used in the STM32 microcontroller bootloader, providing details on each supported document applies to STM32 products embedding any bootloader version, as specified in Application note AN2606 STM32 system memory boot mode, available on These products are listed in Ta b l e 1, and are referred to as STM32 throughout the more information about the USART hardware resources and requirements for your device bootloader, refer to the already mentioned AN2606. Table 1. Applicable productsTypeProduct seriesMicrocontrollersSTM32F0 SeriesSTM32F1 SeriesSTM32F2 SeriesSTM32F3 SeriesSTM32F4 SeriesSTM32F7 SeriesSTM32G0 SeriesSTM32G4 SeriesSTM32H7 SeriesSTM32L0 SeriesSTM32L1 SeriesSTM32L4 SeriesSTM32L5 SeriesSTM32U5 SeriesSTM32WB SeriesSTM32WL Rev 15 Contents1 USART bootloader code sequence.
2 52 Choosing the USARTx baud rate .. baud rate .. baud rate .. 63 Bootloader command set .. command .. Version & Read Protection Status command .. ID command .. Memory command .. command .. Memory command .. Memory command .. Erase Memory command .. Protect command .. Unprotect command .. Protect command .. Unprotect command .. Checksum command .. command .. Special command .. 414 Bootloader protocol version evolution .. 455 Revision history .. 46AN3155 Rev 153/48AN3155 List of tables3 List of tablesTable products .. 1 Table bootloader commands .. 7 Table protocol versions .. 45 Table revision history .. 46 List of figuresAN31554/48AN3155 Rev 15 List of figuresFigure for STM32 with USART.
3 5 Figure command: host side .. 8 Figure command: device side .. 9 Figure Version & Read Protection Status command: host side .. 10 Figure Version & Read Protection Status command: device side.. 11 Figure ID command: host side .. 12 Figure ID command: device side.. 13 Figure Memory command: host side .. 14 Figure Memory command: device side .. 15 Figure command: host side .. 16 Figure command: device side .. 17 Figure Memory command: host side .. 19 Figure Memory command: device side.. 20 Figure Memory command: host side.. 22 Figure Memory command: device side .. 23 Figure Erase Memory command: host side .. 25 Figure Erase Memory command: device side .. 26 Figure Protect command: host side .. 28 Figure Protect command: device side.
4 29 Figure Unprotect command: host side .. 30 Figure Unprotect command: device side .. 31 Figure Protect command: host side.. 32 Figure Protect command: device side .. 32 Figure Unprotect command: host side .. 33 Figure Unprotect command: device side .. 34 Figure Checksum command: host side .. 37 Figure Checksum command: device side .. 38 Figure command: host side .. 39 Figure command: device side .. 40 Figure Special command: host side.. 42 Figure Special command: device side .. 43AN3155 Rev 155/48AN3155 USART bootloader code sequence471 USART bootloader code sequenceFigure 1. Bootloader for STM32 with USARTOnce the system memory boot mode is entered and the STM32 microcontroller (based 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 on the USART IP, the baudrate detection is based on the HW (IP supporting auto baudrate) or on the SW.
5 The following paragraphs explain the SW detection 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 , 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 Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or received onUSARTx Rx pinWait for acommandGET cmdroutineai15702 USARTx selectedAuto-baud rate sequencesend ACK byte & disableunused peripheralsRD cmdroutine(optional)Routines forloading into RAMGO cmdroutineJP to_AddressGO cmdCommandreceivedGET cmdChoosing the USARTx baud rateAN31556/48AN3155 Rev 152 Choosing the USARTx baud rateThe 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.
6 However, the upper and lower limits have to be kept, to ensure proper data 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 The deviation (fB, in percent) between the host baud rate and the microcontroller baud rate can be calculated using the formula below: fBSTM32 baud rate Host baud rate STM32 baud rate------------------------------------ ---------------------------------------- ---------------100% =, where fB 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 Minimum baud rateThe lowest tested baud rate (BLow) is 1200.
7 Baud rates below BLow cause SysTick timer overflow. In this event, USARTx is not correctly Maximum baud rateBHigh is the highest baud rate for which the deviation does not exceed the limit. All baud rates between BLow and BHigh are below the deviation highest tested baud rate (BHigh) is Rev 157/48AN3155 Bootloader command set473 Bootloader command setThe supported commands are listed in Ta b l e 2. Each command is described in this section. Communication safetyAll communication from the programming tool (PC) to the device is verified : 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). Table 2. USART bootloader commandsCommand(1)1.
8 If a denied command is received or an error occurs during the command execution, the bootloader sends NACK byte and goes back to command (2)2. Read protection. When the RDP (Read protection) option is active, only this limited subset of commands is available. All other commands are NACK-ed and have no effect on the device. Once the RDP has been removed, the other commands become the version and the allowed commands supported by the current version of the Version & Read Protection Status(2)0x01 Gets the bootloader version and the Read protection status of the Flash ID(2)0x02 Gets the chip Memory(3)3. Refer to STM32 product datasheets and to AN2606 to know the valid memory areas for these up to 256 bytes of memory starting from an address specified by the (3)0x21 Jumps to user Application code located in the internal Flash memory or in the Memory(3)0x31 Writes up to 256 bytes to the RAM or Flash memory starting from an address specified by the (3)(4)4.
9 Erase (x043) and Extended Erase (0x44) are exclusive. A device can support either the Erase command or the Extended Erase command, but not from one to all the Flash memory Erase(3)(4)0x44 Erases from one to all the Flash memory pages using two byte addressing mode (available only for USART bootloader versions and above).Special0x50 Generic command that allows to add new features depending on the product constraints without adding a new command for every Special0x51 Generic command that allows the user to send more data compared to the Special Protect0x63 Enables the write protection for some Unprotect0x73 Disables the write protection for all Flash memory Protect0x82 Enables the read Unprotect(2)0x92 Disables the read Checksum0xA1 Computes a CRC value on a given memory area with a size multiple of 4 command setAN31558/48AN3155 Rev 15By XOR-ing all received bytes, data plus checksum, the result at the end of the packet must be For each command the host sends a byte and its complement (XOR = 0x00).
10 3. UART: parity check active (even parity).Each packet is either accepted (ACK answer) or discarded (NACK answer): ACK = 0x79 NACK = Get commandThe Get command allows the user to get the version of the bootloader and the supported commands. When the bootloader receives the Get command, it transmits the bootloader version and the supported command codes to the host, as shown in Figure 2. Get command: host sideSend 0x00 + 0xFF Start GetWait for ACKor NACKR eceive the number of bytes(version+commands)Receive the bootloader versionReceive the supported commandsWait for ACKor NACKEnd of GetNACKACKai14631 NACKACKAN3155 Rev 159/48AN3155 Bootloader command set47 Figure 3. Get command: device sideThe STM32 sends the bytes as follows:Byte 1:ACKByte 2:N = 11 = the number of bytes to follow 1 except current and 3:Bootloader version (0 < version < 255), example: 0x10 = version 4:0x00 Get commandByte 5:0x01 Get Version and Read Protection StatusByte 6:0x02 Get IDByte 7:0x11 Read Memory commandByte 8:0x21Go commandByte 9:0x31 Write Memory commandByte 10:0x43 or 0x44 Erase command or Extended Erase command (exclusive commands)Byte 11:0x63 Write Protect commandByte 12:0x73 Write Unprotect commandSend ACK byte Start GetReceivedbyte = 0x00+0xFF?