This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 339aeb45b24c1bcb5ac30d49f7e54d8ed3e74dca
Author: Jerzy Kasenberg <jerzy.kasenb...@codecoup.pl>
AuthorDate: Wed Nov 27 13:30:13 2024 +0100

    hw/script: Add support for nrfutil
    
    This add initial support to program Nordic targets
    with nrfutil.
    
    Signed-off-by: Jerzy Kasenberg <jerzy.kasenb...@codecoup.pl>
---
 hw/scripts/download.sh |  6 ++++
 hw/scripts/nrfutil.sh  | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/scripts/syscfg.yml  |  8 +++++
 3 files changed, 104 insertions(+)

diff --git a/hw/scripts/download.sh b/hw/scripts/download.sh
index e9fc3113b..58ff03578 100644
--- a/hw/scripts/download.sh
+++ b/hw/scripts/download.sh
@@ -73,6 +73,12 @@ case "${MYNEWT_VAL_MYNEWT_DOWNLOADER}" in
     common_file_to_load
     nrfjprog_load
     ;;
+  "nrfutil")
+    check_downloader nrfutil
+    . $CORE_PATH/hw/scripts/nrfutil.sh
+    common_file_to_load
+    nrfutil_load
+    ;;
   "openocd")
     check_downloader openocd
     . $CORE_PATH/hw/scripts/openocd.sh
diff --git a/hw/scripts/nrfutil.sh b/hw/scripts/nrfutil.sh
new file mode 100755
index 000000000..ce86d7c61
--- /dev/null
+++ b/hw/scripts/nrfutil.sh
@@ -0,0 +1,90 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+. $CORE_PATH/hw/scripts/common.sh
+
+#
+# FILE_NAME must contain the name of the file to load
+# FLASH_OFFSET must contain the offset in flash where to place it
+#
+nrfutil_load () {
+    HEX_FILE=${BIN_BASENAME}.hex
+    ELF_FILE=${BIN_BASENAME}.elf
+    if [ -z ${FILE_NAME} ]; then
+        echo "Missing filename"
+        exit 1
+    fi
+    # If nordicDfu is to be used, create hex file directly from ELF
+    if [ "$MYNEWT_VAL_NRFUTIL_TRAITS" == "nordicDfu" ] ; then
+        arm-none-eabi-objcopy -O ihex ${ELF_FILE} ${HEX_FILE}
+    elif [ ! -f "${FILE_NAME}" ]; then
+        # tries stripping current path for readability
+        FILE=${FILE_NAME##$(pwd)/}
+        echo "Cannot find file" $FILE
+        exit 1
+    elif [ -z ${FLASH_OFFSET} ]; then
+        echo "Missing flash offset"
+        exit 1
+    else
+        arm-none-eabi-objcopy -O ihex -I binary --adjust-vma ${FLASH_OFFSET} 
${FILE_NAME} ${HEX_FILE}
+    fi
+
+    if [ -n "${MYNEWT_VAL_NRFJPROG_COPROCESSOR}" ] ; then
+      case ${MYNEWT_VAL_NRFJPROG_COPROCESSOR} in
+        "CP_NETWORK")
+          NRFUTIL_ARG="${NRFUTIL_ARG} --core Network"
+          ;;
+        *)
+          NRFUTIL_ARG="${NRFUTIL_ARG} --core Application"
+          ;;
+      esac
+    fi
+
+    ret=0
+    if [ -z ${NRFUTIL_TRAITS} ] ; then
+        if [ -z ${MYNEWT_VAL_NRFUTIL_TRAITS} ] ; then
+            NRFUTIL_TRAITS="--traits jlink"
+        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
+            fi
+        fi
+    fi
+
+    if [ -z ${ZIP_FILE} ] ; then
+        echo "Downloading" ${HEX_FILE}
+
+        nrfutil device program --firmware ${HEX_FILE} ${NRFUTIL_ARG} 
${NRFUTIL_TRAITS} --options chip_erase_mode=ERASE_RANGES_TOUCHED_BY_FIRMWARE
+
+        if [ $? -ne 0 ]; then
+            ret=1
+        else
+            nrfutil device reset
+        fi
+    fi
+
+    return $ret
+}
diff --git a/hw/scripts/syscfg.yml b/hw/scripts/syscfg.yml
index 6814a692c..c54f8366b 100644
--- a/hw/scripts/syscfg.yml
+++ b/hw/scripts/syscfg.yml
@@ -34,6 +34,8 @@ syscfg.defs:
             - pyocd
             # Nordic Semiconductor tool for NRF5x devices
             - nrfjprog
+            # Nordic Semiconductor tool for NRF5x devices
+            - nrfutil
             # Renesas ezFlashCLI for DA146x devices
             - ezflashcli
         value:
@@ -82,3 +84,9 @@ syscfg.defs:
             Some NRF have more then one core. This can specify non-default core
             (i.e. CP_NETWORK for NRF5340).
         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.
+        value:

Reply via email to