Skip to content

Commit

Permalink
Improve pico_flash task
Browse files Browse the repository at this point in the history
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 <winford@object.stream>
  • Loading branch information
UncleGrumpy committed Feb 7, 2024
1 parent 8359e3b commit b512102
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
33 changes: 27 additions & 6 deletions src/atomvm_pico_flash_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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)}.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b512102

Please sign in to comment.