Skip to content

Commit 539bffd

Browse files
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.
1 parent a33413b commit 539bffd

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

hw/bsp/nordic_pca10059/syscfg.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ syscfg.defs:
3030
tool without debugger.
3131
value: 0
3232

33-
syscfg.BSP_NRF_SDK_FLASH_LAYOUT:
33+
syscfg.vals.BSP_NRF_SDK_FLASH_LAYOUT:
3434
# Change to use Nordic DFU bootloader which is likely to be used
3535
# when device still has original bootloader
36-
NRFUTIL_TRAITS: nordicDfu
36+
NRFUTIL_DFU_MODE: 1
37+
BOOTUTIL_SINGLE_APPLICATION_SLOT: 1
3738

3839
syscfg.vals:
3940
MYNEWT_DOWNLOADER: nrfutil
4041
JLINK_TARGET: nRF52840_xxAA
4142
PYOCD_TARGET: nrf52840
42-
NRFUTIL_TRAITS: jlink
4343

4444
# Enable nRF52840 MCU and common startup code
4545
MCU_TARGET: nRF52840

hw/scripts/nrfutil.sh

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ nrfutil_load () {
3838
echo "Missing filename"
3939
exit 1
4040
fi
41+
42+
NRF_TRAITS_OPT=""
43+
if [ -n "${MYNEWT_VAL_NRFUTIL_TRAITS}" ]; then
44+
NRF_TRAITS_OPT="--traits ${MYNEWT_VAL_NRFUTIL_TRAITS}"
45+
fi
46+
4147
# If nordicDfu is to be used, create hex file directly from ELF
42-
if [ "$MYNEWT_VAL_NRFUTIL_TRAITS" == "nordicDfu" ] ; then
48+
if [ "${MYNEWT_VAL_NRFUTIL_DFU_MODE:-0}" -eq 1 ] ; then
4349
arm-none-eabi-objcopy -O ihex ${ELF_FILE} ${HEX_FILE}
4450
elif [ ! -f "${FILE_NAME}" ]; then
4551
# tries stripping current path for readability
@@ -65,36 +71,49 @@ nrfutil_load () {
6571
fi
6672

6773
ret=0
68-
if [ -z ${NRFUTIL_TRAITS} ] ; then
69-
if [ -z ${MYNEWT_VAL_NRFUTIL_TRAITS} ] ; then
70-
NRFUTIL_TRAITS="--traits jlink"
74+
if [ "${MYNEWT_VAL_NRFUTIL_DFU_MODE:-0}" -eq 1 ] ; then
75+
ZIP_FILE=${BIN_BASENAME}.zip
76+
if [ -n "${MYNEWT_VAL_NRFUTIL_DFU_SN}" ] ; then
77+
PORT=$(nrfutil device list --traits nordicDfu | awk -v dfu_sn=$MYNEWT_VAL_NRFUTIL_DFU_SN \
78+
'dfu_sn==$1 { sn_match=1 }
79+
/^Ports/ { if(sn_match) { print $2; sn_match=0 }}
80+
')
81+
if [ -z "$PORT" ] ; then
82+
echo "Error: NRFUTIL_DFU_SN does not match any connected board."
83+
return 1
84+
fi
7185
else
72-
NRFUTIL_TRAITS="--traits ${MYNEWT_VAL_NRFUTIL_TRAITS}"
73-
if [ $MYNEWT_VAL_NRFUTIL_TRAITS == "nordicDfu" ] ; then
74-
ZIP_FILE=${BIN_BASENAME}.zip
75-
PORT=`nrfutil device list --traits nordicDfu | awk '/ports/ { print $2 }'`
76-
# TODO: hw-version is probably incorrect for non NRF52 devices
77-
nrfutil pkg generate --hw-version 52 --sd-req 0 --application ${HEX_FILE} --application-version 1 ${ZIP_FILE}
78-
echo "Downloading" ${ZIP_FILE}
79-
nrfutil dfu usb-serial -p ${PORT} --package ${ZIP_FILE}
80-
if [ $? -ne 0 ]; then
81-
ret=1
82-
fi
86+
PORT=$(nrfutil device list --traits nordicDfu | awk '/^Ports/ { print $2 }')
87+
PORT_COUNT=$(echo "$PORT" | wc -w)
88+
if [ "$PORT_COUNT" -eq 0 ]; then
89+
echo "Error: No Nordic DFU devices found."
90+
return 1
91+
elif [ "$PORT_COUNT" -gt 1 ]; then
92+
echo "Error: Found multiple DFU devices. Connect exactly one supported device or add NRFUTIL_DFU_SN to your target."
93+
return 1
8394
fi
8495
fi
96+
97+
# TODO: hw-version is probably incorrect for non NRF52 devices
98+
nrfutil pkg generate --hw-version 52 --sd-req 0 --application ${HEX_FILE} --application-version 1 ${ZIP_FILE}
99+
echo "Downloading" ${ZIP_FILE}
100+
nrfutil dfu usb-serial -p ${PORT} --package ${ZIP_FILE}
101+
if [ $? -ne 0 ]; then
102+
ret=1
103+
fi
85104
fi
86105

87106
if [ -z ${ZIP_FILE} ] ; then
88107
jlink_sn
89108

90109
echo "Downloading" ${HEX_FILE}
91-
92-
nrfutil device program --firmware ${HEX_FILE} $SRN_ARG ${NRFUTIL_ARG} ${NRFUTIL_TRAITS} --options chip_erase_mode=ERASE_RANGES_TOUCHED_BY_FIRMWARE
110+
echo nrfutil device program --firmware ${HEX_FILE} ${SRN_ARG} ${NRF_TRAITS_OPT} ${NRFUTIL_ARG} --options chip_erase_mode=ERASE_RANGES_TOUCHED_BY_FIRMWARE
111+
nrfutil device program --firmware ${HEX_FILE} ${SRN_ARG} ${NRF_TRAITS_OPT} ${NRFUTIL_ARG} --options chip_erase_mode=ERASE_RANGES_TOUCHED_BY_FIRMWARE
93112

94113
if [ $? -ne 0 ]; then
95114
ret=1
96115
else
97-
nrfutil device reset $SRN_ARG
116+
nrfutil device reset $SRN_ARG ${NRF_TRAITS_OPT}
98117
fi
99118
fi
100119

hw/scripts/syscfg.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,20 @@ syscfg.defs:
8989
Some NRF have more then one core. This can specify non-default core
9090
(i.e. CP_NETWORK for NRF5340).
9191
value:
92+
NRFUTIL_DFU_MODE:
93+
description: >
94+
Enables firmware upload through nrfutil dfu usb-serial.
95+
value:
9296
NRFUTIL_TRAITS:
9397
description: >
94-
Traits passed to nrfutil command. If not set `jlink` is the default.
95-
It can be 'nordicDfu' to upload binary using Nordic SDK DFU
96-
bootloader protocol.
98+
Optional device traits used by nrfutil to select boards for flashing.
99+
When set (e.g. 'jlink'), all connected devices matching this trait
100+
will be programmed.
101+
value:
102+
NRFUTIL_DFU_SN:
103+
description: >
104+
DFU serial number passed to the nrfutil script.
105+
Used to select the target device for firmware upload.
97106
value:
98107
MYNEWT_DEBUGGER_SN:
99108
description: >

0 commit comments

Comments
 (0)