From 068de6df69b7413f35dcc23fc0b4123ccd921740 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 25 Nov 2021 20:42:43 +1000 Subject: [PATCH 1/4] cmake: introduce "snippet" concept Introduce the concept of snippet's to application configuration. A snippet is a piece of functionality that is not the default behaviour of a board, but is a common configuration that users may wish to apply. One example snippet concept is "usb" which may route the console to USB instead of a serial port. Another common snippet could be "mcuboot", which would define the flash partitions required for applications to be loaded by a bootloader. The name "snippet" was chosen mainly to avoid collisions with existing terms. Alternatives considered include: * overlay (collision with devicetree overlay concept) * config (collision with Kconfig) * part (collision with --pristine build option, flash partitions) * layout (implies memory configuration) The Oxford dictionary definition of snippet appears to match this concept well, "a small piece or brief extract". Snippets are a required devicetree `.overlay` file, an optional `.conf` file and an optional `.cmake` file. `.overlay` files and `.conf` files are appended to to the existing CMake variables `DTC_OVERLAY_FILE` and `CONF_FILE` respectively. The `.cmake` file is evaluated immediately, which allows setting configurations before Kconfig or DTS parsing runs. The initial search paths for these files are `${APPLICATION_SOURCE_DIR}/boards/${BOARD}/snippets` and `${BOARD_DIR}/snippets`, with the application directory having precedence to allow for overriding board defaults. Signed-off-by: Jordan Yates --- cmake/modules/configuration_files.cmake | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cmake/modules/configuration_files.cmake b/cmake/modules/configuration_files.cmake index df569d981d965..17339890a1bff 100644 --- a/cmake/modules/configuration_files.cmake +++ b/cmake/modules/configuration_files.cmake @@ -73,6 +73,24 @@ elseif(EXISTS ${APPLICATION_CONFIG_DIR}/prj.conf) set(CONF_FILE_INCLUDE_FRAGMENTS true) endif() +# Append any .conf files from snippets +if(DEFINED SNIPPETS) + message(STATUS "Snippets: ${SNIPPETS}") + # Convert from space-separated snippets into snippet list + string(REPLACE " " ";" SNIPPETS_RAW_LIST "${SNIPPETS}") + # Search for (optional) ${SNIPPET}.conf files + foreach(SNIPPET IN LISTS SNIPPETS_RAW_LIST) + # Application level board snippets have precedence + if(EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}/snippets/${SNIPPET}.conf) + string(APPEND CONF_FILE " ${APPLICATION_SOURCE_DIR}/boards/${BOARD}/snippets/${SNIPPET}.conf") + set(CONF_FILE_INCLUDE_FRAGMENTS true) + elseif(EXISTS ${BOARD_DIR}/snippets/${SNIPPET}.conf) + string(APPEND CONF_FILE " ${BOARD_DIR}/snippets/${SNIPPET}.conf") + set(CONF_FILE_INCLUDE_FRAGMENTS true) + endif() + endforeach() +endif() + if(CONF_FILE_INCLUDE_FRAGMENTS) zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR}/boards KCONF CONF_FILE BUILD ${CONF_FILE_BUILD_TYPE}) endif() @@ -108,5 +126,21 @@ alternate .overlay file using this parameter. These settings will override the \ settings in the board's .dts file. Multiple files may be listed, e.g. \ DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"") +if(DEFINED SNIPPETS) + # Search for (required) ${SNIPPET}.overlay and (optional) ${SNIPPET}.cmake files + foreach(SNIPPET IN LISTS SNIPPETS_RAW_LIST) + # Application level board snippets have precedence + if(EXISTS ${APPLICATION_SOURCE_DIR}/boards/${BOARD}/snippets/${SNIPPET}.overlay) + string(APPEND DTC_OVERLAY_FILE " ${APPLICATION_SOURCE_DIR}/boards/${BOARD}/snippets/${SNIPPET}.overlay") + include(${APPLICATION_SOURCE_DIR}/boards/${BOARD}/snippets/${SNIPPET}.cmake OPTIONAL NO_POLICY_SCOPE) + elseif(EXISTS ${BOARD_DIR}/snippets/${SNIPPET}.overlay) + string(APPEND DTC_OVERLAY_FILE " ${BOARD_DIR}/snippets/${SNIPPET}.overlay") + include(${BOARD_DIR}/snippets/${SNIPPET}.cmake OPTIONAL NO_POLICY_SCOPE) + else() + message(FATAL_ERROR "Snippet ${SNIPPET} does not exist in ${APPLICATION_SOURCE_DIR}/boards/${BOARD}/snippets or ${BOARD_DIR}/snippets") + endif() + endforeach() +endif() + # The DTC_OVERLAY_FILE variable is now set to its final value. zephyr_boilerplate_watch(DTC_OVERLAY_FILE) From f022d60ad8b552fa76ac5353fee008abce68192b Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 25 Nov 2021 20:54:44 +1000 Subject: [PATCH 2/4] west_commands: build: remove deprecated `-s` opt Remove the `-s` option which was deprecated 3 years ago to make namespace for a snippet argument. Signed-off-by: Jordan Yates --- doc/develop/west/sign.rst | 4 ++-- scripts/west_commands/build.py | 9 --------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/doc/develop/west/sign.rst b/doc/develop/west/sign.rst index 93d7406a5858d..6a55417402fcd 100644 --- a/doc/develop/west/sign.rst +++ b/doc/develop/west/sign.rst @@ -26,8 +26,8 @@ from the :file:`zephyrproject` workspace you created in the .. code-block:: console - west build -b YOUR_BOARD -s bootloader/mcuboot/boot/zephyr -d build-mcuboot - west build -b YOUR_BOARD -s zephyr/samples/hello_world -d build-hello-signed -- \ + west build -b YOUR_BOARD bootloader/mcuboot/boot/zephyr -d build-mcuboot + west build -b YOUR_BOARD zephyr/samples/hello_world -d build-hello-signed -- \ -DCONFIG_BOOTLOADER_MCUBOOT=y \ -DCONFIG_MCUBOOT_SIGNATURE_KEY_FILE=\"bootloader/mcuboot/root-rsa-2048.pem\" diff --git a/scripts/west_commands/build.py b/scripts/west_commands/build.py index 3e72bc9dea27d..7c159686114ca 100644 --- a/scripts/west_commands/build.py +++ b/scripts/west_commands/build.py @@ -100,8 +100,6 @@ def do_add_parser(self, parser_adder): parser.add_argument('-b', '--board', help='board to build for with optional board revision') - # Hidden option for backwards compatibility - parser.add_argument('-s', '--source-dir', help=argparse.SUPPRESS) parser.add_argument('-d', '--build-dir', help='build directory to create or use') self.add_force_arg(parser) @@ -137,17 +135,10 @@ def do_run(self, args, remainder): self.config_board = config_get('board', None) log.dbg('args: {} remainder: {}'.format(args, remainder), level=log.VERBOSE_EXTREME) - # Store legacy -s option locally - source_dir = self.args.source_dir self._parse_remainder(remainder) # Parse testcase.yaml or sample.yaml files for additional options. if self.args.test_item: self._parse_test_item() - if source_dir: - if self.args.source_dir: - log.die("source directory specified twice:({} and {})".format( - source_dir, self.args.source_dir)) - self.args.source_dir = source_dir log.dbg('source_dir: {} cmake_opts: {}'.format(self.args.source_dir, self.args.cmake_opts), level=log.VERBOSE_EXTREME) From 2e9aac50c72c7bcd6ef3b900698d9629bf26d87e Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 25 Nov 2021 21:07:25 +1000 Subject: [PATCH 3/4] west_commands: build: add `--snippet` arg Add a `west build` argument (`-s`, `--snippet`) for constructing the cmake `SNIPPETS` variable. Signed-off-by: Jordan Yates --- scripts/west_commands/build.py | 6 +++++- scripts/west_commands/completion/west-completion.bash | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/west_commands/build.py b/scripts/west_commands/build.py index 7c159686114ca..e79b8575d02ec 100644 --- a/scripts/west_commands/build.py +++ b/scripts/west_commands/build.py @@ -22,7 +22,7 @@ BUILD_USAGE = '''\ west build [-h] [-b BOARD[@REV]]] [-d BUILD_DIR] [-t TARGET] [-p {auto, always, never}] [-c] [--cmake-only] - [-n] [-o BUILD_OPT] [-f] + [-n] [-o BUILD_OPT] [-f] [-s SNIPPET] [source_dir] -- [cmake_opt [cmake_opt ...]] ''' @@ -102,6 +102,8 @@ def do_add_parser(self, parser_adder): help='board to build for with optional board revision') parser.add_argument('-d', '--build-dir', help='build directory to create or use') + parser.add_argument('-s', '--snippet', default=[], action='append', + help='''snippets to apply when building''') self.add_force_arg(parser) group = parser.add_argument_group('cmake and build tool') @@ -431,6 +433,8 @@ def _run_cmake(self, board, origin, cmake_opts): cmake_opts = [] if self.args.cmake_opts: cmake_opts.extend(self.args.cmake_opts) + if self.args.snippet: + cmake_opts.extend(['-DSNIPPETS={}'.format(" ".join(self.args.snippet))]) user_args = config_get('cmake-args', None) if user_args: diff --git a/scripts/west_commands/completion/west-completion.bash b/scripts/west_commands/completion/west-completion.bash index 8ec8b373e8fd3..2bc76f9d7ed49 100644 --- a/scripts/west_commands/completion/west-completion.bash +++ b/scripts/west_commands/completion/west-completion.bash @@ -627,6 +627,7 @@ __comp_west_build() local build_args_opts=" --board -b + --snippet -s --build-dir -d --target -t --pristine -p From 7b43f0854bcc923945bec2ee164a087c0e3866d9 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 25 Nov 2021 21:31:04 +1000 Subject: [PATCH 4/4] boards: nrf52840dk_nrf52840: snippets example Temporary commit to show how existing definitions could be split under the "snippets" paradigm. Example build commands: ``` west build -b nrf52840dk_nrf52840 samples/basic/blinky west build -b nrf52840dk_nrf52840 samples/basic/blinky -s usb west build -b nrf52840dk_nrf52840 samples/basic/blinky -s mb_boot west build -b nrf52840dk_nrf52840 samples/basic/blinky -s mb_slot0 west build -b nrf52840dk_nrf52840 samples/basic/blinky -s usb -s mb_slot0 ``` Signed-off-by: Jordan Yates --- .../arm/nrf52840dk_nrf52840/Kconfig.defconfig | 3 ++ .../dts/mb_partitions.dtsi | 34 +++++++++++++++++++ .../nrf52840dk_nrf52840.dts | 25 +++----------- .../snippets/mb_boot.overlay | 14 ++++++++ .../snippets/mb_slot0.overlay | 14 ++++++++ .../arm/nrf52840dk_nrf52840/snippets/usb.conf | 1 + .../nrf52840dk_nrf52840/snippets/usb.overlay | 11 ++++++ 7 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 boards/arm/nrf52840dk_nrf52840/dts/mb_partitions.dtsi create mode 100644 boards/arm/nrf52840dk_nrf52840/snippets/mb_boot.overlay create mode 100644 boards/arm/nrf52840dk_nrf52840/snippets/mb_slot0.overlay create mode 100644 boards/arm/nrf52840dk_nrf52840/snippets/usb.conf create mode 100644 boards/arm/nrf52840dk_nrf52840/snippets/usb.overlay diff --git a/boards/arm/nrf52840dk_nrf52840/Kconfig.defconfig b/boards/arm/nrf52840dk_nrf52840/Kconfig.defconfig index 31850c6593754..4192a0c0a28ed 100644 --- a/boards/arm/nrf52840dk_nrf52840/Kconfig.defconfig +++ b/boards/arm/nrf52840dk_nrf52840/Kconfig.defconfig @@ -11,4 +11,7 @@ config BOARD config BT_CTLR default BT +config USE_DT_CODE_PARTITION + default y + endif # BOARD_NRF52840DK_NRF52840 diff --git a/boards/arm/nrf52840dk_nrf52840/dts/mb_partitions.dtsi b/boards/arm/nrf52840dk_nrf52840/dts/mb_partitions.dtsi new file mode 100644 index 0000000000000..a31e89ad58445 --- /dev/null +++ b/boards/arm/nrf52840dk_nrf52840/dts/mb_partitions.dtsi @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022, Commonwealth Scientific and Industrial Research + * Organisation (CSIRO) ABN 41 687 119 230. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/delete-node/ &standalone_partition; + +&flash0 { + + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + boot_partition: partition@0 { + label = "mcuboot"; + reg = <0x000000000 0x0000C000>; + }; + slot0_partition: partition@c000 { + label = "image-0"; + reg = <0x0000C000 0x00067000>; + }; + slot1_partition: partition@73000 { + label = "image-1"; + reg = <0x00073000 0x00067000>; + }; + scratch_partition: partition@da000 { + label = "image-scratch"; + reg = <0x000da000 0x0001e000>; + }; + }; +}; diff --git a/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840.dts b/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840.dts index 65c8223eee262..354f63141ddd0 100644 --- a/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840.dts +++ b/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840.dts @@ -20,7 +20,7 @@ zephyr,bt-c2h-uart = &uart0; zephyr,sram = &sram0; zephyr,flash = &flash0; - zephyr,code-partition = &slot0_partition; + zephyr,code-partition = &standalone_partition; }; leds { @@ -251,29 +251,14 @@ arduino_spi: &spi3 { compatible = "fixed-partitions"; #address-cells = <1>; #size-cells = <1>; - - boot_partition: partition@0 { - label = "mcuboot"; - reg = <0x00000000 0x0000C000>; - }; - slot0_partition: partition@c000 { - label = "image-0"; - reg = <0x0000C000 0x00067000>; - }; - slot1_partition: partition@73000 { - label = "image-1"; - reg = <0x00073000 0x00067000>; - }; - scratch_partition: partition@da000 { - label = "image-scratch"; - reg = <0x000da000 0x0001e000>; - }; - /* * The flash starting at 0x000f8000 and ending at * 0x000fffff is reserved for use by the application. */ - + standalone_partition: partition@0 { + label = "standalone"; + reg = <0x000000000 0x000F8000>; + }; /* * Storage partition will be used by FCB/LittleFS/NVS * if enabled. diff --git a/boards/arm/nrf52840dk_nrf52840/snippets/mb_boot.overlay b/boards/arm/nrf52840dk_nrf52840/snippets/mb_boot.overlay new file mode 100644 index 0000000000000..aa31ed2e60d1a --- /dev/null +++ b/boards/arm/nrf52840dk_nrf52840/snippets/mb_boot.overlay @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022, Commonwealth Scientific and Industrial Research + * Organisation (CSIRO) ABN 41 687 119 230. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mb_partitions.dtsi" + +/ { + chosen { + zephyr,code-partition = &boot_partition; + }; +}; diff --git a/boards/arm/nrf52840dk_nrf52840/snippets/mb_slot0.overlay b/boards/arm/nrf52840dk_nrf52840/snippets/mb_slot0.overlay new file mode 100644 index 0000000000000..7ca4ad77f5a86 --- /dev/null +++ b/boards/arm/nrf52840dk_nrf52840/snippets/mb_slot0.overlay @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2022, Commonwealth Scientific and Industrial Research + * Organisation (CSIRO) ABN 41 687 119 230. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mb_partitions.dtsi" + +/ { + chosen { + zephyr,code-partition = &slot0_partition; + }; +}; diff --git a/boards/arm/nrf52840dk_nrf52840/snippets/usb.conf b/boards/arm/nrf52840dk_nrf52840/snippets/usb.conf new file mode 100644 index 0000000000000..7e89fa3743f2e --- /dev/null +++ b/boards/arm/nrf52840dk_nrf52840/snippets/usb.conf @@ -0,0 +1 @@ +CONFIG_USB_DEVICE_STACK=y diff --git a/boards/arm/nrf52840dk_nrf52840/snippets/usb.overlay b/boards/arm/nrf52840dk_nrf52840/snippets/usb.overlay new file mode 100644 index 0000000000000..e42d0105bf9cf --- /dev/null +++ b/boards/arm/nrf52840dk_nrf52840/snippets/usb.overlay @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2021, Commonwealth Scientific and Industrial Research + * Organisation (CSIRO) ABN 41 687 119 230. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +&usbd { + compatible = "nordic,nrf-usbd"; + status = "okay"; +};