This is a fork of the wasp-os fork of dingara's fork of astronomer80's fork of foldedtoad's Python OTA DFU utility.
I plan to update this to use bleak for cross-platform support.
This is a Python program that uses bleak to achieve Over The Air (OTA) Device Firmware Updates (DFU) to a Nordic Semiconductor nRF5 (either nRF51 or nRF52) device via Bluetooth Low Energy (BLE).
- Perform OTA DFU to an nRF5 peripheral without an external USB BLE dongle.
- Ability to detect if the peripheral is running in application mode or bootloader, and automatically switch if needed (buttonless).
- Support for both Legacy (SDK <= 11) and Secure (SDK >= 12) bootloader.
Before using this utility the nRF5 peripheral device needs to be programmed with a DFU bootloader (see Nordic Semiconductor documentation/examples for instructions on that).
- Python 3.6 or above
- The Python packages listed in
requirements.txt(available via pip)
python3 -m venv .venv
.venv/bin/pip3 install requirements.txt
.venv/bin/pip3 install -r requirements.txt
- Your nRF5 peripheral firmware build method will produce a firmware file ending with either
*.hexor*.bin. - Your nRF5 firmware build method will produce an Init file ending with
.dat. - The typical naming convention is
application.binandapplication.dat, but this utility will accept other names.
Use the gen_dat application (you need to compile it with gcc gen_dat.c -o gen_dat on first run) to generate a .dat file from your .bin file. Example:
./gen_dat application.bin application.dat
Note: The gen_dat utility expects a .bin file input, so you'll get Cyclic Redundancy Check (CRC) errors during DFU using a .dat file generated from a .hex file.
An alternative is to use nrfutil from Nordic Semiconductor, but I've found this method to be easier. You may need to edit the gen_dat source to fit your specific application.
You need to use nrfutil to generate firmware packages for the new secure bootloader (SDK > 12) as the package needs to be signed with a private/public key pair. Note that the bootloader will need to be programmed with the corresponding public key. See the nrfutil repo for details.
Note: I've had problems with the pip version of nrfutil. I recommend installing from source instead.
There are two ways to specify firmware files for this utility. Either by specifying both the .hex or .bin file with the .dat file, or more easily by the .zip file, which contains both the hex and dat files.
The new .zip file form is encouraged by Nordic, but the older hex/bin + dat file methods should still work.
> sudo ./dfu.py -f ~/application.hex -d ~/application.dat -a CD:E3:4A:47:1C:E4
or:
> sudo ./dfu.py -z ~/application.zip -a CD:E3:4A:47:1C:E4
You can use scan.py to figure out the address of a DFU target, for example:
$ python3 scan.py
CD:E3:4A:47:1C:E4 <TARGET_NAME>
CD:E3:4A:47:1C:E4 (unknown)
================================
== ==
== DFU Server ==
== ==
================================
Sending file application.bin to CD:E3:4A:47:1C:E4
bin array size: 60788
Checking DFU State...
Board needs to switch in DFU mode
Switching to DFU mode
Enable Notifications in DFU mode
Sending hex file size
Waiting for Image Size notification
Waiting for INIT DFU notification
Begin DFU
Progress: |xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx| 100.0% Complete (60788 of 60788 bytes)
Upload complete in 0 minutes and 14 seconds
segments sent: 3040
Waiting for DFU complete notification
Waiting for Firmware Validation notification
Activate and reset
DFU Server done
- Implement link-loss procedure for Legacy Controller.
- Update example output in readme.
- Add makefile examples.
- More code cleanup.