From 006a85af396187a55ffd612b594d2ccbc2f3e2bb Mon Sep 17 00:00:00 2001 From: Fabian Kaczmarczyck Date: Fri, 15 Dec 2023 15:49:27 +0100 Subject: [PATCH] Removes the nordicsemi pip dependency Tested on Linux, should work on Mac. We leave the responsibility to install `nrfutil` in version 6 to the user. --- deploy.py | 26 ++++++++++++++------------ docs/boards/nrf52840_dongle.md | 3 +++ docs/install.md | 13 +++++++------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/deploy.py b/deploy.py index 6dc21ca0..7d8195a6 100755 --- a/deploy.py +++ b/deploy.py @@ -23,6 +23,7 @@ import collections import copy import os +from serial.tools import list_ports import shutil import subprocess import sys @@ -202,6 +203,12 @@ def assert_python_library(module: str): f"Try to run: pip3 install {module}")) +def list_serials(vid: int, pid: int) -> List[str]: + ports = list_ports.comports() + ports = filter(lambda p: p.vid == vid and p.pid == pid, ports) + return list(map(lambda p: p.serial_number, ports)) + + class RemoveConstAction(argparse.Action): # pylint: disable=redefined-builtin @@ -703,10 +710,10 @@ def check_prerequisites(self): fatal("This board doesn't seem to support flashing through pyocd.") if self.args.programmer == "nordicdfu": - assert_mandatory_binary("nrfutil") assert_python_library("intelhex") - assert_python_library("nordicsemi.lister") - nrfutil_version = __import__("nordicsemi.version").version.NRFUTIL_VERSION + assert_mandatory_binary("nrfutil") + nrfutil_version = self.checked_command_output(["nrfutil", "version"]) + nrfutil_version = nrfutil_version.removeprefix("nrfutil version ") if not nrfutil_version.startswith("6."): fatal(("You need to install nrfutil python3 package v6.0 or above. " f"Found: v{nrfutil_version}. If you use Python >= 3.11, please " @@ -812,22 +819,17 @@ def run(self) -> int: info("Press [ENTER] when ready.") _ = input() # Search for the DFU devices - serial_number = [] - # pylint: disable=g-import-not-at-top,import-outside-toplevel - from nordicsemi.lister import device_lister - for device in device_lister.DeviceLister().enumerate(): - if device.vendor_id == "1915" and device.product_id == "521F": - serial_number.append(device.serial_number) - if not serial_number: + serial_numbers = list_serials(0x1915, 0x521F) + if not serial_numbers: fatal("Couldn't find any DFU device on your system.") - if len(serial_number) > 1: + if len(serial_numbers) > 1: fatal("Multiple DFU devices are detected. Please only connect one.") # Run the command without capturing stdout so that we show progress info("Flashing device using DFU...") dfu_return_code = subprocess.run( [ "nrfutil", "dfu", "usb-serial", f"--package={dfu_pkg_file}", - f"--serial-number={serial_number[0]}" + f"--serial-number={serial_numbers[0]}" ], check=False, timeout=None, diff --git a/docs/boards/nrf52840_dongle.md b/docs/boards/nrf52840_dongle.md index 737e081a..bda58e62 100644 --- a/docs/boards/nrf52840_dongle.md +++ b/docs/boards/nrf52840_dongle.md @@ -19,6 +19,9 @@ customize it. ### Flashing using DFU (preferred method) +You need `nrfutil` version 6. The [install manual](../install.md) has +setup instructions. + To flash the firmware, run: ```shell diff --git a/docs/install.md b/docs/install.md index 5303ee26..fad13696 100644 --- a/docs/install.md +++ b/docs/install.md @@ -25,8 +25,8 @@ following: * python3 and pip (can be installed with the `python3-pip` package on Debian) * the OpenSSL command line tool (can be installed and configured with the `libssl-dev` and `pkg-config` packages on Debian) -* `nrfutil` (can be installed using `pip3 install nrfutil`) if you want to flash - a device with DFU +* `nrfutil` (pip package of the same name), if you want to flash + a device with DFU. Read the disclaimer below. * `uuid-runtime` if you are missing the `uuidgen` command. * `llvm` and `gcc-arm-none-eabi` if you want to use the upgradability feature. @@ -37,10 +37,11 @@ instructions to appropriate binaries for your system. The scripts provided in this project have been tested under Linux and OS X. We haven't tested them on Windows and other platforms. -If you use Python newer than 3.10, then nrfutil for flashing over DFU is -currently not supported. Please use Python 3.10, or play around with [Nordic's -new tool](https://www.nordicsemi.com/Products/Development-tools/nrf-util) -instead. +You need `nrfutil` version 6, if you want to flash over DFU. +The tool doesn't support Python newer than 3.10. Therefore, we don't officially +support DFU for other versions. If you want to try regardless, +[Nordic's new tool](https://www.nordicsemi.com/Products/Development-tools/nrf-util) +might work for you. ### Compiling the firmware