From b512102b8a71f4b4803f8af56a71e1ff039b407a Mon Sep 17 00:00:00 2001 From: Winford Date: Mon, 5 Feb 2024 11:27:50 -0800 Subject: [PATCH] Improve `pico_flash` task This set of changes will use `picotool` to reset the rp2040 device if a serial monitor is attached. A warning will also be issued notifying the user that faster flashing times can be achieved by disconnecting the serial monitor application before using the `atomvm pico_flash` task, as this also requires an extra delay before it can be put back into `BOOTSEL` mode. Signed-off-by: Winford --- README.md | 4 +++- src/atomvm_pico_flash_provider.erl | 33 ++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8faeab5..15c3267 100644 --- a/README.md +++ b/README.md @@ -430,7 +430,9 @@ If your pico uses a different device path or mount directory supply the full pat shell$ rebar3 atomvm pico_flash --path /mnt/pico --reset /dev/cu.usbmodem1411202 -> Warning: There is currently a known bug that occurs when the VM is compiled with the `-DAVM_WAIT_FOR_USB_CONNECT` cmake option. If you have previously connected to the tty serial port with `screen`, `minicom`, or similar and have disconnected or closed the session, the device will take unusually long to reset and fail to mount the FAT partition within 30 seconds and `pico_flash` will fail. This can be worked around by unplugging the pico from usb and plug it back in again, before repeating the flash procedure. +> Note: If you use the `pico_flash` task while you are still connected to a serial monitoring application such as `minicom` or `screen` the `pico_flash` task will attempt to locate [`picotool`](https://github.com/raspberrypi/picotool) in the users PATH. If `picotool` is found it will be used to reboot the pico, disconnecting the serial monitor in the process, and the device will be put into `BOOTSEL` mode after it is rebooted. If `picotool` is not available the user will be informed, and reminded that manually closing the serial monitor is necessary before using `pico_flash`. + +> Warning: There is currently a known bug that occurs when the VM has exited, the processor is left in as hung state. The device will take unusually long attempting to reset, and fail to mount the FAT partition within 30 seconds and `pico_flash` will fail. There is work under way to fix this bug, but in the meantime it can be worked around by unplugging the pico from usb and plug it back in again, before repeating the flash procedure. The following table enumerates the properties that may be defined in your project's `rebar.config` file for this task. Use `pico_flash` as the key for any properties defined for this task. diff --git a/src/atomvm_pico_flash_provider.erl b/src/atomvm_pico_flash_provider.erl index 666241f..2538ca0 100644 --- a/src/atomvm_pico_flash_provider.erl +++ b/src/atomvm_pico_flash_provider.erl @@ -57,7 +57,7 @@ init(State) -> {short_desc, "Convert an AtomVM packbeam file to uf2 and copy to a an rp2040 device"}, {desc, "~n" - "Use this plugin to convert an AtomVM packbeam file to a rp2040 a uf2 file and copy to an rp2040 devices.~n" + "Use this plugin to convert an AtomVM packbeam file to a uf2 file and copy to a rp2040 device.~n" } ]), {ok, rebar_state:add_provider(State, Provider)}. @@ -202,17 +202,38 @@ do_flash(ProjectApps, PicoPath, ResetPort) -> ok; true -> Flag = get_stty_file_flag(), - Reset = lists:join(" ", [ + BootselMode = lists:join(" ", [ "stty", Flag, ResetPort, "1200" ]), rebar_api:info("Resetting device at path ~s", [ResetPort]), - ResetStatus = os:cmd(Reset), + ResetStatus = os:cmd(BootselMode), case ResetStatus of "" -> ok; - _Any -> - rebar_api:error("Reset ~s failed. Is tty console attached?", [ResetPort]), - rebar_api:abort() + Error -> + case os:find_executable(picotool) of + false -> + rebar_api:error("Warning: ~s~nUnable to locate 'picotool', close the serial monitor before flashing, or install picotool for automatic disconnect and BOOTSEL mode.", [Error]), + rebar_api:abort(); + _Path -> + rebar_api:warn("Warning: ~s~nFor faster flashing remember to disconnect serial monitor first.", [Error]), + DevReset = lists:join(" ", [ + "picotool", "reboot", "-f" + ]), + rebar_api:warn("Disconnecting serial monitor with: `~s' in 5 seconds...", [DevReset]), + timer:sleep(5000), + RebootStatus = os:cmd(DevReset), + case RebootStatus of + "The device was asked to reboot into application mode.\n" -> + % wait for reboot to settle before attempting to enter BOOTSEL mode + timer:sleep(3000), + os:cmd(BootselMode), + ok; + BootError -> + rebar_api:error("Failed to prepare pico for flashing: ~s", [BootError]), + rebar_api:abort() + end + end end, rebar_api:info("Waiting for the device at path ~s to settle and mount...", [PicoPath]), wait_for_mount(PicoPath, 0)