Skip to content

Commit ebf4f5a

Browse files
authored
Merge pull request #26 from UncleGrumpy/pico_flash_improved
Improve `pico_flash` task
2 parents 8359e3b + 43f3bc4 commit ebf4f5a

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ If your pico uses a different device path or mount directory supply the full pat
430430

431431
shell$ rebar3 atomvm pico_flash --path /mnt/pico --reset /dev/cu.usbmodem1411202
432432

433-
> 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.
433+
> 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`.
434+
435+
> 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.
434436
435437
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.
436438

src/atomvm_pico_flash_provider.erl

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ init(State) ->
5454
{example, "rebar3 atomvm pico_flash"},
5555
% list of options understood by the plugin
5656
{opts, ?OPTS},
57-
{short_desc, "Convert an AtomVM packbeam file to uf2 and copy to a an rp2040 device"},
57+
{short_desc, "Convert an AtomVM packbeam file to uf2 and copy to an rp2040 device"},
5858
{desc,
5959
"~n"
60-
"Use this plugin to convert an AtomVM packbeam file to a rp2040 a uf2 file and copy to an rp2040 devices.~n"
60+
"Use this plugin to convert an AtomVM packbeam file to a uf2 file and copy to an rp2040 device.~n"
6161
}
6262
]),
6363
{ok, rebar_state:add_provider(State, Provider)}.
@@ -74,8 +74,8 @@ do(State) ->
7474
),
7575
{ok, State}
7676
catch
77-
C:E:S ->
78-
rebar_api:error("An error occurred in the ~p task. Class=~p Error=~p Stacktrace=~p~n", [?PROVIDER, C, E, S]),
77+
_C:E:_S ->
78+
rebar_api:error("An error occurred in the ~p task. Error=~p~n", [?PROVIDER, E]),
7979
{error, E}
8080
end.
8181

@@ -158,7 +158,7 @@ wait_for_mount(Mount, Count) when Count < 30 ->
158158
end;
159159
wait_for_mount(Mount, 30) ->
160160
rebar_api:error("Pico not mounted at ~s after 30 seconds. giving up...", [Mount]),
161-
rebar_api:abort().
161+
erlang:throw(mount_error).
162162

163163
%% @private
164164
check_pico_mount(Mount) ->
@@ -170,7 +170,7 @@ check_pico_mount(Mount) ->
170170
ok;
171171
"false\n" ->
172172
rebar_api:error("Pico not mounted at ~s.", [Mount]),
173-
rebar_api:abort()
173+
erlang:throw(no_device)
174174
end.
175175

176176
%% @private
@@ -202,26 +202,47 @@ do_flash(ProjectApps, PicoPath, ResetPort) ->
202202
ok;
203203
true ->
204204
Flag = get_stty_file_flag(),
205-
Reset = lists:join(" ", [
205+
BootselMode = lists:join(" ", [
206206
"stty", Flag, ResetPort, "1200"
207207
]),
208208
rebar_api:info("Resetting device at path ~s", [ResetPort]),
209-
ResetStatus = os:cmd(Reset),
209+
ResetStatus = os:cmd(BootselMode),
210210
case ResetStatus of
211211
"" ->
212212
ok;
213-
_Any ->
214-
rebar_api:error("Reset ~s failed. Is tty console attached?", [ResetPort]),
215-
rebar_api:abort()
213+
Error ->
214+
case os:find_executable(picotool) of
215+
false ->
216+
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]),
217+
erlang:throw(reset_error);
218+
_Path ->
219+
rebar_api:warn("Warning: ~s~nFor faster flashing remember to disconnect serial monitor first.", [Error]),
220+
DevReset = lists:join(" ", [
221+
"picotool", "reboot", "-f", "-u"
222+
]),
223+
rebar_api:warn("Disconnecting serial monitor with: `~s' in 5 seconds...", [DevReset]),
224+
timer:sleep(5000),
225+
RebootReturn = os:cmd(DevReset),
226+
RebootStatus = string:trim(RebootReturn),
227+
case RebootStatus of
228+
"The device was asked to reboot into BOOTSEL mode." ->
229+
ok;
230+
BootError ->
231+
rebar_api:error("Failed to prepare pico for flashing: ~s", [BootError]),
232+
erlang:throw(picoflash_reboot_error)
233+
end
234+
end
216235
end,
217-
rebar_api:info("Waiting for the device at path ~s to settle and mount...", [PicoPath]),
236+
PrettyPath = lists:join(" ", [
237+
"echo", PicoPath
238+
]),
239+
rebar_api:info("Waiting for the device at path ~s to settle and mount...", [string:trim(os:cmd(PrettyPath))]),
218240
wait_for_mount(PicoPath, 0)
219241
end,
220242
check_pico_mount(PicoPath),
221243
TargetUF2 = get_uf2_file(ProjectApps),
222244
Cmd = lists:join(" ", [
223245
"cp", "-v", TargetUF2, PicoPath
224246
]),
225-
rebar_api:info("Copying ~s to ~s...~n", [TargetUF2, PicoPath]),
226-
io:format("~s", [os:cmd(Cmd)]),
247+
rebar_api:info("Copying packbeam files...~n~s", [os:cmd(Cmd)]),
227248
ok.

0 commit comments

Comments
 (0)