MCUXpresso SDK Documentation

Bootloader packet types#

Introduction#

The MCU bootloader device works in the slave mode. All data communication is initiated by a host, which is either a PC or an embedded host. The MCU bootloader device is the target that receives a command or a data packet. All data communication between the host and the target is packetized.

Note: The term “target” refers to the “MCU bootloader device”.

There are six types of packets used:

  • Ping packet

  • Ping Response packet

  • Framing packet

  • Command packet

  • Data packet

  • Response packet

All fields in the packets are in the little-endian byte order.

Parent topic:Bootloader packet types

Ping packet#

The Ping packet is the first packet sent from the host to the target to establish a connection on a selected peripheral to run the autobaud. The Ping packet can be sent from the host to the target anytime that the target is expecting a command packet. If the selected peripheral is UART, the ping packet must be sent before any other communications. For other serial peripherals it is optional, but it is recommended to determine the serial protocol version.

In response to the Ping packet, the target sends the Ping Response packet, discussed further on in the document.

Ping packet format

Byte #

Value

Name

0

0x5A

start byte

1

0xA6

ping

Ping packet protocol sequence

Parent topic:Bootloader packet types

Ping Response packet#

The target sends the Ping Response packet back to the host after receiving the Ping packet. If the communication is over a UART peripheral, the target uses the incoming Ping packet to determine the baud rate before replying with the Ping Response packet. When the Ping Response packet is received by the host, the connection is established and the host starts sending commands to the target.

Ping Response packet format

Byte #

Value

Parameter

0

0x5A

start byte

1

0xA7

Ping response code

2

Protocol bugfix

3

Protocol minor

4

Protocol major

5

Protocol name = ‘P’ (0x50)

6

Options low

7

Options high

8

CRC16 low

9

CRC16 high

The Ping Response packet can be sent from the host to the target anytime the target expects a command packet. For the UART peripheral to run the autobaud, it must be sent by the host when a connection is first established. It is optional for the other serial peripherals, but it is recommended to determine the serial protocol version. The version number is in the same format as the bootloader version number returned by the GetProperty command.

Parent topic:Bootloader packet types

Framing packet#

The framing packet is used for the flow control and error detection for the communications links that do not have such features built in. The framing packet structure sits between the link layer and the command layer. It wraps the command and data packets as well.

Every framing packet containing data sent in one direction results in a synchronizing response framing packet in the opposite direction.

The framing packet described in this section is used for serial peripherals including the UART, I2C, and SPI. The USB HID peripheral does not use the framing packets. Instead, the packetization inherent in the USB protocol itself is used.

Framing Packet Format

Byte #

Value

Parameter

0

0x5A

start byte

1

packetType

2

length_low

Length is a 16-bit field that specifies the entire command or data packet size in bytes.

3

length_high

4

crc16_low

This is a 16-bit field. The CRC16 value covers the entire framing packet, including the start byte and command or data packets, but does not include the CRC bytes. See the CRC16 algorithm after this table.

5

crc16_high

6 …n

Command or Data packet payload

A special framing packet that contains only a start byte and a packet type is used for synchronization between the host and the target.

Special Framing Packet Format

Byte #

Value

Parameter

0

0x5A

start byte

1

0xAn

packetType

The Packet Type field specifies the type of the packet from one of these defined types:

packetType Field

packetType

Name

Description

0xA1

kFramingPacketType_Ack

The previous packet was received successfully; the sending of more packets is allowed.

0xA2

kFramingPacketType_Nak

The previous packet was corrupt and must be re-sent.

0xA3

kFramingPacketType_AckAbort

The data phase is being aborted.

0xA4

kFramingPacketType_Command

The framing packet contains a command packet payload.

0xA5

kFramingPacketType_Data

The framing packet contains a data packet payload.

0xA6

kFramingPacketType_Ping

Sent to verify that the other side is alive. Also used for the UART autobaud.

| |0xA7|kFramingPacketType_PingResponse|A response to Ping; contains the framing protocol version number and options.|

Parent topic:Bootloader packet types

CRC16 algorithm#

This section provides the CRC16 algorithm.

The CRC is computed over each byte in the framing packet header, excluding the crc16 field itself, plus all payload bytes. The CRC algorithm is the XMODEM variant of CRC-16.

The characteristics of the XMODEM variant are:

|width|16| |polynomial|0x1021| |init value|0x0000| |reflect in|false| |reflect out|false| |xor out|0x0000| |check result|0x31c3|

The check result is computed by running the ASCII character sequence “123456789” through the algorithm.

uint16_t crc16_update(const uint8_t * src, uint32_t lengthInBytes)
{
     uint32_t crc = 0;
     uint32_t j;
     for (j=0; j < lengthInBytes; ++j)
     {
          uint32_t i;
          uint32_t byte = src[j];
          crc ^= byte << 8;
          for (i = 0; i < 8; ++i)
          {
               uint32_t temp = crc << 1;
               if (crc & 0x8000)
               {
                    temp ^= 0x1021;
               }
               crc = temp;
           }
     }
          return crc;
}

Parent topic:Bootloader packet types

Command packet#

The command packet carries a 32-bit command header and a list of 32-bit parameters.

Command packet format

Command packet format (32 bytes)

Command header (4 bytes)

28 bytes for Parameters (Max 7 parameters)

Tag

Flags

Rsvd

Param Count

Param1 (32-bit)

Param2 (32-bit)

Param3 (32-bit)

Param4 (32-bit)

Param5 (32-bit)

Param6 (32-bit)

Param7 (32-bit)

byte 0

byte 1

byte 2

byte 3

-

-

-

-

-

-

-

Command Header format

Byte #

Command header field

0

Command or Response tag

The command header is 4 bytes long, with these fields.

1

Flags

2

Reserved. Should be 0x00.

3

ParameterCount

The header is followed by 32-bit parameters up to the value of the ParameterCount field specified in the header. Because a command packet is 32 bytes long, only seven parameters fit into the command packet.

The command packets are also used by the target to send responses back to the host. The command packets and data packets are embedded into the framing packets for all of the transfers.

Command Tags

Command Tags

Name

0x01

FlashEraseAll

The command tag specifies one of the commands supported by the MCU bootloader. The valid command tags for the MCU bootloader are listed here.

0x02

FlashEraseRegion

0x03

ReadMemory

0x04

WriteMemory

0x05

FillMemory

0x06

FlashSecurityDisable

0x07

GetProperty

0x08

Reserved

0x09

Execute

0x10

FlashReadResource

0x11

Reserved

0x0A

Call

0x0B

Reset

0x0C

SetProperty

0x0D

FlashEraseAllUnsecure

0x0E

FlashProgramOnce

0x0F

FlashReadOnce

0x10

FlashReadResource

0x11

ConfigureMemory

0x12

ReliableUpdate

Response Tags

Response Tag

Name

0xA0

GenericResponse

The response tag specifies one of the responses the MCU bootloader (target) returns to the host. The valid response tags are listed here.

0xA7

GetPropertyResponse (used for sending responses to GetProperty command only)

0xA3

ReadMemoryResponse (used for sending responses to ReadMemory command only)

0xAF

FlashReadOnceResponse (used for sending responses to FlashReadOnce command only)

0xB0

FlashReadResourceResponse (used for sending responses to FlashReadResource command only)

Flags: Each command packet contains a flag byte. Only bit 0 of the flag byte is used. If bit 0 of the flag byte is set to 1, then the data packets follow in the command sequence. The number of bytes that are transferred in the data phase is determined by a command-specific parameter in the parameters array.

ParameterCount: The number of parameters included in the command packet.

Parameters: The parameters are word-length (32 bits). With the default maximum packet size of 32 bytes, a command packet can contain up to seven parameters.

Parent topic:Bootloader packet types

Response packet#

The responses are carried using the same command packet format wrapped with the framing packet data. The types of responses include:

  • GenericResponse

  • GetPropertyResponse

  • ReadMemoryResponse

  • FlashReadOnceResponse

  • FlashReadResourceResponse

GenericResponse: After the MCU bootloader has processed a command, the bootloader sends a generic response with the status and command tag information to the host. The generic response is the last packet in the command protocol sequence. The generic response packet contains the framing packet data and the command packet data (with generic response tag = 0xA0) and a list of parameters (defined in the next section). The parameter count field in the header is always set to 2, for the status code and command tag parameters.

GenericResponse parameters

Byte #

Parameter

Descripton

0 - 3

Status code

The Status codes are errors encountered during the execution of a command by the target. If a command succeeds, then a kStatus_Success code is returned.

4 - 7

Command tag

The Command tag parameter identifies the response to the command sent by the host.

GetPropertyResponse: The GetPropertyResponse packet is sent by the target in response to the host query that uses the GetProperty command. The GetPropertyResponse packet contains the framing packet data and the command packet data with the command/response tag set to the GetPropertyResponse tag value (0xA7).

The parameter count field in the header is set to greater than 1 to always include the status code and one or many property values.

GetPropertyResponse parameters

Byte #

Value

Parameter

0 - 3

Status code

4 - 7

Property value

…

…

Can be up to a maximum of six property values, limited to the size of the 32-bit command packet and property type.

ReadMemoryResponse: The ReadMemoryResponse packet is sent by the target in a response to the host sending a ReadMemory command. The ReadMemoryResponse packet contains the framing packet data and the command packet data with the command/response tag set to the ReadMemoryResponse tag value (0xA3) and the flags field is set to kCommandFlag_HasDataPhase (1).

The parameter count set to 2 for the status code and the data byte count parameters shown here.

ReadMemoryResponse parameters

Byte #

Parameter

Descripton

0 - 3

Status code

The status of the associated Read Memory command.

4 - 7

Data byte count

The number of bytes sent in the data phase.

**FlashReadOnceResponse:**The FlashReadOnceResponse packet is sent by the target in response to the host sending a FlashReadOnce command. The FlashReadOnceResponse packet contains the framing packet data and the command packet data with the command/response tag set to a FlashReadOnceResponse tag value (0xAF) and the flags field set to 0. The parameter count is set to 2 plus the number of words requested to be read in the FlashReadOnceCommand.

FlashReadOnceResponse parameters

Byte #

Value

Parameter

0 – 3

Status Code

4 – 7

Byte count to read

…

…

Can be up to 20 bytes of requested read data.

The FlashReadResourceResponse packet is sent by the target in response to the host sending a FlashReadResource command. The FlashReadResourceResponse packet contains the framing packet data and command packet data with the command/response tag set to a FlashReadResourceResponse tag value (0xB0) and the flags field set to kCommandFlag_HasDataPhase (1).

FlashReadResourceResponse parameters

Byte #

Value

Parameter

0 – 3

Status Code

4 – 7

Data byte count

Parent topic:Bootloader packet types