From e18fa71913aea15f85d1bbaac1c9846cf02adc46 Mon Sep 17 00:00:00 2001 From: Wiktor Kwiatkowski Date: Mon, 3 Nov 2025 13:03:45 +0100 Subject: [PATCH] hw/scripts/nrfutil: make traits optional and improve device selection Traits are no longer by default. They are now optional and only used when the user explicitly wants to flash multiple boards at once. DFU mode is controlled through NRFUTIL_DFU_MODE, and NRFUTIL_DFU_SN can be used to select a specific DFU device when several are connected. --- hw/bsp/nordic_pca10059/syscfg.yml | 6 ++-- hw/scripts/nrfutil.sh | 54 ++++++++++++++++++++----------- hw/scripts/syscfg.yml | 15 +++++++-- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/hw/bsp/nordic_pca10059/syscfg.yml b/hw/bsp/nordic_pca10059/syscfg.yml index 6baacb3ad9..979bf37280 100644 --- a/hw/bsp/nordic_pca10059/syscfg.yml +++ b/hw/bsp/nordic_pca10059/syscfg.yml @@ -30,16 +30,16 @@ syscfg.defs: tool without debugger. value: 0 -syscfg.BSP_NRF_SDK_FLASH_LAYOUT: +syscfg.vals.BSP_NRF_SDK_FLASH_LAYOUT: # Change to use Nordic DFU bootloader which is likely to be used # when device still has original bootloader - NRFUTIL_TRAITS: nordicDfu + NRFUTIL_DFU_MODE: 1 + BOOTUTIL_SINGLE_APPLICATION_SLOT: 1 syscfg.vals: MYNEWT_DOWNLOADER: nrfutil JLINK_TARGET: nRF52840_xxAA PYOCD_TARGET: nrf52840 - NRFUTIL_TRAITS: jlink # Enable nRF52840 MCU and common startup code MCU_TARGET: nRF52840 diff --git a/hw/scripts/nrfutil.sh b/hw/scripts/nrfutil.sh index a6c8544540..03c46d0892 100755 --- a/hw/scripts/nrfutil.sh +++ b/hw/scripts/nrfutil.sh @@ -38,8 +38,14 @@ nrfutil_load () { echo "Missing filename" exit 1 fi + + NRF_TRAITS_OPT="" + if [ -n "${MYNEWT_VAL_NRFUTIL_TRAITS}" ]; then + NRF_TRAITS_OPT="--traits ${MYNEWT_VAL_NRFUTIL_TRAITS}" + fi + # If nordicDfu is to be used, create hex file directly from ELF - if [ "$MYNEWT_VAL_NRFUTIL_TRAITS" == "nordicDfu" ] ; then + if [ "${MYNEWT_VAL_NRFUTIL_DFU_MODE:-0}" -eq 1 ] ; then arm-none-eabi-objcopy -O ihex ${ELF_FILE} ${HEX_FILE} elif [ ! -f "${FILE_NAME}" ]; then # tries stripping current path for readability @@ -65,36 +71,48 @@ nrfutil_load () { fi ret=0 - if [ -z ${NRFUTIL_TRAITS} ] ; then - if [ -z ${MYNEWT_VAL_NRFUTIL_TRAITS} ] ; then - NRFUTIL_TRAITS="--traits jlink" + if [ "${MYNEWT_VAL_NRFUTIL_DFU_MODE:-0}" -eq 1 ] ; then + ZIP_FILE=${BIN_BASENAME}.zip + if [ -n "${MYNEWT_VAL_NRFUTIL_DFU_SN}" ] ; then + PORT=$(nrfutil device list --traits nordicDfu | awk -v dfu_sn=$MYNEWT_VAL_NRFUTIL_DFU_SN \ + 'dfu_sn==$1 { sn_match=1 } + /^Ports/ { if(sn_match) { print $2; sn_match=0 }} + ') + if [ -z "$PORT" ] ; then + echo "Error: NRFUTIL_DFU_SN does not match any connected board." + return 1 + fi else - NRFUTIL_TRAITS="--traits ${MYNEWT_VAL_NRFUTIL_TRAITS}" - if [ $MYNEWT_VAL_NRFUTIL_TRAITS == "nordicDfu" ] ; then - ZIP_FILE=${BIN_BASENAME}.zip - PORT=`nrfutil device list --traits nordicDfu | awk '/ports/ { print $2 }'` - # TODO: hw-version is probably incorrect for non NRF52 devices - nrfutil pkg generate --hw-version 52 --sd-req 0 --application ${HEX_FILE} --application-version 1 ${ZIP_FILE} - echo "Downloading" ${ZIP_FILE} - nrfutil dfu usb-serial -p ${PORT} --package ${ZIP_FILE} - if [ $? -ne 0 ]; then - ret=1 - fi + PORT=$(nrfutil device list --traits nordicDfu | awk '/^Ports/ { print $2 }') + PORT_COUNT=$(echo "$PORT" | wc -w) + if [ "$PORT_COUNT" -eq 0 ]; then + echo "Error: No Nordic DFU devices found." + return 1 + elif [ "$PORT_COUNT" -gt 1 ]; then + echo "Error: Found multiple DFU devices. Connect exactly one supported device or add NRFUTIL_DFU_SN to your target." + return 1 fi fi + + # TODO: hw-version is probably incorrect for non NRF52 devices + nrfutil pkg generate --hw-version 52 --sd-req 0 --application ${HEX_FILE} --application-version 1 ${ZIP_FILE} + echo "Downloading" ${ZIP_FILE} + nrfutil dfu usb-serial -p ${PORT} --package ${ZIP_FILE} + if [ $? -ne 0 ]; then + ret=1 + fi fi if [ -z ${ZIP_FILE} ] ; then jlink_sn echo "Downloading" ${HEX_FILE} - - nrfutil device program --firmware ${HEX_FILE} $SRN_ARG ${NRFUTIL_ARG} ${NRFUTIL_TRAITS} --options chip_erase_mode=ERASE_RANGES_TOUCHED_BY_FIRMWARE + nrfutil device program --firmware ${HEX_FILE} ${SRN_ARG} ${NRF_TRAITS_OPT} ${NRFUTIL_ARG} --options chip_erase_mode=ERASE_RANGES_TOUCHED_BY_FIRMWARE if [ $? -ne 0 ]; then ret=1 else - nrfutil device reset $SRN_ARG + nrfutil device reset $SRN_ARG ${NRF_TRAITS_OPT} fi fi diff --git a/hw/scripts/syscfg.yml b/hw/scripts/syscfg.yml index 012d6b709b..77076dce59 100644 --- a/hw/scripts/syscfg.yml +++ b/hw/scripts/syscfg.yml @@ -89,11 +89,20 @@ syscfg.defs: Some NRF have more then one core. This can specify non-default core (i.e. CP_NETWORK for NRF5340). value: + NRFUTIL_DFU_MODE: + description: > + Enables firmware upload through nrfutil dfu usb-serial. + value: NRFUTIL_TRAITS: description: > - Traits passed to nrfutil command. If not set `jlink` is the default. - It can be 'nordicDfu' to upload binary using Nordic SDK DFU - bootloader protocol. + Optional device traits used by nrfutil to select boards for flashing. + When set (e.g. 'jlink'), all connected devices matching this trait + will be programmed. + value: + NRFUTIL_DFU_SN: + description: > + DFU serial number passed to the nrfutil script. + Used to select the target device for firmware upload. value: MYNEWT_DEBUGGER_SN: description: >