Low-end embedded devices in IoT networks typically utilize low-power communication protocols (e.g., BLE, ZigBee, sub-GHz, etc.) to extend battery life and conserve CPU/memory resources. To bridge the connection between these devices and the cloud, a more powerful device is often employed as a gateway device. Aside from collecting, manipulating, and forwarding data from the end devices, many gateways will allow for some type of device management. This could include services such as device provisioning, authentication, configuration, and/or firmware management.
In the article Implementing BLE OTA Firmware Updates on PSoC 6 with ModusToolbox, it was shown how the bootloader service could be added to the BLE GATT profile to enable OTA firmware updates on PSoC 6 devices. This article demonstrates how a gateway device can take advantage of this functionality to remotely update any PSoC device in the network that utilizes this bootloader service. To this end, an example application written in Python was developed to implement Cypress's Device Firmware Update (DFU) host protocol and send a new firmware image to a target device. It is therefore assumed that the gateway device is a Linux-based system with Python installed.
Any Linux-based device with BLE capability should work for this example. Note that some devices include integrated BLE chipsets (e.g., Raspberry Pi 4) while others will require the use of an external BLE adapter (e.g., BeagleBone Black). The example application is written in Python and utilizes the bluepy library to interface with the BLE device. The bluepy library was chosen because it is very simple to use and appears to still be supported. As a reference, the exact hardware and software components used to develop this application are listed below.
- BeagleBone Black (running Debian 10.6)
- BLE Dongle
- Python (version 3.7+)
- bluepy (version 1.3.0)
- crcmod (version 1.7)
Of course, a target device is also required to execute this example. Please refer to the article Implementing BLE OTA Firmware Updates on PSoC 6 with ModusToolbox for a complete walkthrough of adding the DFU library to a PSoC 6 device and generating the application image file.
The application's source code is available from the GitHub repository.
This section builds the foundation of the example application from the bottom-up. Starting with the Python bluepy library, the basics of connecting to a BLE peripheral and exchanging data will be presented. Next, Cypress's DFU host command/response protocol is explained. Finally, a brief discussion about the .cyadc2 file containing the application data is provided.
The Python bluepy library provides an interface for communicating with BLE devices from a Linux system. It defines several objects (e.g., Peripheral, Service, and Characteristic objects) which makes it a very intuitive library to work with. The bluepy documentation provides a description of all objects, their public interfaces, and some basic example code.
To connect to a BLE device, start by instantiating a Peripheral object and providing the device's MAC address as an argument. If the connection is successful, this peripheral object can be used to discover the device's services and characteristics, read/write the values of these characteristics, disconnect from the device etc. However, to receive asynchronous notifications from the device, a delegate object must be assigned to the Peripheral instance using the Peripheral.withDelegate() method. This delegate object must be a subclass of the DefaultDelegate class. As a simple example, we define a class named "MyDelegate" to simply store the handle of the characteristic that sent the notification along with the data it sent.
An example of scanning for devices and obtaining their MAC addresses is provided in the example application. For now, though, it is assumed that the device's MAC address is known.
Once connected to the BLE device, the desired services and characteristics must be discovered. For this application, the Command characteristic provided by the Bootloader service (see the GATT profile tree on left-hand side of Figure 1) will be used to exchange data with the target device. Fortunately, the discovery process is simple because we know the UUID of this characteristic is "00060001-F8CE-11E4-ABF4-0002A5D5C51B" (see the parameters configuration section on the right-hand side of Figure 1).
Figure 1: Noting the Command characteristic's UUID with the Bluetooth Configurator in ModusToolbox
The Peripheral.getCharacteristics() accepts the UUID of the characteristic in question as an argument. It returns a list of characteristic objects, which should contain exactly one item.
Finally, to receive notifications from the target device, notifications must be enabled by setting the first bit in the Client Characteristic Configuration Descriptor (CCCD). This descriptor is also shown in the GATT profile tree on left-hand side of Figure 1. To obtain a Descriptor object for the Command characteristic's CCCD, simply use the Characteristic.getDescriptors() method with the CCCD UUID provided as an argument. This UUID is easily accessible via the AssingedNumbers object as shown below. Note that, once again, a list with exactly one item should be returned.
With the Descriptor object for the CCCD in hand, notifications can be enabled by writing a value of 0x0001 to the descriptor. Use the Descriptor.write() method with the new value provided as an argument (as a Bytes object in little-endian byte order). Also, the withResponse parameter must be set to True. The Descriptor.read() method can then be used to verify that the change was successful.
At this point, the host can send commands to the target device and receive the target's responses. Listing 1 is an example of starting a DFU operation by sending the Enter DFU command. Lines 1 – 25 summarize the process of connecting to the target, discovering the Command characteristic, and enabling notifications. Lines 27 – 38 demonstrate the procedure for exchanging commands with the target. In the first step of this procedure, a DFU command is written to the Command characteristic using the Characteristic.write() method. Then, the Peripheral.waitForNotificaitons() method is used to block program execution until either a notification is received from the target or the timeout interval elapses. Lastly, the notification data is retrieved from the Delegate object and processed.
Running the code provided in Listing 1 results in the output shown in Figure 2. The structure of the command and response packets, as well as the procedure for updating the device's firmware, will be discussed in the next section.
Figure 2: Screenshot of the output resulting from running the program provided in Listing 1
Host Command/Response Protocol
The DFU host interacts with the target device using a simple command/response protocol, which is well-documented in Appendix B of . Command packets are sent by the host and response packets are sent by the target. An example of this was just shown in the above section (Figure 2) in which the Enter DFU command packet was sent by the host and the target responded with a packet indicating successful execution of the command. Let's look at these packets in more detail.
Figure 3: DFU command packet structure (from )
Figure 3 shows the structure of the command packets sent by the host. Note that the Data Length and Checksum fields are in little-endian format while the byte order of the "N bytes of data" field is dictated by the command itself. The Enter DFU command, shown explicitly in Figure 2 as "0138040004030201b9ff17", can then be decomposed as shown in Table 1. Comparing this to the Enter DFU command documentation in section B.2.1 in , it becomes clear how these values where chosen. The command byte is 0x38 and 4 bytes of data are provided as input. These bytes are the product ID and will be interpreted by the Enter DFU command as 0x01020304.
Table 1: Decomposition of the Enter DFU command packet sent by the host in Figure 2
|Start of Packet||Command||Data Length (N)||N Bytes of Data||Checksum|
End of Packet
The packet checksum is calculated by simply adding all the preceding bytes together and taking the 2's compliment of the result. Listing 2 provides an explicit example of a simple Python function capable of constructing command packets given only the command byte and the command payload (see the output in Figure 4).
Figure 4: Screenshot of the output resulting from running the program provided in Listing 2
The response packets returned by the target device have the same structure as the command packets except a 1-byte status code takes the place of the command byte, as shown in Figure 5. A success code of 0x00 indicates that the preceding command executed successfully on the target. All other codes indicate some type of error (see Table 11 in  for the complete list).
Figure 5: DFU response packet structure (from )
In Figure 2, it was shown that the target device responded to the Enter DFU command packet with "010008000000000000000401F2FF17". Table 2 breaks this packet down into its individual fields. Notice that the target returned a status code of 0x00 (success) and 8 bytes of data which, according to section B.2.1 of , can be interpreted as JTAG ID: 0x00000000, Device Revision: 0x00, and DFU SDK Version: 0x010400. The checksum can be verified in the same way as the command packet (see lines 14 – 17 in Listing 2).
Table 2: Deconstruction of the Enter DFU response packet sent by the target in Figure 2
|Start of Packet||Status Code||Data Length (N)||N Bytes of Data||Checksum|
End of Packet
Now that the host can construct DFU commands and verify the response packets, it can now be used to update the target device's firmware. The basic procedure for doing so is outlined with the flowchart in Figure 6. The blue elements represent DFU commands sent by the host and the others are essential data preparation steps. Note that not all the available DFU commands are used in this procedure. Again, see Appendix B of  for a complete list of available DFU commands.
Figure 6: Flowchart of a typical DFU operation performed by the host to update the target's firmware (DFU commands in blue)
This command starts a DFU operation. If any other commands are sent before this command, they will be ignored by the target device.
Set Application Metadata
There is a dedicated region of the target device's flash memory set aside for storing information about its applications (see Appendix D of ). For each application, the application start address (4 bytes) and application length (4 bytes) are stored in this section, the values for which are provided by the host via the Set Application Metadata command. An example of such a command packet is: "014C09000100000510FCFF000099FD17". Here, the data bytes are interpreted as Application: 1, Application Start Address: 0x10050000, and Application Length: 0x0000FFFC (65532 bytes). Note that these values were obtained from the application image file discussed in the Application Image File section.
Send Data/Program Data
At this point in the procedure, the host begins reading row data from the application image file and sending that data to the target device. A checksum must accompany this data so the target can verify its integrity before writing it to memory. This checksum is calculated using the CRC-32C algorithm. A simple way to get this value is with Python (Listing 3). Note that for simplicity, the Application object provided with the Example Application is used to read the first row of application data.
After calculating the checksum for the entire row of data, it is common practice to split the data into smaller pieces and send these pieces to the target one at a time. This prevents the command packets from becoming too large and starving the communication channel. The downside of this, however, is it will take longer for the host to transfer the application to the target device. Therefore, the size of the data pieces should be as large as the application/network can tolerate.
When splitting the row data into n smaller pieces, the first n-1 pieces are sent by the host using the Send Data command. When the target receives this command, it will simply append the received piece of data to a buffer without writing it to memory. The last piece of row data (piece number n) should be sent with the Program Data command along with the checksum calculated in Listing 3 and the memory address the row of data should be written to. When the target receives this last piece of data, it will append it to the buffer to complete the row, verify the entire row with the checksum, and write the row data to the specified address.
After the last row of data is successfully sent to the device, it is a good idea to follow up with the Verify Application command. This command tells the target to calculate the checksum of the entire application and compare it to the one stored in flash memory. As we will see in the Application Image File section, this checksum is stored in the last 4 bytes of the file.
Finally, to end the DFU operation, the host will send the Exit DFU command. Once the target receives this command, it will again ignore any commands that follow (unless it is the Enter DFU command).
Application Image File
In the article Implementing BLE OTA Firmware Updates on PSoC 6 with ModusToolbox, the process of creating a PSoC 6 application capable of being remotely loaded onto a target device is outlined. At the end of this process, after building the application, an application image file is generated as a project artifact. This file has the extension ".cyacd2" and it contains the loadable application firmware in an ASCII hex format. An example of such a file is provided in Listing 4. Note that only the first and last rows of data are shown for brevity. A complete description of the .cyacd2 file format can be found in Appendix C of .
The first lines of the file do not contain application data, but rather application metadata. First is the header row with the structure shown in Table 3. The header in Listing 4 indicates that the File Version is 1, the silicon ID is "00000000", the silicon revision is 0, the checksum type is 0, the app ID is 1, and the product ID is "01020304" (little-endian). Recall that the product ID is sent to the target using the Enter DFU command and the app ID is sent to the target using the Set Application Metadata command.
Table 3: .cyacd2 header row structure (derived from )
|File Version||Silicon ID||Silicon Revision||Checksum Type||App ID|
|1 byte||4 bytes||1 byte||1 byte||1 byte||4 bytes|
The second line is the Application Verification information. It begins with the string "@APPINFO:" and provides the application start address and the application length. Recall that these are the values sent to the target device via the Set Application Metadata command. In Listing 4, the start address is 0x10050000 and the length is 0xFFFC bytes (i.e., 65532 bytes). Note that, strangely, these values are in big-endian format.
The remaining rows in the file are the application data rows. Each of these rows corresponds to a row of memory in the target device which stores the application firmware. In .cyacd2 files, these rows have the structure shown in Figure 8. Again, the row address is in little-endian format.
Table 4: .cyacd2 data row structure (derived from )
|1 character ":"||4 bytes||N bytes|
The last line of row data is special because the last four bytes contain the checksum for the entire application. This can clearly be seen in Listing 4. When the host sends the Verify Application command to the target device, the target will calculate the checksum of the entire stored application firmware and compare it to these last four bytes. The target's response to the host indicates whether the checksums where a match.
The Example Application
Using the concepts described above, an example python application called update.py has been created to demonstrate how a Linux system can be utilized as a host to update a remote target device. The source code is available from the GitHub repository.
This application depends on the bluepy and crcmod packages, so be sure they are installed in your environment. By default, the application expects a virtual environment to be setup as follows:
Setting up the environment in a different manner may require the first line of the update.py file to be modified. The usage of the application is as follows:
$ update.py application_file [target_MAC_address]
The first argument is required and is the path to the application image file described above. The second argument is optional. If the MAC address of the target device is known, the user can provide it here and skip the process of scanning for available devices. If the second argument is not provided, the application will present the user with a list of available BLE devices from which the user must choose a device to update. An example of executing the application without the second argument is shown below in Figure 7.
If the second argument (target MAC address) is not provided, the application will have to be executed with root privileges.
Figure 7: Using the example application to scan for available BLE devices
The application consists of two python modules (files). The update.py module contains the main function and defines two classes: Target and ScannerUI. The Target class is simply a generalization of bluepy's Peripheral class, inheriting all of its functionality while adding a method called updateFirmware() (Listing 5). This method implements the procedure outlined in Figure 6 to update the target device's firmware. It also prints status information to the user as shown in Figure 8.
Figure 8: Using the example application to update the target device's firmware (lines omitted)
At this point, the ScannerUI object is unpolished and meant for demonstration purposes only. It works in tandem with the btle.Scanner object to present a simple user interface consisting of a table of discovered devices and a prompt for the user to choose a device to connect to (Figure 7).
The second module is called
cydfu.py. It also defines two classes: DFUProtocol and Application. The DFUProtocol class encapsulates the host command/response protocol described above. The Application class handles the extraction of data from the application image file.
Contact the Author
This application has been designed to remotely update the firmware of a PSoC target device utilizing Cypress's DFU middleware library. Though not perfect or complete by any means, it serves as an example of using a Python BLE library to exchange data with a peripheral device according to the DFU specification and perform OTA firmware updates. These core concepts can, of course, be repurposed to perform OTA updates on devices from other manufacturers (e.g., STMicroelectronics, Microchip, Renesas) as well. To that end, this application is a great starting point.
 M. Ainsworth, "PSoC 6 MCU Device Firmware Update Software Development Kit Guide," Appl. Note 213924, pp. 44-53, 8 December 2018.