gen_uf2.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/sh
  2. # gen_uf2.sh -- convert a compiled .hex into a .uf2 for drag-and-drop flashing.
  3. #
  4. # The Adafruit nRF52 core ships uf2conv.py but leaves the UF2 objcopy recipe
  5. # COMMENTED OUT in platform.txt (recipe.objcopy.uf2.pattern), so `arduino-cli
  6. # compile` only ever emits .hex/.zip and never a .uf2. This regenerates the
  7. # .uf2 from the .hex after the compile step.
  8. #
  9. # Usage: ./gen_uf2.sh <input.hex> <output.uf2> [family]
  10. set -e
  11. HEX="$1"
  12. OUT="$2"
  13. # nRF52840 UF2 family id (boards.txt: feather52840.build.uf2_family=0xADA52840).
  14. FAMILY="${3:-0xADA52840}"
  15. if [ -z "$HEX" ] || [ -z "$OUT" ]; then
  16. echo "usage: $0 <input.hex> <output.uf2> [family]" >&2
  17. exit 2
  18. fi
  19. if [ ! -f "$HEX" ]; then
  20. echo "gen_uf2.sh: input hex not found: $HEX" >&2
  21. exit 1
  22. fi
  23. # Locate uf2conv.py inside the installed Adafruit nRF52 core. Honor a custom
  24. # Arduino data dir (config get), falling back to the per-OS defaults.
  25. DATA_DIR="$(arduino-cli config get directories.data 2>/dev/null || true)"
  26. if [ -z "$DATA_DIR" ]; then
  27. if [ -d "$HOME/Library/Arduino15" ]; then
  28. DATA_DIR="$HOME/Library/Arduino15"
  29. else
  30. DATA_DIR="$HOME/.arduino15"
  31. fi
  32. fi
  33. UF2CONV="$(find "$DATA_DIR/packages/adafruit/hardware/nrf52" -name uf2conv.py 2>/dev/null | sort | tail -n1)"
  34. if [ -z "$UF2CONV" ]; then
  35. echo "gen_uf2.sh: could not find uf2conv.py under $DATA_DIR" >&2
  36. exit 1
  37. fi
  38. # Prefer python3; fall back to a bare python (the CI runner's setup-python
  39. # provides `python`, while macOS only ships `python3`). uf2conv.py is py3-safe.
  40. PYTHON="$(command -v python3 || command -v python || true)"
  41. if [ -z "$PYTHON" ]; then
  42. echo "gen_uf2.sh: no python interpreter found" >&2
  43. exit 1
  44. fi
  45. "$PYTHON" "$UF2CONV" -f "$FAMILY" -c -o "$OUT" "$HEX"
  46. echo "gen_uf2.sh: wrote $OUT (family $FAMILY)"