#hid #adafruit #uf2 #flash #command-line #makecode

app hf2-cli

CLI for Microsoft HID Flashing Library for UF2 Bootloaders

10 releases

0.3.3 Oct 29, 2021
0.3.1 Oct 17, 2020
0.2.1 Jul 29, 2020
0.1.1 Feb 18, 2020
0.1.0 Dec 5, 2019

#1571 in Embedded development

MIT/Apache

52KB
1K SLoC

hf2-cli

Command line implementation of the hf2 flashing over hid protocol commonly used in by Microsoft MakeCode and Adafruit hardware.

Unless you know otherwise, you probably want cargo-hf2

prerequisites

Utilizes the hidapi-sys crate which uses libusb.

linux

Youll need libusb depending on your distro you might do sudo apt-get install libudev-dev libusb-1.0-0-dev.

If you'd like to not use sudo, you'll need udev rules. With your board plugged in and in bootloader mode, use lsusb to find your vendorid, seen here as 239a

Bus 001 Device 087: ID 239a:001b Adafruit Industries Feather M0

Then put your vendorid below and save to something like /etc/udev/rules.d/99-adafruit-boards.rules

ATTRS{idVendor}=="239a", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="usb", ATTRS{idVendor}=="239a", MODE="0666"
SUBSYSTEM=="tty", ATTRS{idVendor}=="239a", MODE="0666"

Then reboot or run

sudo udevadm control --reload-rules
sudo udevadm trigger

mac

On mac, as of Catalina you will get a permissions prompt and must follow directions to allow "Input Monitoring" for the Terminal application.

install

cargo install hf2-cli

hf2 flashing elf files, or as a cargo runner

cargo build --release --example blinky_basic then hf2 elf target/thumbv7em-none-eabihf/release/examples/blinky_basic

Hf2 will attempt to autodetect a device by sending the bininfo command to any whitelisted vid/pids it finds connected and using the first one that responds, or you can specify pid and vid (before the subcommand) instead. hf2 --vid 0x239a --pid 0x003d elf target/thumbv7em-none-eabihf/release/examples/blinky_basic

However the optimal use is as a cargo runner. In your .cargo/config set hf2 as your runner

[target.thumbv7em-none-eabihf]
runner = "hf2 elf"

Then either cargo run --release --example blinky_basic Or use your ide's "run" button and it will build and upload.

hf2 standalone to flash binaries

The flash command deals in binaries, not elf files so you're going to have to get a bin with something like cargo binutils cargo objcopy --release --example blinky_basic -- -O binary blinky_basic.bin

Then all you need your bootloaders address offset. hf2 blinky_basic.bin -a 0x4000

Hf2 will attempt to autodetect a device by sending the bininfo command to any whitelisted vid/pids it finds connected and using the first one that responds, or you can specify pid and vid (before the subcommand) instead. hf2 -v 0x239a -p 0x003d flash -f blinky_basic.bin -a 0x4000

troubleshooting

If it cant find a device, make sure your device is in a bootloader mode ready to receive firmware.

thread 'main' panicked at 'Are you sure device is plugged in and in bootloader mode?: OpenHidDeviceError', src/libcore/result.rs:1165:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

On the PyGamer, two button presses enables a blue and green screen that says PyGamer and also generally creates a flash drive which you should be able to see (though this doesn't use that method).

If you find another error, be sure to run with debug to see where in the process it failed and include those logs when reporting

RUST_LOG=debug hf2 -v 0x239a -p 0x003d flash -f neopixel_rainbow.bin -a 0x4000

Dependencies

~6–15MB
~182K SLoC