Introduction

In the previous post, Implementing Over-the-Air Device Firmware Update for nRF52 – Part 2, we introduced the OTA DFU process on the nRF52.

In this post, we’ll go over testing the process using one of the DFU examples included in the nRF5 SDK, the “BLE Secure DFU Bootloader” example..

The hardware requirements for running the BLE Secure DFU Bootloader example are:

  • A development PC (Windows, Linux, or macOS) – for building the bootloader and application, and generating cryptographic keys.
  • An nRF52832 or nRF52840 development kit – used as the DFU target (Note: In all the steps below, we’ll be using an nRF52840 DK).
  • A mobile phone (iOS or Android) – used as the DFU controller (alternatively, you could use a second nRF52 DK along with your development PC and use nRF Connect for Desktop).

The software requirements are:

  • nRF5 SDK version 15.3.0
  • Segger Embedded Studio (SES)
  • Python (and pip) – check out the following articles for installation instructions:
  • GNU ARM Embedded Toolchain
    • Download from the official website here
    • Set your system’s path to include the <GNU ARM Tolchain>/bin folder – mostly needed for Linux and macOS. Windows will do this automatically during the installation of the toolchain.
    • Edit nRF5 SDK’s Makefile.posix file to point to the correct location of the GNU ARM Toolchain and indicate the correct version (you can find out the version by running arm-none-eabi-gcc –version). The Makefile.posix file is located at <nRF5 SDK Folder>/components/toolchain/gcc/Makefile.posix.
  • nRF Connect for Mobile (iOS, or Android) or nRF Connect for Desktop if you’re using a PC as the DFU controller.
  • nRF Command Line Tools – download the appropriate version for your operating system and install the two packages included (JLink installer & nRF Command Line Tools installer).
  • Git
  • Set up the micro-ecc library:
    • Clone the GitHub repo. You can simply run the following command in your terminal or command prompt:
      git clone https://github.com/kmackay/micro-ecc.git
    • Make sure to clone it in the folder: <nRF5 SDK Folder>/external/micro-ecc/micro-ecc
    • Navigate to the folder <nRF5 SDK Folder>/external/micro-ecc/nrf52hf_armgcc/armgcc/
    • Note: the *hf* in the folder name above is used for chipsets that have an FPU (floating-point unit). If your target chipset does not include an FPU, then use the folder with *nf* in the name.
    • Build the library by running make
    • The output file will be linked when building the Secure Bootloader

Once you’ve got all the hardware and software installed, you’re ready to move on to the next step.

Testing Out OTA DFU on nRF52

Here are the general steps needed to add and perform Secure OTA DFU over BLE for your nRF52 project. In our specific case, we’ll be testing the BLE Secure DFU Bootloader example.

1 – Generate Private-Public Key Pair

  • Navigate to a folder of your choice for storing the private-public key pair files.
  • Run the commands:
    nrfutil keys generate private.key
    nrfutil keys display --key pk --format code private.key --out_file public_key.c

These commands will create private-public key pair files. For example:

private.key

public_key.c

2 – Replace the Placeholder Public Key File

  • Open the example located at <nRF5 SDK Folder>/examples/dfu/secure_bootloader/pca10056_ble/ses/secure_bootloader_ble_s140_pca10056.emProject
  • Replace the file dfu_public_key.c with the one generated in step #1, or simply copy/paste the contents of public_key.c into dfu_public_key.c

3 – Build the BLE Secure DFU Bootloader

Build the BLE Secure DFU Bootloader example project (using Segger Embedded Studio).

4 – Flash the Bootloader and the SoftDevice to the nRF52 DK

  • Load both the Bootloader and the SoftDevice using SES.
  • Choose the top menu “Target” ==> “Connect J-Link”.
  • Once connected, choose the top menu “Target” again ==> “Erase All”.
  • When that’s finished, we can now load the Bootloader and SoftDevice. Choose “Target” ==> “Download secure_bootloader_ble_s140_pca10056”.

5 – Verify that BLE Secure Bootloader is running

  • After loading the images in the previous step, verify that both LED 1 and LED 2 are on.
  • Next, open up nRF Connect on Mobile (or Desktop ==> Bluetooth app) and verify that the device is advertising with the name “DfuTarg”:
  • This indicates that the BLE Secure DFU Bootloader is running correctly and ready to accept a new DFU package.

6 – Building the nRF52 Application

  • Next, we’ll build our nRF52 application in SES.
  • This is the application that we want to include in the DFU package.
  • Once you build it, locate the output .hex file.

7 – Generating the DFU Package

  • To generate the DFU package .zip file, we need to use the nrfutil command-line tool.
  • The command takes in the private key generated from step #1 and the .hex file from step #6.
  • In this example, we’ll be including the application only (no Bootloader or SoftDevice).
  • Copy the .hex file from step #6 (.hex file of the application) to the same location as the private key from step #1.
  • Now, run the following command:
    nrfutil pkg generate --hw-version 52 --application-version 1 --application ble_app_blinky_pca10056_s140.hex --sd-req 0xB6 --sd-id 0xB6 --key-file private.key app_dfu_package.zip
  • Let’s look at each of the arguments passed into the command:
    • pkg: Display or generate a DFU package (zip file).
    • generate: Generate a zip file for performing DFU.
    • --hw-version 52: The hardware version.
    • --application-version 1: The assigned application version.
    • --application ble_app_blinky_pca10056_s140.hex: The application hex file to be included in the DFU package.
    • --sd-req 0xB6: The SoftDevice firmware ID(s) required for the update to be processed, of which one must be present on the target device. Below is the list of SoftDevice firmware IDs supported by version 5.2.0 of the nrfutil command-line tool. We are using SoftDevice s140 version 6.1.1 which matches ID value: 0xB6.
    • --sd-id 0xB6: The new SoftDevice ID to be used as –sd-req for the Application update in case the ZIP contains a SoftDevice and an Application.
    • --key-file private.key: The private (signing) key in PEM format.
    • app_dfu_package.zip: Name of the output ZIP file (the DFU package).

8 – Discover and Connect to the DFU Target

  • Use nRF Connect for Mobile (or nRF Connect for Desktop Bluetooth App with a second nRF52 DK connected to the PC) and look for the “DfuTarg” advertisement.
  • You’ll need to make sure the package you created in step #7 is accessible on the mobile phone you’re using (if you’re using nRF Connect for Mobile).
  • Now, connect to the DFU target:

9 – Start the DFU Process

  • After you’ve connected to the DFU target in nRF Connect on Mobile, swipe to the left twice to navigate to the DFU screen. Then click “Open Document Picker”:
  • Choose the .zip file (from step #7) and start the DFU process.
  • Click the “Start” button:

10 – New Firmware Application

Once the DFU process is complete, you should see that your new application (that was part of the DFU package) is now running on the DFU Target development kit.
In this example, I used the ble_app_blinky peripheral application:

Next Steps: Buttonless DFU for your Nordic nRF52 project

If you’re interested in learning about over-the-air device firmware update in much more detail including implementing it in your own nRF52 project, then check out the Bluetooth Developer Academy where I cover this topic in a full course.

By joining the Bluetooth Developer Academy, you will get access to a growing library of courses and tutorials.

Here’s what one Academy member has to say:

If you’re developing a BLE project, you need two things, a good BLE sniffer and the Bluetooth Developer Academy. I am very happy to be part of this community and look forward to what comes next.

– Christopher Gates, Principal System Security Architect – Velentium

The current courses include:

  • The Basics of Bluetooth Low Energy
  • Analysis of BLE events using a BLE sniffer
  • Long-range mode (Coded PHY) using Bluetooth 5.0
  • Developing nRF52 applications using Visual Studio Code
  • Over the Air Device Firmware Update (OTA DFU) – nRF52 use case
  • Getting Started with Zephyr (including adding custom GATT Services and Characteristics)
  • The Developer’s Guide to what’s new in Bluetooth 5.2
  • SweynTooth: A Summary for BLE Developers
  • Introduction to BLE Security
  • Getting Started with BlueZ development
  • Introduction to BLE Development for iOS
  • …and more courses added each month!

For a full list of courses included, check out the Courses Library here:

Bluetooth Developer Academy Courses Library

The Academy also features a thriving community of Bluetooth experts, developers, and innovators. You’ll get to connect and interact with other experts in the Bluetooth space, learn from others’ experience and knowledge, and share yours.

Also included in the Academy is access to private support from me personally.

In the community, you will find:

  • Discussions around new features such as long-range mode (Bluetooth 5.0) and direction-finding (Bluetooth 5.1).
  • Discussions around the capabilities of different BLE sniffers.
  • Comparisons of BLE support and restrictions in iOS and Android.
  • Various technical questions and answers to these questions.
  • Listing of Bluetooth-related job openings.
  • And many more discussions!

Learn More About the Bluetooth Developer Academy

Summary & Closing

In this post, we went over:

  • Hardware Requirements
  • Software Requirements
  • The different tools needed to build a DFU image
  • Steps for building and testing out the DFU example

2 Comments

  1. Avatar Lars Hammarstrand (CENTIX) on February 4, 2020 at

    Very useful practical articles with good examples, thank you!

    Ps..
    Looking forward to joining the Developer Academy after we’ve performed the internal bootcamp with our new staff.

    • Mohammad Afaneh Mohammad Afaneh on February 4, 2020 at

      Thanks, Lars.

Leave a Comment