Skip to content

Commit

Permalink
Add flash script for NiceNano with DFU
Browse files Browse the repository at this point in the history
This is an alternative flashing method for the Nice Nano V2's.  It will
work with other nrf52 devices with some extending.

The user only needs to put the n!n into bootloader mode and they will
able to be flashed without requiring additional steps.  As well, because
the system no-longer has a mounted filesystem just up and disappearing
there wont be left behind dead device references and the user doesn't
have to worry about umount getting locked up into uninterruptible sleep,
which means the user needs to reboot with a system that cannot cleanly
unmount the root filesystem anymore.
  • Loading branch information
nakato committed Jul 1, 2024
1 parent 6785095 commit 57c29a2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
8 changes: 8 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
inherit firmware;
};

flash-nicenano-dfu = pkgs.callPackage ./nix/flash-nicenano-dfu.nix {
inherit firmware adafruit-nrfutil;
};

update = pkgs.callPackage ./nix/update.nix {};
});

Expand Down Expand Up @@ -55,6 +59,10 @@
- Run `nix run .#update` to update West dependencies, including ZMK version, and bump the `zephyrDepsHash` on the derivation
- GitHub Actions to automatically PR flake lockfile bumps and West dependency bumps are included
- Using something like Mergify to automatically merge these PRs is recommended - see <https://github.com/lilyinstarlight/zmk-nix/blob/main/.github/mergify.yml> for an example Mergify configuration
## Alternative flash methods
- nice-nano via DFU over serial: change flash to flash = zmk-nix.packages.''${system}.flash-nicenano-dfu.override { inherit firmware; };
'';
};
};
Expand Down
87 changes: 87 additions & 0 deletions nix/flash-nicenano-dfu.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{ lib
, writeShellApplication
, systemd
, adafruit-nrfutil
, firmware
}:

writeShellApplication {
name = "zmk-nicenano-dfu-flash";

runtimeInputs = [
adafruit-nrfutil
# udevadm is from systemd
systemd
];

text = ''
TTY_PREFIXES=(ttyUSB ttyACM)
available() {
shopt -s nullglob
for prefix in "''${TTY_PREFIXES[@]}"; do
for tty in /dev/"''${prefix}"*; do
if [ "$(udevadm info --query=property --property=ID_MODEL --value "$tty")" == "nice_nano_v2" ]; then
echo "$tty"
shopt -u nullglob
return
fi
done
done
shopt -u nullglob
return 1
}
flash=("$@")
parts=(${toString firmware.parts or ""})
if [ "''${#flash[@]}" -eq 0 ]; then
if [ "''${#parts[@]}" -eq 0 ]; then
flash=("")
else
flash=("''${parts[@]}")
fi
else
for part in "''${flash[@]}"; do
if ! printf '%s\0' "''${parts[@]}" | grep -Fxqz -- "$part"; then
echo "The '$part' part does not exist in the firmware '"'${firmware.name}'"'"
exit 1
fi
done
fi
cleanup() {
for p in "''${flash[@]}"; do
rm -f "''${upgradedir}/''${p}.zip"
done
rmdir "''${upgradedir}"
}
upgradedir="$(mktemp -d)"
trap cleanup EXIT
for part in "''${flash[@]}"; do
echo -n "Double tap reset and plug in$([ -n "$part" ] && echo " the '$part' part of") the keyboard via USB"
while ! device="$(available)"; do
echo -n .
sleep 3
done
echo
adafruit-nrfutil dfu genpkg --dev-type 0x0052 --application ${firmware}/*"$([ -n "$part" ] && echo "_$part")".hex "''${upgradedir}/''${part}.zip"
adafruit-nrfutil dfu serial --package "''${upgradedir}/''${part}.zip" --port "$device" -b 115200
echo "Firmware copy complete."
sleep 1
echo
done
'';

meta = with lib; {
description = "ZMK nicenano DFU firmware flasher";
license = licenses.mit;
platforms = platforms.linux;
maintainers = with maintainers; [ ];
};
}

0 comments on commit 57c29a2

Please sign in to comment.