BLE Peripheral Role · Device Firmware Upgrade · NRF52832 · NRF52840 · NRF52840 DK board

Serial UART DFU on nRF5 SDK (by using host tool instead of nRFUtil in C Code application)


Based on the documentation of BLE Secure DFU example, it supports on the BLE / ANT/Serial UART DFU.

This blog would mainly focus on the Serial (UART) DFU.  Inside the Nordic official example, it would use the nrfutil (written in Python script) as the host application for device firmware upgrade.

I would use another alternative host application (in C code, based on nRFutil 3.5 version) to show how to do the application FW upgrade only.  This example is tested at the SDK 15.2 under Window or Linux Ubuntu environment.


nrfutil is a Python package that includes the nrfutil command line utility and the nordicsemi library.


Here are the procedure how to run on the serial DFU on NRF SDK.

Step 1

Download the Nordic NRF5 15.2 SDK (

Step 2

Prepare the GCC compiler Install the GNU ARM Embedded compiler / Linker

For example,  this compiler is installed at C:\Tools\gcc-arm-none-eabi-7-2018-q2-update-win32

Modify the corresponding makefile inside the Nordic SDK (e.g. window — nRF5_SDK_15.2.0_9412b96\components\toolchain\gcc\

Step 3

Download / Compile the micro-ecc library (because secure DFU requires to use the micro-ecc 3rd party library)

Under Window:

  • Install the Chocolatey (Software Management Automation) 
  • Install the make package under chocolatey tool (run the command prompt terminal as administrator right)

  • Install the git at the terminal (recommended to use cmder terminal or download from choco install git)
  • Run the batch file (build_all.bat) under nRF5_SDK_15.2.0_9412b96\external\micro-ecc folder

Step 4

Generate the private key and public key (Make sure the version of nRFUtil is 3.5 if you are using the SDK 15.2 DFU,  nrfutil version)

You can download the pre-compiled execute file at  nRFUtil works on the Python 2.7.x currently.

@echo generate the private key
nrfutil keys generate demo_private.key

@echo generate the public_key from private key
nrfutil keys display --key pk --format code demo_private.key --out_file demo_public_key.c

Step 5

Update the public key at the bootloader project (dfu_public_key.c)

Recompile the bootloader (secure_bootloader\pca10040_uart_debug in this case) on the SES project file.

Step 6

Prepare two difference application firmware files and generate corresponding zip package for testing by using the private key.

nrfutil pkg generate --hw-version 52 --application-version 1 --application ble_app_uart_pca10040_s132_FW1.hex --sd-req 0xB7 --key-file demo_private.key

nrfutil pkg generate --hw-version 52 --application-version 1 --application ble_app_uart_pca10040_s132_FW2.hex --sd-req 0xB7 --key-file demo_private.key

The softdevice version (sd-req) can be found at nrfutil github (

In this case, S132v6.1.1 (version 0xB7) is used. You can check the softdevice version at

Step 7

Program the softdevice on the nRF52 DK board.

nrfjprog --program s132_nrf52_6.1.1_softdevice.hex --reset --sectorerase

Program the bootloader on the DK board .

Step 8

Compile the Serial DFU Host (written in C code, UartSecureDFU.exe) tool

Step 9

Run the DFU by using the UartSecureDFU.exe

All the materials are placed at

17 thoughts on “Serial UART DFU on nRF5 SDK (by using host tool instead of nRFUtil in C Code application)

  1. Greate pieces. Keep posting such kind of info on your
    blog. Im really impressed by your blog.
    Hello there, You have done a great job. I will certainly digg
    it and individually suggest to my friends. I am sure they will be benefited from this site.


    1. You just need to use the make file either under windows (such as MSYS2) or linux.
      if you use the windows, you need to specify the makefile such as you need to add the make -f Makefile_win32 (makefile for window platform).


  2. Its mentioned as “application FW upgrade only”. So with C code, whether only Application can be upgraded. Any plans for SoftDevice and Bootloader up-gradation feature ?


    1. It can support softdevice and bootloader upgrade. This blog is to share the demo what I did. Please notice that it is not bug free guarantee.


  3. I used this code for nRF52840 Softdevice,Bootloader,App from raspberry pi over UART and it is working fine. but when either Softdevice/Bootloader merged firmware or App firmware update is running, and it is stopped , it is unable to resume firmware update process.


    1. The UART DFU is only tested on the SDK 15.2 (with nRFUtil 3.5). You may need to check at your configuration.
      The code is just to share the proof of concept. It doesn’t guarantee to work on all the conditions. Please notice about this.


  4. I have a query on Secure Serial DFU, whenever there is a change in Flash and Ram address.
    In original example code below are the macros.


    After adding our code, we need to modify macro to


    Because of this will it cause any issue if we do Secure Serial DFU on original binary with latest package with changed FLASH_SIZE and RAM_SIZE macros.


  5. This application is exactly what I was looking for but I am trying to get it to work over serial USB (not UART) to the nrf52840 dev kit. It seems like it ‘should’ work but am running into troubles with the UART_SLIP_SIZE_MAX of 128 being violated and causing failure in uart_slip_send():

    if (nSize > UART_SLIP_SIZE_MAX)
    logger_error(“Cannot encode SLIP!”);

    err_code = 1;

    I increased UART_SLIP_SIZE_MAX and things progress further (the init packet gets sent) but sending the image fails. Do you know if this should work over serial USB and if not what changes might be necessary?


      1. Jimmy,

        I changed UART_SLIP_SIZE_MAX to 64 like you suggested. Still not working. Here’s a trace from running the app (note that I added extra logging of the length of the message sent over slip shown as uart_slip_send: nSize = NN). You can see that it fails when trying to send a message of 1025 bytes since UART_SLIP_SIZE_MAX is set to 64).

        Sending Application image.
        uart_slip_send: nSize = 2
        Set Packet Receipt Notification 0
        uart_slip_send: nSize = 3
        uart_slip_send: nSize = 1
        Sending init packet…
        SEND INIT PKT 1
        Selecting Object: type:1
        uart_slip_send: nSize = 2
        Object selected: max_size:512 offset:141 crc:0x55771FB9
        SEND INIT PKT 2
        uart_slip_send: nSize = 1
        Sending firmware file…
        Selecting Object: type:2
        uart_slip_send: nSize = 2
        Object selected: max_size:4096 offset:0 crc:0x00000000
        uart_slip_send: nSize = 6
        Streaming Data: len:4096 offset:0 crc:0x00000000
        uart_slip_send: nSize = 1025
        Cannot encode SLIP!


  6. Hi Jimmy, thanks for your sharing. But I find that I can only do UartSecureDFU.exe COM8 -v -v once and it succeed. Later if I want to upgrade to another FW e.g. I type UartSecureDFU.exe COM8 -v -v and it tells me “Read no data from UART!”. No matter how many times I try always the same. How can this be fixed?
    So the Host can only upgrade firmware when there’s no application code inside? And can only upgrade once? Now if I want to change to another app firmware, I need to remove the app firmware inside and then type UartSecureDFU.exe COM8 -v -v.
    Anything I did wrong? Do I misunderstood some of your instruction? Thank you in advance.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.