BLE DFU with Zephyr OS

Hello,

I'm tring to get OAD DFU working on an nrf52840 using Zephyr OS. Relevant information:

  • Host OS: Ubuntu 22.04, kernel 6.5.0
  • Zephyr SDK Version: 0.16.3
  • MCUBoot Version: 2.0.0

The problem is when I build my project, which I modified from the peripheral_hr project, I get the following build errors:

$ west build -p -b nrf52840dk_nrf52840 dfu-oad/ -d build-dfu-oad -- -DCONFIG_MCUBOOT_SIGNATURE_KEY_FILE=\"bootloader/mcuboot/root-rsa-2048.pem\"               [56/173]-- west build: making build dir /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad pristine
-- west build: generating a build system                                                                                                                                                                                                    
Loading Zephyr default modules (Zephyr base).                                                                                                                                                                                               
-- Application: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/dfu-oad
-- CMake version: 3.27.4                                   
-- Found Python3: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/.venv/bin/python3 (found suitable version "3.10.12", minimum required is "3.8") found components: Interpreter
-- Cache files will be written to: /home/thomasokeeffe/.cache/zephyr                                                  
-- Zephyr version: 3.5.99 (/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr)      
-- Found west (found suitable version "1.2.0", minimum required is "0.14.0")                                                                                                                                                                
-- Board: nrf52840dk_nrf52840                                                                                         
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK                                                      
-- Found host-tools: zephyr 0.16.3 (/opt/zephyr-sdk-0.16.3)                                                           
-- Found toolchain: zephyr 0.16.3 (/opt/zephyr-sdk-0.16.3)                                                            
-- Found Dtc: /opt/zephyr-sdk-0.16.3/sysroots/x86_64-pokysdk-linux/usr/bin/dtc (found suitable version "1.6.0", minimum required is "1.4.6")
-- Found BOARD.dts: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840.dts
-- Generated zephyr.dts: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/zephyr.dts
-- Generated devicetree_generated.h: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/include/generated/devicetree_generated.h
-- Including generated dts.cmake file: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/dts.cmake
Parsing /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/Kconfig                  
Loaded configuration '/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840_defconfig'
Merged configuration '/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/dfu-oad/prj.conf'
Merged configuration '/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/misc/generated/extra_kconfig_options.conf'
Configuration saved to '/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/.config'
Kconfig header saved to '/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/include/generated/autoconf.h'
-- Found GnuLd: /opt/zephyr-sdk-0.16.3/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd (found version "2.38")
-- The C compiler identification is GNU 12.2.0                                                                        
-- The CXX compiler identification is GNU 12.2.0                                                                      
-- The ASM compiler identification is GNU                                                                             
-- Found assembler: /opt/zephyr-sdk-0.16.3/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc                                    
-- Including signing script: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/cmake/mcuboot.cmake
-- Configuring done (4.5s)                                                                                            
-- Generating done (0.1s)                                  
-- Build files have been written to: /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad
-- west build: building application                                                                                   
[1/276] Preparing syscall dependency handling

[3/276] Generating include/generated/version.h                                                                        
-- Zephyr version: 3.5.99 (/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr), build: zephyr-v3.5.0-1183-gec202d852f8d                                                                                   
[22/276] Building C object CMakeFiles/app.dir/src/main.c.obj                                                                
FAILED: CMakeFiles/app.dir/src/main.c.obj                                                                             
/opt/zephyr-sdk-0.16.3/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -DKERNEL -DNRF52840_XXAA -DPICOLIBC_INTEGER_PRINTF_SCANF -D_FORTIFY_SOURCE=1 -D_POSIX_C_SOURCE=200809 -D__LINUX_ERRNO_EXTENSIONS__ -D__PROGRAM_START -D__ZEPHYR__=1 -I/home/t
homasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/samples/bluetooth -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/include -I/home/thomasokeeffe/Documents/school/drexel
/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/include/generated -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/soc/arm/nordic_nrf/nrf52 -I/home/thomasokeeffe/Documents/school/drexel/rese
arch/CTS_sensor/zephyrproject/zephyr/soc/arm/nordic_nrf/common/. -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/subsys/mgmt/mcumgr/util/include -I/home/thomasokeeffe/Documents/school/drexel/resear
ch/CTS_sensor/zephyrproject/zephyr/subsys/mgmt/mcumgr/transport/include -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/subsys/bluetooth/controller/ll_sw/nordic/hal/nrf5/nrfx_glue -I/home/thomasoke
effe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/subsys/bluetooth -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/modules/hal/cmsis/CMSIS/Core/Include -I/home/thomasokeeffe/Documents/
school/drexel/research/CTS_sensor/zephyrproject/zephyr/modules/cmsis/. -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/modules/hal/nordic/nrfx -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_se
nsor/zephyrproject/modules/hal/nordic/nrfx/drivers/include -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/modules/hal/nordic/nrfx/mdk -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zep
hyrproject/zephyr/modules/hal_nordic/nrfx/. -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/modules/debug/segger/SEGGER -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/modu
les/debug/segger/Config -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/modules/crypto/tinycrypt/lib/include -I/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/modules/lib/zcb
or/include -fno-strict-aliasing -Os -imacros /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad/zephyr/include/generated/autoconf.h -fno-printf-return-value -fno-common -g -gdwarf-4 -fdiagnostics
-color=always -mcpu=cortex-m4 -mthumb -mabi=aapcs -mfp16-format=ieee -mtp=soft --sysroot=/opt/zephyr-sdk-0.16.3/arm-zephyr-eabi/arm-zephyr-eabi -imacros /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephy
r/include/zephyr/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-pic -fno-pie -fno-asynch
ronous-unwind-tables -ftls-model=local-exec -fno-reorder-functions --param=min-pagesize=0 -fno-defer-pop -fmacro-prefix-map=/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/dfu-oad=CMAKE_SOURCE_DIR -fmacro-p
refix-map=/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr=ZEPHYR_BASE -fmacro-prefix-map=/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject=WEST_TOPDIR -ffunction-sections
-fdata-sections --specs=picolibc.specs -std=c99 -MD -MT CMakeFiles/app.dir/src/main.c.obj -MF CMakeFiles/app.dir/src/main.c.obj.d -o CMakeFiles/app.dir/src/main.c.obj -c /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/ze
phyrproject/dfu-oad/src/main.c                                                                                        
In file included from /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/dfu-oad/src/main.c:28:
/home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/zephyr/include/zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h:14:10: fatal error: bootutil/image.h: No such file or directory
   14 | #include <bootutil/image.h>                                                                                   
      |          ^~~~~~~~~~~~~~~~~~                        
compilation terminated.                                                                                                                                                                                                                     
[39/276] Building C object zephyr/CMakeFiles/zephyr.dir/subsys/logging/log_core.c.obj                                 
ninja: build stopped: subcommand failed.                                                                              
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /home/thomasokeeffe/Documents/school/drexel/research/CTS_sensor/zephyrproject/build-dfu-oad

I am able to build and flash the USB DFU example project, and update the image via DFU, so I know my setup is functioning.

I have attached the relevant code and config files. I was attempting to follow this guide: https://getwavecake.com/blog/adding-ota-dfu-to-a-nordic-zephyr-project/. I ran into some problems with the KConfig options, but I think I found a solution to that here: DFU with nrf connect 2.4. However, with those changes I get the above build error. Please let me know what I can do to fix this.

/* main.c - Application main entry point */

/*
 * Copyright (c) 2015-2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/types.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/usb/usb_device.h>

#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/services/bas.h>
#include <zephyr/bluetooth/services/hrs.h>

#include <zephyr/mgmt/mcumgr/transport/smp_bt.h>
#include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
#include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>

LOG_MODULE_REGISTER(main);

static const struct bt_data ad[] = {
	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
		      BT_UUID_16_ENCODE(BT_UUID_HRS_VAL),
		      BT_UUID_16_ENCODE(BT_UUID_BAS_VAL),
		      BT_UUID_16_ENCODE(BT_UUID_DIS_VAL))
};

static void connected(struct bt_conn *conn, uint8_t err)
{
	if (err) {
		printk("Connection failed (err 0x%02x)\n", err);
	} else {
		printk("Connected\n");
	}
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
	printk("Disconnected (reason 0x%02x)\n", reason);
}

BT_CONN_CB_DEFINE(conn_callbacks) = {
	.connected = connected,
	.disconnected = disconnected,
};

static void bt_ready(void)
{
	int err;

	printk("Bluetooth initialized\n");

	err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
	if (err) {
		printk("Advertising failed to start (err %d)\n", err);
		return;
	}

	printk("Advertising successfully started\n");
}

static void auth_cancel(struct bt_conn *conn)
{
	char addr[BT_ADDR_LE_STR_LEN];

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

	printk("Pairing cancelled: %s\n", addr);
}

static struct bt_conn_auth_cb auth_cb_display = {
	.cancel = auth_cancel,
};

static void bas_notify(void)
{
	uint8_t battery_level = bt_bas_get_battery_level();

	battery_level--;

	if (!battery_level) {
		battery_level = 100U;
	}

	bt_bas_set_battery_level(battery_level);
}

static void hrs_notify(void)
{
	static uint8_t heartrate = 90U;

	/* Heartrate measurements simulation */
	heartrate++;
	if (heartrate == 160U) {
		heartrate = 90U;
	}

	bt_hrs_notify(heartrate);
}

int main(void)
{
	int err;

	err = usb_enable(NULL);
	if (err != 0) {
		LOG_ERR("Failed to enable USB");
		return 0;
	}

	LOG_INF("This device supports USB DFU class.\n");

    os_mgmt_register_group();
    img_mgmt_register_group();
    stat_mgmt_register_group();

	err = bt_enable(NULL);
	if (err) {
		printk("Bluetooth init failed (err %d)\n", err);
		return 0;
	}

	bt_ready();

	bt_conn_auth_cb_register(&auth_cb_display);

    err = smp_bt_register();

    if (err) {
        printk("SMP BT register failed (err: %d)", err);
    }

	/* Implement notification. At the moment there is no suitable way
	 * of starting delayed work so we do it here
	 */
	while (1) {
		k_sleep(K_SECONDS(1));

		/* Heartrate measurements simulation */
		hrs_notify();

		/* Battery level simulation */
		bas_notify();
	}
	return 0;
}

4670.prj.conf

  • Thank you for the updated code. I can now see the board advertising in the nRF Connect app on my android phone. However, when I click the DFU button and select the dfu application zip file that I generated from the blinky example, I see the following in the app log:

    nRF Connect, 2023-12-01
    Zephyr Heartrate Sensor (CF:CD:B0:4A:9E:BF)
    V	13:51:32.784	[McuMgr] Connecting...
    D	13:51:32.786	[McuMgr] gatt = device.connectGatt(autoConnect = false, TRANSPORT_LE, LE 1M)
    D	13:51:32.810	[McuMgr] [Callback] Connection state changed with status: 0 and new state: 2 (CONNECTED)
    I	13:51:32.812	[McuMgr] Connected to CF:CD:B0:4A:9E:BF
    D	13:51:32.814	[McuMgr] wait(300)
    I	13:51:32.815	[McuMgr] MTU changed to: 498
    V	13:51:33.117	[McuMgr] Discovering services...
    D	13:51:33.124	[McuMgr] gatt.discoverServices()
    I	13:51:33.137	[McuMgr] Services discovered
    V	13:51:33.142	[McuMgr] Primary service found
    D	13:51:33.147	[McuMgr] gatt.setCharacteristicNotification(da2e7828-fbce-4e01-ae9e-261174997c48, true)
    V	13:51:33.153	[McuMgr] Enabling notifications for da2e7828-fbce-4e01-ae9e-261174997c48
    D	13:51:33.158	[McuMgr] gatt.writeDescriptor(00002902-0000-1000-8000-00805f9b34fb, value=0x01-00)
    I	13:51:33.254	[McuMgr] Data written to descr. 00002902-0000-1000-8000-00805f9b34fb
    V	13:51:33.264	[McuMgr] Waiting for value change...
    V	13:51:33.268	[McuMgr] Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    D	13:51:33.270	[McuMgr] gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48, value=0x000000010000FF06A0, WRITE COMMAND)
    I	13:51:33.277	[McuMgr] Data written to da2e7828-fbce-4e01-ae9e-261174997c48
    I	13:51:33.348	[McuMgr] Notification received from da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 01-00-00-06-00-00-FF-06-BF-62-72-63-08-FF
    I	13:51:33.355	[McuMgr] Wait for value changed complete
    A	13:51:33.361	[McuMgr] Received Header (Op: 1, Flags: 0, Len: 6, Group: 0, Seq: 255, Command: 6) CBOR {"rc":8}
    W	13:51:33.365	[McuMgr] Error: NOT_SUPPORTED (8)
    A	13:51:33.372	[McuMgr] Sending (10 bytes) Header (Op: 0, Flags: 0, Len: 2, Group: 1, Seq: 0, Command: 0) CBOR {}
    V	13:51:33.375	[McuMgr] Writing characteristic da2e7828-fbce-4e01-ae9e-261174997c48 (WRITE COMMAND)
    D	13:51:33.378	[McuMgr] gatt.writeCharacteristic(da2e7828-fbce-4e01-ae9e-261174997c48, value=0x0000000200010000BFFF, WRITE COMMAND)
    I	13:51:33.387	[McuMgr] Data written to da2e7828-fbce-4e01-ae9e-261174997c48
    I	13:51:33.480	[McuMgr] Notification received from da2e7828-fbce-4e01-ae9e-261174997c48, value: (0x) 01-00-00-06-00-01-00-00-BF-62-72-63-08-FF
    A	13:51:33.491	[McuMgr] Received Header (Op: 1, Flags: 0, Len: 6, Group: 1, Seq: 0, Command: 0) CBOR {"rc":8}
    W	13:51:33.496	[McuMgr] Error: NOT_SUPPORTED (8)
    V	13:51:33.509	[McuMgr] Disconnecting...
    D	13:51:33.513	[McuMgr] gatt.disconnect()
    D	13:51:33.519	[McuMgr] [Callback] Connection state changed with status: 0 and new state: 0 (DISCONNECTED)
    I	13:51:33.521	[McuMgr] Disconnected
    D	13:51:33.522	[McuMgr] gatt.close()
    

  • Hi, 

    Try this peripheral_hr_DFU.7z, and use the sample pm_static.yml to update the image. See Static partition requirement for DFU.

    “Beware that this code/configuration is not fully tested or qualified and should be considered provided “as-is”. Please test it with your application and let me know if you find any issues.”

    -Amanda H.

Related