Skip to content

Commit 5685f2a

Browse files
committed
Enhancements for the esp32_flash task
The `offset` for the beam application partition is now read from the partition table on the attached esp32 device. When a custom partition table is used that does not use `main.avm` for the beam app partition name the `app_partition` parameter should be used to specify the application partition to be flashed. Valid application partition sub-types (the type is `data`) are `phy` or `0xAA`. If the `offset` parameter is specified it will be used to verify that the offset address of the application partition matched the expected value. This may be used to prevent flashing to a standard build of AtomMV for application that require a custom partition table. The `port` that the ESP32 is attached to is now auto discovered by default. When more than one ESP32 device is plugged into USB the port should be specified to control which device is flashed. Error reporting has been improved with descriptive error messages. Dialyzer warnings for the esp32_flash task have been fixed when analyzing atomvm_rebar3_plugin. Signed-off-by: Winford <[email protected]>
1 parent a33859b commit 5685f2a

13 files changed

+405
-35
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Added dialyzer task to simplify running dialyzer on AtomVM applications.
1818
- Added support for rp2350 devices to allow for default detection of the device mount path.
1919
- Added configuration paramenter for setting the path to picotool for the pico_flash task.
20+
- Added `app_partition` parameter to `esp32_flash` task. This is only needed to be provided for
21+
custom partition tables that do not use `main.avm` for the beam application partition name, or to
22+
flash to a custom alternate partition.
2023

2124
### Changed
2225
- The `uf2create` task now creates `universal` format uf2 files by default, suitable for both
2326
rp2040 or rp2350 devices.
2427
- The `pico_flash` task now checks that a device is an RP2 platform before resetting to `BOOTSEL`
2528
mode, preventing interference with other MCUs that may be attached to the host system.
2629
- The `pico_flash` task now aborts on all errors rather than trying to continue after a failure.
30+
- The `offset` used by the `esp32_flash` task is now read from the partition table of the device.
31+
When this parameter is provided it will be used to verify the offset of the application partition on
32+
flash matches the expected value.
33+
- The `esp32_flash` task now uses auto discovery for the `port` by default.
34+
- Stacktraces are not shown by default if the `esp32_flash` fails, instead a descriptive error
35+
message is displayed. To view the stacktrace use diagnostic mode.
36+
37+
### Fixed
38+
- The `esp32_flash` task aborts when an error occurs, rather than attempt to continue after a step
39+
has failed.
2740

2841
## [0.7.5] (2025.05.27)
2942

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,24 +255,30 @@ Running this AVM file will boot the `myapp` application automatically, without h
255255

256256
You may use the `esp32_flash` task to flash the generated AtomVM packbeam application to the flash storage on an ESP32 device connected over a serial connection.
257257

258-
shell$ rebar3 help atomvm esp32_flash
259-
260-
Use this plugin to flash an AtomVM packbeam file to an ESP32 device.
261-
262-
Usage: rebar3 atomvm esp32_flash [-e <esptool>] [-c <chip>] [-p <port>]
263-
[-b <baud>] [-o <offset>]
264-
265-
-e, --esptool Path to esptool.py
266-
-c, --chip ESP chip (default auto)
267-
-p, --port Device port (default /dev/ttyUSB0)
268-
-b, --baud Baud rate (default 115200)
269-
-o, --offset Offset (default 0x210000)
258+
```shell
259+
shell$ rebar3 help atomvm esp32_flash
260+
261+
Use this plugin to flash an AtomVM packbeam file to an ESP32 device.
262+
263+
Usage: rebar3 atomvm esp32_flash [-e <esptool>] [-c <chip>] [-p <port>]
264+
[-b <baud>] [-o <offset>]
265+
[-a <app_partition>]
266+
267+
-e, --esptool Path to esptool.py
268+
-c, --chip ESP chip (default auto)
269+
-p, --port Device port (default auto discovery)
270+
-b, --baud Baud rate (default 115200)
271+
-o, --offset Offset (default read from device) *old behavior
272+
deprecated, use app_partition. When given, verifies
273+
expected offset to actual
274+
-a, --app_partition Application partition name (default main.avm)
275+
```
270276

271277
The `esp32_flash` task will use the `esptool.py` command to flash the ESP32 device. This tool is available via the <a href="https://docs.espressif.com/projects/esp-idf/en/latest/esp32/">IDF SDK</a>, or directly via <a href="https://github.com/espressif/esptool">github</a>. The `esptool.py` command is also available via many package managers (e.g., MacOS Homebrew).
272278

273-
By default, the `esp32_flash` task will assume the `esptool.py` command is available on the user's executable path. Alternatively, you may specify the full path to the `esptool.py` command via the `-e` (or `--esptool`) option
279+
By default, the `esp32_flash` task will assume the `esptool.py` command is available on the user's executable path. Alternatively, you may specify the full path to the `esptool.py` command via the `-e` (or `--esptool`) option.
274280

275-
By default, the `esp32_flash` task will write to port `/dev/ttyUSB0` at a baud rate of `115200`. You may control the port and baud settings for connecting to your ESP device via the `-port` and `-baud` options to the `esp32_flash` task, e.g.,
281+
By default, the `esp32_flash` task uses port auto discovery at a baud rate of `115200`. You may control the port and baud settings for connecting to your ESP device via the `-port` and `-baud` options to the `esp32_flash` task, e.g.,
276282

277283
shell$ rebar3 atomvm esp32_flash --port /dev/tty.SLAB_USBtoUART --baud 921600
278284
...
@@ -301,7 +307,8 @@ The following table enumerates the properties that may be defined in your projec
301307
| `chip` | `string()` | ESP32 chip type |
302308
| `port` | `string()` | Device port on which the ESP32 can be located |
303309
| `baud` | `integer()` | Device BAUD rate |
304-
| `offset` | `string()` | Offset into which to write AtomVM application |
310+
| `offset` | `string()` | Optionally verify offset on flash matches expected value. Original behavior deprecated: use `app_partition` for custom images |
311+
| `app_partition` | `string()` | Name of application partition to write AtomVM application for custom partition tables |
305312

306313
Example:
307314

@@ -314,9 +321,20 @@ Alternatively, the following environment variables may be used to control the ab
314321
* `ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_PORT`
315322
* `ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_BAUD`
316323
* `ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_OFFSET`
324+
* `ATOMVM_REBAR3_PLUGIN_ESP32_APP_PARTITION`
317325

318326
Any setting specified on the command line take precedence over settings in `rebar.config`, which in turn take precedence over environment variable settings, which in turn take precedence over the default values specified above.
319327

328+
```note
329+
The `offset` configuration option is deprecated, the correct offset for standard AtomVM builds are
330+
determined by the partition table flashed to the device. Elixir supported builds are recognized and
331+
the correct offset will be used. When using a custom partition table it is necessary to supply the
332+
`app_partition` name. If an offset is given it will be compared to the address of the discovered
333+
`app_partition` and an error will be raised if they do not match. The partition name of the
334+
application partition for standard AtomVM partition tables is `main.avm`, but it is not necessary
335+
to supply the name when using a release image or one of the standard partition tables.
336+
```
337+
320338
The `esp32_flash` task depends on the `packbeam` task, so the packbeam file will get automatically built if any changes have been made to its dependencies.
321339

322340
### The `stm32_flash` task

UPDATING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
# `atomvm_rebar3_plugin` Update Instructions
88

9+
## (unreleased)
10+
11+
- The `esp32_flash` task now reads the application `offset` from the partition table on the device.
12+
If you are using a custom partition table that does not use `main.avm` for the application partition
13+
name you should supply the name used with the `app_partition` parameter. An `offset` may optionally
14+
be supplied to assure the offset of the application partition matches the expected offset, this may
15+
be helpful to assure that specific applications are only flashed to devices with a custom build of
16+
AtomVM.
17+
- Pico 2 (RP2350) devices are recognized and now work with default parameters. Specifying device
18+
path and uf2 flavor for these chipsets is no longer necessary.
19+
920
## 0.6.* -> 0.7.*
1021

1122
- The `atomvm_rebar3_plugin` tasks have been moved into the `atomvm` namespace (from the [`rebar3`](https://rebar3.org) `default` namespace). The "legacy" tasks in the `default` namespace are deprecated, and users will be issued a warning when used. Be sure to use the `atomvm` namespace in any future usage of this plugin, as the deprecated tasks may be removed without warning. E.g., `rebar3 atomvm packbeam ...`

src/atomvm_esp32_flash_provider.erl

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,21 @@
3030
-define(OPTS, [
3131
{esptool, $e, "esptool", string, "Path to esptool.py"},
3232
{chip, $c, "chip", string, "ESP chip (default auto)"},
33-
{port, $p, "port", string, "Device port (default /dev/ttyUSB0)"},
33+
{port, $p, "port", string, "Device port (default auto discovery)"},
3434
{baud, $b, "baud", integer, "Baud rate (default 115200)"},
35-
{offset, $o, "offset", string, "Offset (default 0x210000)"}
35+
{offset, $o, "offset", string,
36+
"Offset (default read from device) *old behavior deprecated, use app_partition."
37+
" When given, verifies expected offset to actual"},
38+
{app_partition, $a, "app_partition", string, "Application partition name (default main.avm)"}
3639
]).
3740

3841
-define(DEFAULT_OPTS, #{
3942
esptool => "esptool.py",
4043
chip => "auto",
41-
port => "/dev/ttyUSB0",
42-
baud => 115200,
43-
offset => "0x210000"
44+
port => "auto",
45+
baud => "115200",
46+
offset => auto,
47+
app_partition => "main.avm"
4448
}).
4549

4650
%%
@@ -81,13 +85,22 @@ do(State) ->
8185
maps:get(chip, Opts),
8286
maps:get(port, Opts),
8387
maps:get(baud, Opts),
84-
maps:get(offset, Opts)
88+
maybe_convert_string(maps:get(offset, Opts)),
89+
list_to_binary(maps:get(app_partition, Opts)),
90+
State
8591
),
8692
{ok, State}
8793
catch
94+
C:rebar_abort:S ->
95+
rebar_api:error(
96+
"A fatal error occurred in the ~p task.",
97+
[?PROVIDER]
98+
),
99+
rebar_api:debug("Class=~p, Error=~p~nSTACKTRACE:~n~p~n", [C, rebar_abort, S]),
100+
{error, rebar_abort};
88101
C:E:S ->
89102
rebar_api:error(
90-
"An error occurred in the ~p task. Error=~p",
103+
"An unhandled error occurred in the ~p task. Error=~p",
91104
[?PROVIDER, E]
92105
),
93106
rebar_api:debug("Class=~p, Error=~p~nSTACKTRACE:~n~p~n", [C, E, S]),
@@ -105,6 +118,7 @@ format_error(Reason) ->
105118
%% @private
106119
get_opts(State) ->
107120
{ParsedArgs, _} = rebar_state:command_parsed_args(State),
121+
rebar_api:debug("ParsedArgs = ~p", [ParsedArgs]),
108122
RebarOpts = atomvm_rebar3_plugin:get_atomvm_rebar_provider_config(State, ?PROVIDER),
109123
ParsedOpts = atomvm_rebar3_plugin:proplist_to_map(ParsedArgs),
110124
maps:merge(
@@ -133,20 +147,47 @@ env_opts() ->
133147
maps:get(baud, ?DEFAULT_OPTS)
134148
)
135149
),
136-
offset => os:getenv(
137-
"ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_OFFSET",
138-
maps:get(offset, ?DEFAULT_OPTS)
150+
offset => maybe_convert_string(
151+
os:getenv(
152+
"ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_OFFSET",
153+
maps:get(offset, ?DEFAULT_OPTS)
154+
)
155+
),
156+
app_partition => os:getenv(
157+
"ATOMVM_REBAR3_PLUGIN_ESP32_APP_PARTITION",
158+
maps:get(app_partition, ?DEFAULT_OPTS)
139159
)
140160
}.
141161

142162
%% @private
143163
maybe_convert_string(S) when is_list(S) ->
144-
list_to_integer(S);
164+
case lists:prefix("0x", S) of
165+
true ->
166+
list_to_integer(lists:subtract(S, "0x"), 16);
167+
false ->
168+
list_to_integer(S)
169+
end;
145170
maybe_convert_string(I) ->
146171
I.
147172

148173
%% @private
149-
do_flash(ProjectApps, EspTool, Chip, Port, Baud, Offset) ->
174+
do_flash(ProjectApps, EspTool, Chip, Port, Baud, Address, Partition, State) ->
175+
Offset =
176+
case Address of
177+
auto ->
178+
read_flash_offset(EspTool, Port, Partition, State);
179+
Val ->
180+
Offset0 = read_flash_offset(EspTool, Port, Partition, State),
181+
case Val =:= Offset0 of
182+
true ->
183+
Offset0;
184+
false ->
185+
rebar_api:abort(
186+
"The configured offset 0x~.16B does not match the partition table on the device (0x~.16B).",
187+
[Val, Offset0]
188+
)
189+
end
190+
end,
150191
[ProjectAppAVM | _] = [get_avm_file(ProjectApp) || ProjectApp <- ProjectApps],
151192
Portparam =
152193
case Port of
@@ -172,9 +213,12 @@ do_flash(ProjectApps, EspTool, Chip, Port, Baud, Offset) ->
172213
"keep",
173214
"--flash_size",
174215
"detect",
175-
Offset,
216+
integer_to_list(Offset),
176217
ProjectAppAVM
177218
]),
219+
AVMApp = filename:basename(ProjectAppAVM),
220+
rebar_api:info("Flashing ~s to device.", [AVMApp]),
221+
%% The following log output is parsed by the tests and should not be changed or removed.
178222
rebar_api:info("~s~n", [Cmd]),
179223
rebar_api:console("~s", [os:cmd(Cmd)]),
180224
ok.
@@ -185,3 +229,44 @@ get_avm_file(App) ->
185229
Name = binary_to_list(rebar_app_info:name(App)),
186230
DirName = filename:dirname(OutDir),
187231
filename:join(DirName, Name ++ ".avm").
232+
233+
%% @private
234+
read_flash_offset(Esptool, Port, PartName, State) ->
235+
TempFile = get_part_tempfile(State),
236+
rebar_api:info("Reading application partition offset from device...", []),
237+
try esp_partition:read_app_offset(Esptool, Port, PartName, TempFile) of
238+
Offset ->
239+
Offset
240+
catch
241+
_:invalid_partition_table:_ ->
242+
rebar_api:abort("Invalid partition data!", []);
243+
_:no_device:_ ->
244+
rebar_api:abort("No ESP32 device attached!", []);
245+
_:{partition_not_found, Partition}:_ ->
246+
rebar_api:error("The partition ~s was not fount on device partition table!", [Partition]),
247+
rebar_api:abort(
248+
"When using a custom partition table always specify the 'app_partition' NAME.", []
249+
);
250+
_:{invalid_subtype, Type}:_ ->
251+
rebar_api:abort("The partition ~s was found, but used invalid subtype 0x~s.", [
252+
PartName, Type
253+
]);
254+
_:invalid_partition_data:_ ->
255+
rebar_api:abort("The partition ~s was found, but partition data is invalid.", [PartName]);
256+
_:Error:_ ->
257+
rebar_api:abort("Unexpected error reading partition table from device, ~p.", [Error])
258+
end.
259+
260+
%% @private
261+
get_part_tempfile(State) ->
262+
OutDir = filename:absname(rebar_dir:base_dir(State)),
263+
TempFile = filename:absname_join(OutDir, "part.tmp"),
264+
case filelib:is_file(TempFile) of
265+
true ->
266+
rebar_api:debug("Removing possibly stale partition dump data ~s", [TempFile]),
267+
Cmd = lists:join(" ", ["rm", TempFile]),
268+
os:cmd(Cmd);
269+
false ->
270+
ok
271+
end,
272+
TempFile.

0 commit comments

Comments
 (0)