From e4b19de009c2730d507fd9c1727cde098dfeb9b3 Mon Sep 17 00:00:00 2001 From: Josh Klar Date: Sun, 2 Sep 2018 23:39:55 -0700 Subject: [PATCH] Escape hatches: flash the user keyboard script as part of DFU rather than pushing with ampy. WAY more stable, mostly unbrickable now --- Makefile | 52 ++++++++++------------- boards/klardotsh/twotwo_matrix_feather.py | 5 +-- entrypoints/feather_nrf52832.py | 5 +++ 3 files changed, 29 insertions(+), 33 deletions(-) create mode 100644 entrypoints/feather_nrf52832.py diff --git a/Makefile b/Makefile index 50235c2..43e2eab 100644 --- a/Makefile +++ b/Makefile @@ -12,43 +12,37 @@ build-feather-test: lint devdeps @echo "===> Building circuitpython/mpy-cross" @make -C vendor/circuitpython/mpy-cross @echo "===> Pulling Nordic BLE stack" - @cd vendor/circuitpython/ports/nrf && ./drivers/bluetooth/download_ble_stack.sh 2>/dev/null + @cd vendor/circuitpython/ports/nrf && ./drivers/bluetooth/download_ble_stack.sh 2>/dev/null >/dev/null @echo "===> Preparing KMK source for bundling into CircuitPython" @rm -rf vendor/circuitpython/ports/nrf/freeze/kmk - @cp -av kmk vendor/circuitpython/ports/nrf/freeze - @echo "===> Building CircuitPython with KMK" - make -C vendor/circuitpython/ports/nrf BOARD=feather_nrf52832 SERIAL=/dev/ttyUSB0 SD=s132 FROZEN_MPY_DIR=freeze clean dfu-gen dfu-flash - @echo "===> Pushing keyboard config" - sleep 2 - -pipenv run ampy rm main.py - #pipenv run ampy put boards/klardotsh/twotwo_matrix_feather.py main.py - @echo - @echo - @echo "IT IS NOT SAFE TO AUTOMATICALLY FLASH THIS BOARD IF YOU USE A WHILE TRUE LOOP. YOUR DEVICE CAN BECOME UNRECOVERABLE WITHOUT A JLINK" - @echo "TRY RUNNING pipenv run ampy run boards/klardotsh/twotwo_matrix_feather.py AND WATCHING THE SERIAL OUTPUT IN ANOTHER CONSOLE" - @echo "THAT IS CURRENTLY ALL THE KEYBOARD YOU CAN ABSOLUTELY SAFELY USE" - @echo "I'M DISCUSSING THIS WITH THE ADAFRUIT DEVELOPERS ON DISCORD, HOPEFULLY main.py BECOMES FLASHABLE BY DFU IN THE FUTURE" - @echo - @echo "Okay, now that the all caps disclaimer is out of the way..." - @echo "You can still run pipenv run ampy put boards/klardotsh/twotwo_matrix_feather.py main.py" - @echo "Your board will probably work. However, anything depending on the Python REPL (this includes ampy) will not." - @echo "If you do get stuck in this purgatory, run 'make oh-god-everything-is-stuck', which will flash a purposefully-failing build to your device" - @echo "This workaround ONLY works if your keyboard script tries to import from kmk, which I sure hope it does." - @echo "Once that process finishes, you can come back to make build-feather-test safely, or go play in the REPL, or drink." + @rm -rf vendor/circuitpython/ports/nrf/freeze/kmk_keyboard_user.py + @cp -av kmk vendor/circuitpython/ports/nrf/freeze/ + @cp -av boards/klardotsh/twotwo_matrix_feather.py vendor/circuitpython/ports/nrf/freeze/kmk_keyboard_user.py + @echo "===> Building and flashing CircuitPython with KMK and your keymap" + @make -C vendor/circuitpython/ports/nrf BOARD=feather_nrf52832 SERIAL=/dev/ttyUSB0 SD=s132 FROZEN_MPY_DIR=freeze clean dfu-gen dfu-flash + @echo "===> Flashing entrypoint if it doesn't already exist" + @sleep 2 + @-timeout -k 5s 10s pipenv run ampy rm main.py 2>/dev/null + @timeout -k 5s 10s pipenv run ampy put entrypoints/feather_nrf52832.py main.py + @echo "===> Flashed keyboard successfully!" -oh-god-everything-is-stuck: lint devdeps - @echo "===> Building a keyboard that explicitly CANNOT import KMK. Assuming your infinite-looping keyboard script imported KMK, it will now fail and be wiped. Cheers!" +# Fully wipe the board with only stock CircuitPython +burn-it-all-with-fire: lint devdeps + @echo "===> Flashing STOCK CircuitPython with no KMK or user keyboard scripts, and wiping entrypoint" + @echo "===> This is the nuclear option. Ctrl-C to cancel, or any key to continue" + @read @echo "===> Pulling dependencies, this may take several minutes" @git submodule update --init --recursive @echo "===> Building circuitpython/mpy-cross" @make -C vendor/circuitpython/mpy-cross @echo "===> Pulling Nordic BLE stack" - @cd vendor/circuitpython/ports/nrf && ./drivers/bluetooth/download_ble_stack.sh 2>/dev/null + @cd vendor/circuitpython/ports/nrf && ./drivers/bluetooth/download_ble_stack.sh 2>/dev/null >/dev/null @echo "===> Preparing KMK source for bundling into CircuitPython" @rm -rf vendor/circuitpython/ports/nrf/freeze/kmk - @echo "===> Building CircuitPython WITHOUT KMK to induce ImportError" - make -C vendor/circuitpython/ports/nrf BOARD=feather_nrf52832 SERIAL=/dev/ttyUSB0 SD=s132 FROZEN_MPY_DIR=freeze clean dfu-gen dfu-flash + @rm -rf vendor/circuitpython/ports/nrf/freeze/kmk_keyboard_user.py + @echo "===> Building CircuitPython WITHOUT KMK or user keyboard script to induce ImportError" + @make -C vendor/circuitpython/ports/nrf BOARD=feather_nrf52832 SERIAL=/dev/ttyUSB0 SD=s132 FROZEN_MPY_DIR=freeze clean dfu-gen dfu-flash @echo "===> Wiping keyboard config" - sleep 2 - pipenv run ampy rm main.py - @echo "===> Done! Fix your board script and retry flashing the right way" + @sleep 2 + @-pipenv run ampy rm main.py 2>/dev/null + @echo "===> Wiped! Probably safe to flash keyboard, try Python serial REPL to verify?" diff --git a/boards/klardotsh/twotwo_matrix_feather.py b/boards/klardotsh/twotwo_matrix_feather.py index 2c429f1..e8b23eb 100644 --- a/boards/klardotsh/twotwo_matrix_feather.py +++ b/boards/klardotsh/twotwo_matrix_feather.py @@ -1,12 +1,11 @@ import board -import time from kmk.circuitpython.matrix import MatrixScanner from kmk.common.consts import DiodeOrientation from kmk.common.keymap import Keymap -if __name__ == '__main__': +def main(): cols = (board.A4, board.A5) rows = (board.D27, board.A6) @@ -22,5 +21,3 @@ if __name__ == '__main__': while True: keymap.parse(matrix.raw_scan()) - # Yield to the controller to allow things like the REPL to take over - time.sleep(0.001) diff --git a/entrypoints/feather_nrf52832.py b/entrypoints/feather_nrf52832.py new file mode 100644 index 0000000..1f52211 --- /dev/null +++ b/entrypoints/feather_nrf52832.py @@ -0,0 +1,5 @@ +from kmk_keyboard_user import main + + +if __name__ == '__main__': + main()