Files

79 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Flash a XIAO nRF52840 with a text console (repeater, room server, sensor)
# over USB serial DFU -- no UF2 drive, no mount, no root, no 1200-baud touch.
#
# tools/xiao_serial_dfu.sh <firmware.zip> <usb-serial> <board>
#
# firmware.zip the DFU package (.pio/build/<env>/firmware.zip, or a release
# asset *.zip)
# usb-serial the board's USB serial number (matched in /dev/serial/by-id),
# or its name/alias in the local config
# board its name for serial_cli.py (local config, tools/mctools_config.py)
#
# NRF=<dir> directory holding tool-adafruit-nrfutil
# (default ~/.platformio/packages)
# PY=<python> interpreter (default python3)
# CLI=<path> serial_cli.py (default: next to this script)
#
# Sends `dfu serial` on the console, waits for the bootloader port
# (XIAO_nRF52840_Sense_<sn>) and runs adafruit-nrfutil on it; a board already
# in the bootloader is flashed as it is. Reports success only when the app is
# back on USB -- check `ver` yourself. Companion builds have no console: use
# `pio run -e <env> -t upload` there (touch). Refuses a board whose config
# entry has `refuse` set (e.g. the radio of an observer). Runs on any host that
# has the board on USB and a checkout of this repo -- no PlatformIO project
# build needed there, only the nrfutil package.
set -euo pipefail
case "${1:-}" in -h | --help | "")
sed -n '3,/^[^#]/{/^#/s/^# \{0,1\}//p}' "$0"
exit 0
;;
esac
[ $# -eq 3 ] || {
echo "usage: $0 <firmware.zip> <usb-serial> <board>" >&2
exit 2
}
ZIP=$1 SN=$2 BOARD=$3
# a board name/alias of the local config -> its USB serial; a serial passes through
SN="$(python3 "$(dirname "$0")/mctools_config.py" serial "$SN" 2>/dev/null || echo "$SN")"
WHY="$(python3 "$(dirname "$0")/mctools_config.py" get "$SN" refuse 2>/dev/null || true)"
[ -z "$WHY" ] || {
echo "refused: $WHY" >&2
exit 1
}
[ -f "$ZIP" ] || {
echo "$ZIP: no such file" >&2
exit 1
}
NRF=${NRF:-$HOME/.platformio/packages}
PY=${PY:-python3}
CLI=${CLI:-$(dirname "$(readlink -f "$0")")/serial_cli.py}
port() { compgen -G "/dev/serial/by-id/*$1$SN*" | head -1; }
if [ -n "$(port Seeed_Studio_XIAO_nRF52840_)" ]; then
timeout 20 "$PY" "$CLI" "$BOARD" "dfu serial" >/dev/null 2>&1 || true
fi
B=""
for _ in $(seq 1 25); do
B=$(port XIAO_nRF52840_Sense_ || true)
[ -n "$B" ] && break
sleep 1
done
[ -n "$B" ] || {
echo "$BOARD: bootloader port did not appear" >&2
exit 1
}
PYTHONPATH="$NRF/tool-adafruit-nrfutil/site-packages" timeout 120 "$PY" \
"$NRF/tool-adafruit-nrfutil/adafruit-nrfutil.py" dfu serial -pkg "$ZIP" \
-p "$B" -b 115200 --singlebank 2>&1 | tail -1
for _ in $(seq 1 30); do
if [ -n "$(port Seeed_Studio_XIAO_nRF52840_)" ]; then
echo "$BOARD: app back on USB"
exit 0
fi
sleep 2
done
echo "$BOARD: app did not come back on USB" >&2
exit 1