1.4.2 GCC for ARM ........................................................................................................................................ 5
1.4.3 GNU make ............................................................................................................................................. 5
1.4.4 Fetch and compile the encryption libraries ................................................................................... 6
2.1 Bluetooth address ....................................................................................................................................... 7
6 Test DFU ............................................................................................................................................. 27
6.1 Program and test application ................................................................................................................. 27
6.2 Prepare and test update .......................................................................................................................... 28
A Glossary .............................................................................................................................................. 30
Related documents ................................................................................................................................ 31
Revision history ....................................................................................................................................... 31
UBX-19050198 - R03 Contents Page 3 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
1 Software preparation
1.1 Nordic Semiconductor nRF5 SDK
A fresh installation of the nRF5 SDK is assumed. The SDK may be downloaded from reference [6]. For
this application note, the installation is assumed to be located in:
C:\u-blox\nRF5_SDK_17.0.0_9d13099
1.2 SEGGER Embedded Studio
Nordic Semiconductor have partnered with SEGGER to provide a no-cost, no-size-limit, commercial
license for SEGGER Embedded Studio (SES) for use with Nordic ICs, including the nRF5x series on
which many u-blox modules are based. Install SES from SEGGER and a license from Nordic:
Several utilities from Nordic Semiconductor are used throughout the process. Install these utilities:
1.3.1 nRF Connect for Desktop
nRF Connect for Desktop provides several tools for working with the Nordic Semiconductor-based
u-blox modules. nRF Connect for Desktop may be downloaded from reference [7].
1.3.2 nRF Command Line Tools
nRF Command Line Tools provides nrfjprog and mergehex, used for development, programming, and
debugging. nRF Command Line Tools may be downloaded from reference [8].
1.3.3 nRF Util
nRF Util is used for generating the DFU update packages, bootloader settings generation, key
generation, and performing updates. nRF Util may be downloaded from the Nordic Semiconductor
GitHub repository [9].
nRF Util depends on Python v3.7 or later. Python may be downloaded from reference [10].
☞ It is suggested to install the python component wheel before installing nrfutil:
pip install wheel
UBX-19050198 - R03 Software preparation Page 4 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
1.4 Encryption libraries
Encryption libraries are required to create the signed update files use with DFU. Follow the steps
outlined in the Nordic instructions to add the encryption libraries to the SDK. For installation
instructions, see reference [11].
The following installations are required to install the encryption libraries.
1.4.1 Git
Git is a distributed version control system used by many development projects. macOS supports git
directly through installing X-Code. On Linux, install git with the package manager for the operating
system. On Windows, install it from reference[12].
☞ Any other form of git may be used, such as through Linux installed on Windows through WSL or a
virtual machine.
The SDK uses git to fetch the source code for micro-ecc.
1.4.2 GCC for ARM
The GCC for ARM compiler is used to compile the micro-ecc libraries. GCC for ARM may be downloaded
from reference [13].
The nRF5 SDK v17 uses a newer version of the ARM GCC compiler than the instructions at the
InfoCenter link indicates. Download this version:
GNU Arm Embedded Toolchain: 7-2018-q2-update June 27, 2018
☞ When executing the installer, be sure to select the option to "Add path to environment variable".
1.4.3 GNU make
The make utility is used to manage the build process. There are several ways to install it. macOS and
Linux include
reference [14].
On Windows, update the PATH environment variable to include the binary folder for
C:\Program Files (x86)\GnuWin32\bin
make as part of the usual development tools. For Windows, the installer is found at
make.exe:
UBX-19050198 - R03 Software preparation Page 5 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
1.4.4 Fetch and compile the encryption libraries
Once the GCC compiler, git, and make are in place, a shortcut provided within the SDK is used to do
compile and install the encryption libraries. Open a Command window and navigate to the directory
At this point, the SDK has the necessary encryption libraries not only for DFU use, but also for
establishing secure connections through pairing and bonding.
The preparation to this point only needs done once after the SDK zip file is extracted.
UBX-19050198 - R03 Software preparation Page 6 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
2 Hardware preparation
For this application note, the BMD-340-EVAL board and a nRF52840 USB dongle are used to
demonstrate the process.
Any of the nRF52840 or nRF52832 boards may be used: EVK-NINA-B3, nRF52840 Dongle, nRF52840
DK, EVK-ANNA-B1, EVK-NINA-B1, BMD-300-EVAL, BMD-301-EVAL, BMD-350-EVAL, or Nordic
Semiconductor nRF52 DK.
If an alternative board is being used, adjust the directories for the project:
☞ The BMD-330-EVAL and BMD-360-EVAL have limited DFU functionality. For example, updating
the SoftDevice (Bluetooth stack) is not supported due to the flash and RAM memory sizes.
2.1 Bluetooth address
u-blox modules are programmed with a unique, public Bluetooth address (MAC address).
⚠ Some of the activities outlined in this application note require fully erasing the module, including
the Bluetooth address.
To save this address for later reference, use nrfjprog to read the User Information Configuration
Register (UICR) area. This example uses the file name
evk_uicr.hex to store the UICR contents:
nrfjprog --readuicr evk_uicr.hex
Save the hex file in a convenient location.
The UICR may be restored with:
nrfjprog --program evk_uicr.hex
The example does not use the public Bluetooth address. Instead, it uses a static random Bluetooth
address that is found in the Factory Information Configuration Registers (FICR). Application note [5]
describes how to add code to read and assign the public Bluetooth address held in the UICR.
UBX-19050198 - R03 Hardware preparation Page 7 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
3 Application firmware
Navigate to the example application called ble_app_uart:
To maintain the integrity of the original example code, make a copy of the project and all its
subdirectories. Only the content of the working directory will be modified:
Compile and load the application to the BMD-340-EVAL to confirm operation before any changes are
mode. Details for testing the functionality are located at reference [15].
Open the file in nRF Connect to have a look at the memory layout of the hex file. The hex file is located
in the Output directory for the example:
The Programmer app of nRF Connect is used to view the memory layout.
Figure 1: nRF Connect main window (programmer)
UBX-19050198 - R03 Application firmware Page 8 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Add the hex file, or just drag it into the "File Memory Layout" section:
Figure 2: nRF Connect showing application only
Only the application itself is in the file. Notice the gap below. This is where the SoftDevice gets loaded.
Add or drag the SoftDevice hex file to nRF Connect. The SoftDevice is located here:
UBX-19050198 - R03 Application firmware Page 9 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Now both are shown, along with the MBR:
Figure 3: nRF Connect showing application with SoftDevice
While nRF Connect can be used to program the BMD-340, switch back to SES and load the SoftDevice
and application from there:
Figure 4: SES download application to target
UBX-19050198 - R03 Application firmware Page 10 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Figure 5: SES download progress window
LED1 should start flashing, indicating that the BMD-340 is advertising for a connection. Go ahead and
test the application according to the instructions at the InfoCenter link above.
UBX-19050198 - R03 Application firmware Page 11 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
4 Button DFU
4.1 DFU bootloader
After confirming operation, the bootloader can now be added. Nordic Semiconductor provides several
DFU examples. The Secure Bootloader example is used here. See the Nordic Semiconductor
InfoCenter for details about this secure bootloader [16].
On the local drive, navigate to the
secure_bootloader DFU directory of the examples and make a copy
Compiling the bootloader “as is” will generate an error:
#error "Debug public key not valid for production. Please see
https://github.com/NordicSemiconductor/pc-nrfutil/blob/master/README.md to generate it"
⚠ Nordic Semiconductor supplies an example keys along with test files. These are only made
available so the process of working with updating a file on a device can be tested; however, they
cannot be used for production.
4.2 Key generation
nrfutil is used to generate a custom set of keys. Navigate to the directory where the default public
key is located and change the name of the original key file,
move dfu_public_key.c dfu_public_key.c.NORDIC
dfu_public_key.c:
Generate new keys. First, start with the private key:
nrfutil keys generate dfu_private_key.pem
Generated private key and stored it in: dfu_private_key.pem
Then create the public key in C language:
nrfutil keys display --key pk --format code --out_file dfu_public_key.c
dfu_private_key.pem
/* This file was automatically generated by nrfutil on 2019-06-21 (YY-MM-DD) at 14:25:09 */
nrfutil will be used with the package generate function. The help output will show all of the
available optoins.
nrfutil pkg generate --help
For this example, the following options are used:
Option Description
Compiler output hex file
Text version string
The string “1.2.3” is converted to an integer: 10203
The default number "52" is used for a nRF52xxx device. This can
actually be any integer as well, for example to reflect the host board
version. If new code cannot be loaded onto an older version board,
then this is the value for that check.
If your application requires a specific version of the SoftDevice, this
value is used for that check.
☞ nrfutil pkg generate –help will list the available
SoftDevice versions and their corresponding firmware IDs. More
than one may be included here, comma separated.
☞ The following SoftDevice v7.x.x codes are not listed in the help
UBX-19050198 - R03 Button DFU Page 16 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Select “Connect”. Observe the "Secure DFU" service and update icon:
Figure 9: nRF Connect showing secure DFU icon
UBX-19050198 - R03 Button DFU Page 17 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Select the icon to start the DFU process. Navigate to the zip file, then select "Start DFU".
Figure 10: nRF Connect ready for DFU upgrade
A status bar will show up, first with a progress bar, then "Complete".
At this point, the Eval board will start advertising "Nordic_UART" – the example from Section 3 –
though now it includes the bootloader.
If power to the Eval board is reset or power-cycled while holding Button 4, it will re-enter bootloader
mode indicated by both LED1 and LED2 being lit.
Up to this point, the following is completed:
• Confirmed that the application code works as expected
• Generated private and public keys for the bootloader and update files
• Compiled the bootloader with the new public key
• Generated a DFU update zip file with the new private key
• Performed a DFU update and observed the application running
• Return to bootloader mode through a button press.
☞ Up to this point, the application did not require any modification. The DFU zip file is generated from
the existing application hex and generated public key files.
UBX-19050198 - R03 Button DFU Page 18 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
5 Buttonless DFU
If there are no available buttons available to enter bootloader mode through hardware, or to create a
more seamless experience for the end user, the process may be started automatically through the
"buttonless DFU" feature. This requires modification of the application to add a new service which
enables this feature.
A Bluetooth low energy service is a collection of information and behaviors to perform a particular
function or feature. In this case, the service will accept a data value (a characteristic) and write it to a
register that is persistent across a device reset. The bootloader then reads this register on startup
to determine if it should continue with the DFU function, or pass control back to the application.
Additional details can be found in this tutorial [17] from Nordic Semiconductor.
In short, this service performs what was originally enabled by pressing button 4 while resetting the
Eval board.
The ble_app_buttonless_dfu example will be added to the ble_app_uart example.
To save the work completed so far, copy the modified project a new example folder:
☞ The last item above may already be in the list. If so, do not add it again.
UBX-19050198 - R03 Buttonless DFU Page 20 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
The final values to adjust are the RAM_START address and RAM_SIZE. These are found in the Linker
options, within the Section Placement Macros line.
Figure 12: SES project RAM settings
Since the DFU service is being added alongside the Nordic UART Service (NUS), additional memory
needs allocated to flash. This is accomplished by increasing the start address of RAM and decreasing
RAM size. The new characteristics are 16-bytes each; increase the RAM_START by 0x10 and
decrease the RAM_SIZE by 0x10.
For nRF5 SDK v16.0.0, the original values are:
RAM_START=0x20002ae8
RAM_SIZE=0x3d518
The new values will be:
RAM_START=0x20002af8
RAM_SIZE=0x3d508
Select “OK” for each open dialog window to return to the main SES window.
⚠ Forgetting to change the RAM start and size is a common error that will cause a hard fault stop.
UBX-19050198 - R03 Buttonless DFU Page 21 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
5.2 sdk_config.h
Open the project sdk_config.h file.
There are several settings that need modified to enable DFU. Each line in this list are in separate
locations throughout the file. Perform a search for each of these lines and change its value as noted:
#define BLE_DFU_ENABLED 1 // was 0
#define NRF_PWR_MGMT_CONFIG_AUTO_SHUTDOWN_RETRY 1 // was 0
#define NRF_SDH_BLE_VS_UUID_COUNT 2 // was 1
#define NRF_SDH_BLE_SERVICE_CHANGED 1 // was 0
5.3 Libraries
The DFU library and driver files need added to the project.
Add a new folder in the project called nRF_DFU and add these existing files from the DFU directory of
the Bluetooth low energy components:
The project still has the same ble_app_uart functionality as the original example application, though
now it also includes everything needed to start adding the buttonless DFU function.
5.4 main.c
With the "behind the scenes" items configured, the DFU code can be added to main.c.
Several header files are needed. Add these to the other header files near the top of
UBX-19050198 - R03 Buttonless DFU Page 22 of 32
C1-Public
main.c:
Add Nordic Semiconductor DFU to SDK example - Application note
The advertising name will be changed to confirm the DFU version is running. Search for
“Nordic_UART” and change it to “UART_DFU”:
#define DEVICE_NAME "UART_DFU" /**< Name of device. Will be included in the advertising
data. Changed for DFU */
A new event handler function is added to act on the data being sent to the DFU service. Place this
code above the
/**@brief Function for handling DFU events
*
* @details This function is called when entering buttonless DFU
*
* @param[in] event Buttonless DFU event.
*/
static void ble_dfu_buttonless_evt_handler(ble_dfu_buttonless_evt_type_t event)
{
switch (event)
{
case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE:
NRF_LOG_INFO("Device is preparing to enter bootloader mode\r\n");
break;
case BLE_DFU_EVT_BOOTLOADER_ENTER:
NRF_LOG_INFO("Device will enter bootloader mode\r\n");
break;
case BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED:
NRF_LOG_ERROR("Device failed to enter bootloader mode\r\n");
break;
default:
NRF_LOG_INFO("Unknown event from ble_dfu.\r\n");
break;
}
}
“assert_nrf_callback” function:
The next added function handles the power management. The bootloader service writes a value to a
persistent register, then issues a system reset. On restart, the bootloader reads this value to
determine whether it should continue, or pass control to the application.
☞ Code can be added here to prevent the DFU from starting if something critical is going on within
the application.
Add this function code below the DFU event handler that was just added:
/**@brief Function for handling bootloader power management events
*
* @details This function is called to set a persistent register which informs the
* bootloader it should continue or pass control back to the application
*
* @param[in] event Power management event.
*/
static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
{
switch (event)
{
case NRF_PWR_MGMT_EVT_PREPARE_DFU:
NRF_LOG_INFO("Power management wants to reset to DFU mode\r\n");
// Change this code to tailor to your reset strategy.
// Returning false here means that the device is not ready
// to jump to DFU mode yet.
//
// Here is an example using a variable to delay resetting the device:
//
/* if (!im_ready_for_reset)
UBX-19050198 - R03 Buttonless DFU Page 23 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
{
return false;
}
*/
break;
default:
// Implement any of the other events available
// from the power management module:
// -NRF_PWR_MGMT_EVT_PREPARE_SYSOFF
// -NRF_PWR_MGMT_EVT_PREPARE_WAKEUP
// -NRF_PWR_MGMT_EVT_PREPARE_RESET
return true;
}
NRF_LOG_INFO("Power management allowed to reset to DFU mode\r\n");
return true;
}
NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0); // persistent register for \
determining DFU status \
on startup
The DFU service is initialized by adding it to the services_init() function, in bold, italic below:
// BEGIN Block Added for DFU
// ONLY ADD THIS BLOCK TO THE EXISTING FUNCTION
// Initialize the DFU service
ble_dfu_buttonless_init_t dfus_init =
{
.evt_handler = ble_dfu_buttonless_evt_handler
};
err_code = ble_dfu_buttonless_init(&dfus_init);
APP_ERROR_CHECK(err_code);
// END Block Added for DFU
}
Check that the application compiles without errors.
☞ Do not load the application to the BMD-340-EVAL at this time. A different process will be used to
load it to at a later step.
5.5 Hex file generation
Three of the four required components of the project are completed:
• Bootloader
• Application with buttonless DFU code included
• SoftDevice
UBX-19050198 - R03 Buttonless DFU Page 24 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
The last crucial part – bootloader settings – needs generated. The bootloader settings values tell the
bootloader about the application:
• Is a valid application present?
• What is the start address of the application?
• What are the version numbers of the bootloader, SoftDevice, and application?
This file is generated by nrfutil with the new application file as input. The output is a new hex file which
is then combined with the bootloader, SoftDevice and application hex files. A single hex file is then
generated from the four files and used to program a "blank-part" with everything in one pass over the
SWD port.
To avoid long file paths when using the nrfutil command, copy the hex files generated up to this point
to a common directory:
md c:\u-blox\hexfiles
Copy the respective hex files to this new location:
This final hex file – bl_set_s140_app.hex – with all the parts included is what is typically used on a
production line, or for pre-programming the module ahead of assembly into the end-product.
☞ Other details may be added to the hex file, including the public Bluetooth address noted in section
2.1 above.
UBX-19050198 - R03 Buttonless DFU Page 26 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
6 Test DFU
6.1 Program and test application
Everything is now in place to test the new application.
Erase the BMD-340 to ensure nothing is left-over from previous work:
nrfjprog –f nrf52 --recover
Recovering device. This operation might take 30s.
Erasing user code and UICR flash areas.
The final step is to load the hex file to the BMD-340:
nrfjprog --program bl_set_s140_app.hex
Parsing hex file.
Reading flash area to program to guarantee it is erased.
Checking that the area to write is not protected.
Programming device.
Power-cycle the BMD-340-EVAL. The application will start advertising and blink LED1 as before.
Connect to it with nRF Connect:
Figure 14: nRF Connect with application showing secure DFU icon
The bootloader icon is shown as with the button DFU example. The new name, UART_DFU, and the
Nordic UART Service, UART over BLE, are also present.
UBX-19050198 - R03 Test DFU Page 27 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
6.2 Prepare and test update
Since the application is already compiled to a hex file, it can be used as the source application file. The
application version is stepped to inform the bootloader that it is a new, valid update is available.
If the new file is selected with nRF Connect, an error will be generated since a newer version is present
on the BMD-340.
UBX-19050198 - R03 Test DFU Page 28 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Figure 16: nRF Connect showing error when attempting an "old" update
Version checking is performed for all three components: bootloader, SoftDevice, and application.
There are many more aspects of the Nordic DFU service, such as activating additional transports
(UART, USB, Bluetooth Mesh, Zigbee, and Thread), additional security with bond forwarding, and so
on. For further information, see reference [18].
UBX-19050198 - R03 Test DFU Page 29 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Appendix
A Glossary
Abbreviation Definition
ASCII American Standard Code for Information Interchange
ARM Arm (Advanced RISC Machines) Holdings
CPU Central Processing Unit
DFU Device Firmware Update
DK Development Kit (see EVK)
EVK EValuation Kit
FICR Factory Information Control Registers
GCC GNU Compiler Collection
GNU Recursive acronym “GNU Not Unix”
IDE Integrated Development Environment
LED Light Emitting Diode
MAC address Media Access Control address: a unique identifier assigned to a network interface controller
NUS Nordic UART Service
RAM Random Access Memory
SDK Software Development Kit
SES SEGGER Embedded Studio
SoftDevice Bluetooth low energy stack provided by Nordic Semiconductor
TLA Three Letter Acronym
UART Universal Asynchronous Receiver Transmitter
UICR User Information Control Registers
USB Universal Serial Bus
WSL Windows Subsystem for Linux
Table 2: Explanation of the abbreviations and terms used
UBX-19050198 - R03 Appendix Page 30 of 32
C1-Public
Add Nordic Semiconductor DFU to SDK example - Application note
Related documents
[1] ANNA-B112 system integration manual, UBX-18009821
[2] NINA-B1 system integration manual, UBX-15026175
[3] NINA-B3 system integration manual, UBX-17056748
[4] BMD-345 data sheet, UBX-19039908
[5] u-blox package information guide, UBX-14001652
[6] Using the public IEEE address from UICR application note, UBX-19050198
[7] Nordic Semiconductor nRF5 SDK
[8] Nordic Semiconductor nRF Connect for Desktop
[9] Nordic Semiconductor nRF Command Line Tools
[10] Nordic Semiconductor nRF Util GitHub repository
[11] Python scripting language download
[12] Micro_ecc encryption libraries backend instructions
[13] Git version control download
[14] GNU Arm Embedded Toolchain (GCC for Arm) download
[15] GNU make download
[16] UART example test instructions
[17] Nordic Semiconductor bootloader details
[18] Nordic Semiconductor Bluetooth services tutorial
[19] Nordic Semiconductor Bootloader and DFU modules
☞ For product change notifications and regular updates of u-blox documentation, register on our
website, www.u-blox.com.
Revision history
Revision Date Name Comments
R01 16-Dec-2019 brec Initial release
R02 02-May-2020 brec Added reference to UBX-19050198, corrected minor typographical errors