Skip to content

Commit

Permalink
Add implementation for DFU via ThingSet
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjaeger committed May 31, 2024
1 parent 6e4c2ea commit e3adf90
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
west build -p -b esp32c3_devkitm samples/counter -- -DEXTRA_CONF_FILE=ble.conf -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y
west build -p -b esp32c3_devkitm samples/counter -- -DEXTRA_CONF_FILE=wifi_websocket.conf -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y
west build -p -b native_posix samples/counter -- -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y
west build -p -b nrf52840dk_nrf52840 samples/counter -- -DOVERLAY_CONFIG=dfu.conf
west build -p -b nucleo_l073rz samples/counter -- -DEXTRA_CONF_FILE=serial.conf -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y
west build -p -b nucleo_l073rz samples/counter -- -DEXTRA_CONF_FILE=storage_eeprom.conf -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y
west build -p -b native_posix samples/counter -- -DEXTRA_CONF_FILE=auth.conf -DCONFIG_COMPILER_WARNINGS_AS_ERRORS=y
Expand Down
1 change: 1 addition & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if THINGSET_SDK
rsource "src/Kconfig.auth"
rsource "src/Kconfig.ble"
rsource "src/Kconfig.can"
rsource "src/Kconfig.dfu"
rsource "src/Kconfig.log_backend"
rsource "src/Kconfig.lorawan"
rsource "src/Kconfig.serial"
Expand Down
6 changes: 5 additions & 1 deletion include/thingset/sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ extern "C" {
#define TS_ID_NET_CAN_NODE_ADDR 0x28C

/* Device Firmware Upgrade group items */
#define TS_ID_DFU 0x2D
#define TS_ID_DFU 0x2D
#define TS_ID_DFU_INIT 0x2D0
#define TS_ID_DFU_WRITE 0x2D1
#define TS_ID_DFU_DATA 0x2D2
#define TS_ID_DFU_BOOT 0x2D3

/* Log group items */
#define TS_ID_LOG 0x2E
Expand Down
23 changes: 23 additions & 0 deletions samples/counter/dfu.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) The ThingSet Project Contributors
# SPDX-License-Identifier: Apache-2.0

CONFIG_THINGSET_DFU=y

# Flash driver to store firmware image
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_FLASH_PAGE_LAYOUT=y
#CONFIG_FLASH_LOG_LEVEL_WRN=y
CONFIG_IMG_MANAGER=y
#CONFIG_IMG_BLOCK_BUF_SIZE=512
#CONFIG_IMG_ERASE_PROGRESSIVELY=y

#CONFIG_MPU_ALLOW_FLASH_WRITE=y

CONFIG_STREAM_FLASH=y

# Application-side MCUboot configuration
CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_MCUBOOT_IMG_MANAGER=y
CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="bootloader/mcuboot/root-ec-p256.pem"
CONFIG_MCUBOOT_GENERATE_CONFIRMED_IMAGE=y
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ zephyr_library_sources_ifdef(CONFIG_THINGSET_SDK packetizer.c)
zephyr_library_sources_ifdef(CONFIG_THINGSET_AUTH auth.c)
zephyr_library_sources_ifdef(CONFIG_THINGSET_BLE ble.c)
zephyr_library_sources_ifdef(CONFIG_THINGSET_CAN can.c)
zephyr_library_sources_ifdef(CONFIG_THINGSET_DFU dfu.c)
zephyr_library_sources_ifdef(CONFIG_THINGSET_LOG_BACKEND log_backend.c)
zephyr_library_sources_ifdef(CONFIG_THINGSET_LORAWAN lorawan.c)
zephyr_library_sources_ifdef(CONFIG_THINGSET_SERIAL serial.c)
Expand Down
18 changes: 18 additions & 0 deletions src/Kconfig.dfu
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) The ThingSet Project Contributors
# SPDX-License-Identifier: Apache-2.0

menuconfig THINGSET_DFU
bool "ThingSet device firmware upgrade"
depends on FLASH
select REBOOT

if THINGSET_DFU

config THINGSET_DFU_CHUNK_SIZE
int "Firmware chunk size"
range 64 4096
default 256
help
Maximum chunk size that can be transmitted in one ThingSet message.

endif # THINGSET_DFU
105 changes: 105 additions & 0 deletions src/dfu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) The ThingSet Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/device.h>
#include <zephyr/dfu/flash_img.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/sys/reboot.h>

#include <thingset.h>
#include <thingset/sdk.h>

LOG_MODULE_REGISTER(thingset_dfu, CONFIG_LOG_DEFAULT_LEVEL);

#define TARGET_IMAGE_AREA FIXED_PARTITION_ID(slot1_partition)

static uint8_t bytes_buf[CONFIG_THINGSET_DFU_CHUNK_SIZE];
static THINGSET_DEFINE_BYTES(bytes_item, bytes_buf, 0);

static int32_t thingset_dfu_init();
static int32_t thingset_dfu_write();
static int32_t thingset_dfu_boot();

THINGSET_ADD_GROUP(TS_ID_ROOT, TS_ID_DFU, "DFU", THINGSET_NO_CALLBACK);
THINGSET_ADD_FN_INT32(TS_ID_DFU, TS_ID_DFU_INIT, "xInit", &thingset_dfu_init, THINGSET_ANY_RW);
THINGSET_ADD_FN_INT32(TS_ID_DFU, TS_ID_DFU_WRITE, "xWrite", &thingset_dfu_write, THINGSET_ANY_RW);
THINGSET_ADD_ITEM_BYTES(TS_ID_DFU_WRITE, TS_ID_DFU_DATA, "bData", &bytes_item, THINGSET_ANY_RW, 0);
THINGSET_ADD_FN_INT32(TS_ID_DFU, TS_ID_DFU_BOOT, "xBoot", &thingset_dfu_boot, THINGSET_ANY_RW);

static bool dfu_initialized = false;

static struct flash_img_context flash_img_ctx;

static int32_t thingset_dfu_init()
{
int err;

if (!IS_ENABLED(CONFIG_IMG_ERASE_PROGRESSIVELY)) {
LOG_DBG("Erasing flash area");

err = boot_erase_img_bank(TARGET_IMAGE_AREA);
if (err) {
LOG_ERR("Failed to erase image bank (err %d)", err);
return err;
}
}

err = flash_img_init_id(&flash_img_ctx, TARGET_IMAGE_AREA);
if (err) {
LOG_ERR("Failed to initialize flash img (err %d)", err);
return err;
}

dfu_initialized = true;

return 0;
}

static int32_t thingset_dfu_write()
{
int err;

if (!dfu_initialized) {
LOG_ERR("DFU not initialized");
return -EBUSY;
}

err = flash_img_buffered_write(&flash_img_ctx, bytes_item.bytes, bytes_item.num_bytes, false);
if (err) {
LOG_ERR("Failed to write data (err %d)", err);
return err;
}
else {
size_t total_bytes = flash_img_bytes_written(&flash_img_ctx);
LOG_INF("Total bytes downloaded: 0x%.6X (%d)", total_bytes, total_bytes);
}

return 0;
}

static int32_t thingset_dfu_boot()
{
int err;
uint8_t dummy = 0;

/* make sure that flash_img buffer is flushed */
flash_img_buffered_write(&flash_img_ctx, &dummy, 0, true);

err = boot_request_upgrade(BOOT_UPGRADE_TEST);
if (err) {
LOG_ERR("Failed to request upgrade (err %d)", err);
return err;
}

LOG_INF("Program downloaded, rebooting...");

sys_reboot(SYS_REBOOT_COLD);

return 0;
}
4 changes: 3 additions & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ manifest:
- hal_st
- loramac-node
- mbedtls
- tinycrypt
- mcuboot
- picolibc
- segger
- tinycrypt
- name: thingset-node-c
remote: thingset
revision: d2a610db9a650067ce9fc3381c0b350c9246c88c
Expand Down

0 comments on commit e3adf90

Please sign in to comment.