MCU OTA Update

Last Updated on : 2024-06-24 08:13:31download

An over-the-air (OTA) update is the wireless delivery of new software, firmware, or other data to connected devices.

The MCU SDK supports OTA updates for the MCU. You can first upload the update file to the Tuya Developer Platform. The Bluetooth module can download the update from the server and send it to the MCU through the serial port. This way, the MCU gets the update and writes it to the local flash memory for installation. For more information, see Update Firmware and Select and Manage Firmware Versions.

Update process

The OTA update consumes the memory of the MCU. After a device is paired, an OTA update can be initiated through the preset configuration or check for updates. The following diagram describes how it works.

Yes
No
Yes
No
Yes
No
Yes
No
Start
Module sends an OTA update request
MCU accepts the OTA update request?
Module sends the update information
End
OTA update can proceed?
Module sends the update offset
MCU returns the offset of update that has been received
Module transfers the update
Transfer is finished?
Module sends the update completion command
MCU returns success?
Update succeeds
Update fails

Commands

The following table lists the commands used in the MCU OTA update process.

Command Description
0xE8 The Bluetooth module queries the MCU’s version number
0xE9 MCU proactively reports version number
0xEA Initiate an update request
0xEB Send the update file information
0xEC Request the update file offset
0xED Transmit the update file
0xEE Request the update result

Query MCU’s version number (0xE8)

The Bluetooth module uses this command to query the MCU version number while it gets the MCU information using the command 0x01.

The module sends the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xE8
4
5
2 Data length 0x00
0x00
6 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Example: 55 AA 00 E8 00 00 E7

The MCU returns the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xE8
4
5
2 Data length 0x00
0x06
6 to 11 6 Data See the following table
12 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Data format

1 to 3 4 to 6
Soft_Ver Hard_ver
  • Soft_Ver: The firmware version of the MCU. For example, 0x01 00 02 represents v1.0.2.

  • Hard_ver: The hardware version of the MCU, the PCBA version number.

Example: 55 AA 00 E8 00 06 01 00 00 01 00 00 EF

Report MCU version proactively (0xE9)

The MCU proactively sends its version number to the module after startup (generally after the UART initialization). If the module does not respond, the MCU will resend the message.

The MCU sends the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xE9
4
5
2 Data length 0x00
0x06
6 to 11 6 Data See the following table
12 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Data format

1 to 3 4 to 6
Soft_Ver Hard_ver
  • Soft_Ver: The firmware version of the MCU. For example, 0x01 00 02 represents v1.0.2.
  • Hard_ver: The hardware version of the MCU, the PCBA version number.

Example: 55 AA 00 E9 00 06 01 00 00 01 00 00 F0

The module returns the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xE9
4
5
2 Data length 0x00
0x01
6 1 State Return value
7 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Return value of state:

  • 0x00: Success
  • Other values: Failure

Example: 55 AA 00 E9 00 01 00 E9

Initiate an update request (0xEA)

The Bluetooth module initiates an update request to get the maximum length of one packet for serial transmission.

The module sends the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEA
4
5
2 Data length 0x00
0x02
6 to 7 2 The maximum length of one packet Len1 See the description below
8 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Len1: The maximum length of a packet that can be transmitted over the serial port, in the unit of bytes.

Example: 55 AA 00 EA 00 02 00 C8 B3

The MCU returns the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEA
4
5
2 Data length 0x00
0x06
6 to 11 6 Data See the following table
12 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Data format

1 2 to 4 5 to 6
Flag The MCU’s current firmware version (Version) The MCU-specified maximum length of a packet (Len2)
  • Flag: 0x00 indicates the update is accepted. 0x01 indicates the update is rejected.
  • Version: The current firmware version. For example, 0x01 00 02 represents v1.0.2.
  • Len1: The module-specified maximum length of a packet.
  • Len2: The MCU-specified maximum length of a packet. If Len1 is less than Len2, Len1 prevails. Otherwise, Len2 prevails.

Example: 55 AA 00 EA 00 06 00 01 00 00 00 C8 B8

Send the update file information (0xEB)

The module sends the information about the firmware update to the MCU. The MCU determines whether to accept the update accordingly.

The module sends the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEB
4
5
2 Data length 0x00
0x23
6 to 40 35 Data See the following table
41 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Data format

8 bytes 3 bytes 16 bytes 4 bytes 4 bytes
Product ID (PID) File version File MD5 hash File length CRC32
  • PID: The PID of the MCU.
  • Firmware version: The version of firmware to be installed. For example, 0x010002 represents v1.0.2.
  • File MD5 hash: The MD5 value of the firmware update.
  • File length: The total length of the firmware update, in bytes.
  • CRC32 checksum: The CRC32 value of the firmware update.

The MCU returns the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEB
4
5
2 Data length 0x00
0x19
6 to 30 25 Data See the following table
31 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Data format

1 byte 4 bytes 4 bytes 16 bytes
State The size of the update that has already been downloaded. The CRC32 checksum of the update that has already been downloaded. The MD5 hash value of the update that has already been downloaded. (Not used currently)

State:

  • 0x00: The update is performed as expected.
  • 0x01: The received PID does not match the one written to the device.
  • 0x02: The update version is earlier than or equal to the current version.
  • 0x03: The update size exceeds the upper limit.
  • Others: Reserved.
  • The MCU returns information about the update that has been downloaded to continue downloading from where it was interrupted.
    • After receiving the response message, the app will calculate the CRC32 checksum and compare it to the value received.
    • If the two match exactly, the app will change the start offset to the received data length in the following transmission request. Otherwise, the start offset is 0, meaning downloading from scratch.
  • The resumable transfer also follows the normal update process, starting from initiating an update request. If the module operates in any state rather than bound and connected, the MCU should reset the update status if any to start the following update procedure.

Request the update offset (0xEC)

Send the start offset of the file and compare it with the offset returned by the MCU. If the two match exactly, the file transfer can continue from that offset. Otherwise, the entire file will be transferred from scratch.

The module sends the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEC
4
5
2 Data length 0x00
0x04
6 to 9 4 Offset See the following table
10 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Offset: Indicate where to start downloading the update. The value has four bytes.

The MCU returns the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEC
4
5
2 Data length 0x00
0x04
6 to 9 4 Offset See the following table
10 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Offset: the start offset specified by the MCU.

The start offset address specified by the MCU should take precedence over the one provided by the app. The MCU-specified offset is usually less than the one provided by the app.

Transmit the update (0xED)

The module sends the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xED
4
5
2 Data length (Len) Upper 8 bits
Lower 8 bits
6 to 6+Len-1 Len Data See the following table
6+Len 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Data format

2 bytes 2 bytes 2 bytes n byte(s)
Packet ID The length (n) of current packet data The CRC16 checksum of the current packet The payload of the current packet

The packet ID starts from 0. The length of the current packet cannot exceed the specified maximum length of a packet.

The MCU returns the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xED
4
5
2 Data length 0x00
0x01
6 1 State Return value
7 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Return value of state:

  • 0x00: Success.
  • 0x01: Packet ID error.
  • 0x02: The data length does not match the expected one.
  • 0x03: CRC check failed.
  • 0x04: Other errors.

Request the update result (0xEE)

The module sends the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEE
4
5
2 Data length 0x00
0x00
6 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

The MCU returns the following data.

No. Bytes Field Description
0
1
2 Header 0x55
0xAA
2 1 Version number 0x00
3 1 Command (CMD) 0xEE
4
5
2 Data length 0x00
0x01
6 1 State Return value
7 1 CRC8 Start from the header, add up all the bytes, and then divide the sum by 256 to get the remainder

Return value of state:

  • 0x00: Success.
  • 0x01: The total data length error.
  • 0x02: The data length does not match the expected one.
  • 0x03: Other errors.

FAQs

Q: What should I pay attention to during the OTA update process of Bluetooth LE + X devices?

A: For Bluetooth LE + X devices, such as Bluetooth LE and LET Cat.1 combo devices, please note that a valid CRC32 checksum cannot be sent when the device is updated over the LTE Cat.1 channel. Therefore, the MCU should use MD5 to verify the update file. The CRC16 checksum of the update package is valid and available.