diff --git a/README.md b/README.md index ceb2c51eb..940cff9f6 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,25 @@ Before getting started, make sure you have a proper Zephyr development environment. Follow the official [Zephyr Getting Started Guide](https://docs.zephyrproject.org/latest/getting_started/index.html). + +### Python VENV + +To get started its recommended to initialize a virtual-environment for python. + +``` +mkdir -p my-workspace +cd my-workspace +python -m venv .venv +source .venv/bin/activate # note. activate.{fish,csh} etc for other shells! +``` + +From there you can install west and all the required python dependencies for zephyr's build system +without touching your system's python installation. + +``` +pip install west +``` + ### Initialization The first step is to initialize the workspace folder (``my-workspace``) where @@ -68,6 +87,12 @@ cd my-workspace west update ``` +Before building, ensure all required python packages are installed + +``` +west packages pip --install +``` + ### Building and running To build the application, run the following command: diff --git a/app/boards/nucleo_f413zh.overlay b/app/boards/nucleo_f413zh.overlay new file mode 100644 index 000000000..c99fc78e8 --- /dev/null +++ b/app/boards/nucleo_f413zh.overlay @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Nordic Semiconductor ASA + * SPDX-License-Identifier: Apache-2.0 + */ + +/* This devicetree overlay file will be automatically picked by the Zephyr + * build system when building the sample for the nucleo_f413zh board. It shows + * how the example-application can be built on sample boards already provided + * by Zephyr. + */ + +/ { + example_sensor: example-sensor { + compatible = "zephyr,example-sensor"; + input-gpios = <&gpioc 13 (GPIO_ACTIVE_HIGH)>; + }; + + blink_led: blink-led { + compatible = "blink-gpio-led"; + led-gpios = <&gpiob 0 GPIO_ACTIVE_HIGH>; + blink-period-ms = <1000>; + }; +}; + +&gpioc { + status = "okay"; +}; + +&gpiob { + status = "okay"; +}; + + diff --git a/sysbuild-example/CMakeLists.txt b/sysbuild-example/CMakeLists.txt new file mode 100644 index 000000000..0166ff26f --- /dev/null +++ b/sysbuild-example/CMakeLists.txt @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: Apache-2.0 +# Author: James Walmsley + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +test_sysbuild(REQUIRED) + +project(sysbuild-example) + +target_sources(app PRIVATE main_image/src/main.c) + diff --git a/sysbuild-example/README.md b/sysbuild-example/README.md new file mode 100644 index 000000000..a5d5cbe94 --- /dev/null +++ b/sysbuild-example/README.md @@ -0,0 +1,15 @@ +# Example Sysbuild Project + +The aim of this folder is to demonstrate a typical sysbuild project from the ground-up. + +## Build + +``` +cd my-workspace/example-application +west build --sysbuild sysbuild-example \ + -DEXTRA_DTC_OVERLAY_FILE=(pwd)/sysbuild-example/overlays/nucleo_f413zh_partitions.overlay \ + -Dmcuboot_EXTRA_DTC_OVERLAY_FILE=(pwd)/sysbuild-example/overlays/nucleo_f413zh_partitions.overlay \ + -Dmfg_image_EXTRA_DTC_OVERLAY_FILE=(pwd)/sysbuild-example/overlays/nucleo_f413zh_partitions.overlay \ + -Ddfu_app_EXTRA_DTC_OVERLAY_FILE=(pwd)/sysbuild-example/overlays/nucleo_f413zh_partitions.overlay +``` + diff --git a/sysbuild-example/boards/nucleo_f413zh.overlay b/sysbuild-example/boards/nucleo_f413zh.overlay new file mode 100644 index 000000000..0e2a58b0f --- /dev/null +++ b/sysbuild-example/boards/nucleo_f413zh.overlay @@ -0,0 +1,7 @@ +/ { + chosen { + zephyr,code-partition = &app_partition; + }; +}; + + diff --git a/sysbuild-example/dfu_app/CMakeLists.txt b/sysbuild-example/dfu_app/CMakeLists.txt new file mode 100644 index 000000000..a72d55b3c --- /dev/null +++ b/sysbuild-example/dfu_app/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2025 James Walmsley +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(dfu_app) +target_sources(app PRIVATE src/main.c) + diff --git a/sysbuild-example/dfu_app/boards/nucleo_f413zh.overlay b/sysbuild-example/dfu_app/boards/nucleo_f413zh.overlay new file mode 100644 index 000000000..0e2a58b0f --- /dev/null +++ b/sysbuild-example/dfu_app/boards/nucleo_f413zh.overlay @@ -0,0 +1,7 @@ +/ { + chosen { + zephyr,code-partition = &app_partition; + }; +}; + + diff --git a/sysbuild-example/dfu_app/prj.conf b/sysbuild-example/dfu_app/prj.conf new file mode 100644 index 000000000..3c0f8a655 --- /dev/null +++ b/sysbuild-example/dfu_app/prj.conf @@ -0,0 +1,9 @@ +CONFIG_BOOTLOADER_MCUBOOT=y +CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="bootloader/mcuboot/root-rsa-2048.pem" +CONFIG_FLASH=y +CONFIG_IMG_MANAGER=y +CONFIG_STREAM_FLASH=y +CONFIG_USB_DFU_CLASS=y +CONFIG_USB_DEVICE_STACK=y +CONFIG_FLASH_MAP=y + diff --git a/sysbuild-example/dfu_app/src/main.c b/sysbuild-example/dfu_app/src/main.c new file mode 100644 index 000000000..f31cb1630 --- /dev/null +++ b/sysbuild-example/dfu_app/src/main.c @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2025 James Walmsley + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +int main(void) +{ + printk("Hello world from 2 %s\n", CONFIG_BOARD_TARGET); + + return 0; +} + + diff --git a/sysbuild-example/main_image/src/main.c b/sysbuild-example/main_image/src/main.c new file mode 100644 index 000000000..5196fe3a1 --- /dev/null +++ b/sysbuild-example/main_image/src/main.c @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2025 James Walmsley + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +int main(void) +{ + printk("Main image on: %s\n", CONFIG_BOARD_TARGET); + return 0; +} + + diff --git a/sysbuild-example/mfg_image/CMakeLists.txt b/sysbuild-example/mfg_image/CMakeLists.txt new file mode 100644 index 000000000..e402fcda0 --- /dev/null +++ b/sysbuild-example/mfg_image/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2025 James Walmsley +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZPEHYR_BASE}) + +project(mfg_image) +target_sources(app PRIVATE src/main.c) + diff --git a/sysbuild-example/mfg_image/boards/nucleo_f413zh.overlay b/sysbuild-example/mfg_image/boards/nucleo_f413zh.overlay new file mode 100644 index 000000000..0e2a58b0f --- /dev/null +++ b/sysbuild-example/mfg_image/boards/nucleo_f413zh.overlay @@ -0,0 +1,7 @@ +/ { + chosen { + zephyr,code-partition = &app_partition; + }; +}; + + diff --git a/sysbuild-example/mfg_image/prj.conf b/sysbuild-example/mfg_image/prj.conf new file mode 100644 index 000000000..f1c6855d2 --- /dev/null +++ b/sysbuild-example/mfg_image/prj.conf @@ -0,0 +1,2 @@ +CONFIG_BOOTLOADER_MCUBOOT=y +CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="bootloader/mcuboot/root-rsa-2048.pem" diff --git a/sysbuild-example/mfg_image/src/main.c b/sysbuild-example/mfg_image/src/main.c new file mode 100644 index 000000000..368221905 --- /dev/null +++ b/sysbuild-example/mfg_image/src/main.c @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2025 James Walmsley + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +int main(void) +{ + printk("Manufacturing image on: %s\n", CONFIG_BOARD_TARGET); + + return 0; +} + diff --git a/sysbuild-example/overlays/nucleo_f413zh_partitions.overlay b/sysbuild-example/overlays/nucleo_f413zh_partitions.overlay new file mode 100644 index 000000000..2daaa84ed --- /dev/null +++ b/sysbuild-example/overlays/nucleo_f413zh_partitions.overlay @@ -0,0 +1,42 @@ +/delete-node/ &slot0_partition; +/delete-node/ &slot1_partition; +/delete-node/ &scratch_partition; + +&flash0 { + /* + * Setting the erase/write block sizes for stm32f4xx + * Its not defined in the main dts because the devices + * have varying sector sizes. + * + * However we can use 128K as a working size across + * the mcuboot slots. + */ + erase-block-size = ; + write-block-size = <1>; + + partitions { + /* + * + */ + app_partition: app_partition@a { + label = "app-max-size"; + reg = <0x00020000 DT_SIZE_K(512)>; + }; + + /* + * This paritition layout is for SWAP_WITH_MOVE, + * Where the primary slot must be 1 sector bigger than the secondary slot! + */ + slot0_partition: partition@20000 { + label = "image-0"; + reg = <0x00020000 DT_SIZE_K(512 + 128)>; + + }; + + slot1_partition: partition@C0000 { + label = "image-1"; + reg = <0x000C0000 DT_SIZE_K(512)>; + }; + }; +}; + diff --git a/sysbuild-example/prj.conf b/sysbuild-example/prj.conf new file mode 100644 index 000000000..05c7e3c1d --- /dev/null +++ b/sysbuild-example/prj.conf @@ -0,0 +1,7 @@ +CONFIG_BOOTLOADER_MCUBOOT=y +CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="bootloader/mcuboot/root-rsa-2048.pem" +CONFIG_FLASH=y +CONFIG_IMG_MANAGER=y +CONFIG_STREAM_FLASH=y +CONFIG_FLASH_MAP=y + diff --git a/sysbuild-example/sysbuild.cmake b/sysbuild-example/sysbuild.cmake new file mode 100644 index 000000000..c587fd5ab --- /dev/null +++ b/sysbuild-example/sysbuild.cmake @@ -0,0 +1,16 @@ +# Copyright (c) 2025 James Walmsley +# SPDX-License-Identifier: Apache-2.0 + +ExternalZephyrProject_Add( + APPLICATION mfg_image + SOURCE_DIR ${APP_DIR}/mfg_image +) + +ExternalZephyrProject_Add( + APPLICATION dfu_app + SOURCE_DIR ${APP_DIR}/dfu_app +) + +add_dependencies(${DEFAULT_IMAGE} mfg_image) +add_dependencies(${DEFAULT_IMAGE} dfu_app) + diff --git a/sysbuild-example/sysbuild.conf b/sysbuild-example/sysbuild.conf new file mode 100644 index 000000000..eaed74ed6 --- /dev/null +++ b/sysbuild-example/sysbuild.conf @@ -0,0 +1,3 @@ +SB_CONFIG_BOOTLOADER_MCUBOOT=y +SB_CONFIG_MCUBOOT_MODE_SWAP_USING_MOVE=y + diff --git a/sysbuild-example/sysbuild/mcuboot/boards/nucleo_f413zh.overlay b/sysbuild-example/sysbuild/mcuboot/boards/nucleo_f413zh.overlay new file mode 100644 index 000000000..8994f330b --- /dev/null +++ b/sysbuild-example/sysbuild/mcuboot/boards/nucleo_f413zh.overlay @@ -0,0 +1,7 @@ +/ { + chosen { + zephyr,code-partition = &boot_partition; + }; +}; + + diff --git a/sysbuild-example/sysbuild/mcuboot/prj.conf b/sysbuild-example/sysbuild/mcuboot/prj.conf new file mode 100644 index 000000000..baa008851 --- /dev/null +++ b/sysbuild-example/sysbuild/mcuboot/prj.conf @@ -0,0 +1,36 @@ +CONFIG_PM=n + +CONFIG_MAIN_STACK_SIZE=10240 + +CONFIG_BOOT_SWAP_SAVE_ENCTLV=n +CONFIG_BOOT_ENCRYPT_IMAGE=n + +CONFIG_BOOT_UPGRADE_ONLY=n +CONFIG_BOOT_BOOTSTRAP=y + +### mbedTLS has its own heap +# CONFIG_HEAP_MEM_POOL_SIZE is not set + +### We never want Zephyr's copy of tinycrypt. If tinycrypt is needed, +### MCUboot has its own copy in tree. +# CONFIG_TINYCRYPT is not set +# CONFIG_TINYCRYPT_ECC_DSA is not set +# CONFIG_TINYCRYPT_SHA256 is not set + +CONFIG_FLASH=y + +### Various Zephyr boards enable features that we don't want. +# CONFIG_BT is not set +# CONFIG_BT_CTLR is not set +# CONFIG_I2C is not set + +CONFIG_LOG=y +CONFIG_LOG_MODE_MINIMAL=y # former CONFIG_MODE_MINIMAL +### Ensure Zephyr logging changes don't use more resources +CONFIG_LOG_DEFAULT_LEVEL=0 +### Use info log level by default +CONFIG_MCUBOOT_LOG_LEVEL_INF=y +### Decrease footprint by ~4 KB in comparison to CBPRINTF_COMPLETE=y +CONFIG_CBPRINTF_NANO=y +### Use the minimal C library to reduce flash usage +CONFIG_MINIMAL_LIBC=y diff --git a/west.yml b/west.yml index c3e406760..414894483 100644 --- a/west.yml +++ b/west.yml @@ -20,3 +20,5 @@ manifest: - cmsis_6 # required by the ARM port for Cortex-M - hal_nordic # required by the custom_plank board (Nordic based) - hal_stm32 # required by the nucleo_f302r8 board (STM32 based) + - mcuboot + - mbedtls