-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation for DFU via ThingSet
- Loading branch information
1 parent
6e4c2ea
commit e3adf90
Showing
8 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters