Skip to content

Commit 3a391b9

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 <winford@object.stream>
1 parent e62d9dd commit 3a391b9

14 files changed

+622
-46
lines changed

CHANGELOG.md

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

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

3245
## [0.7.5] (2025.05.27)
3346

README.md

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

261261
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.
262262

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

276282
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).
277283

278-
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
284+
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.
279285

280-
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.,
286+
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.,
281287

282288
shell$ rebar3 atomvm esp32_flash --port /dev/tty.SLAB_USBtoUART --baud 921600
283289
...
@@ -306,7 +312,8 @@ The following table enumerates the properties that may be defined in your projec
306312
| `chip` | `string()` | ESP32 chip type |
307313
| `port` | `string()` | Device port on which the ESP32 can be located |
308314
| `baud` | `integer()` | Device BAUD rate |
309-
| `offset` | `string()` | Offset into which to write AtomVM application |
315+
| `offset` | `string()` | Optionally verify offset on flash matches expected value. Original behavior deprecated: use `app_partition` for custom images or to target a different partition |
316+
| `app_partition` | `string()` | Name of application partition to write AtomVM application for custom partition tables |
310317

311318
Example:
312319

@@ -319,9 +326,21 @@ Alternatively, the following environment variables may be used to control the ab
319326
* `ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_PORT`
320327
* `ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_BAUD`
321328
* `ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_OFFSET`
329+
* `ATOMVM_REBAR3_PLUGIN_ESP32_APP_PARTITION`
322330

323331
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.
324332

333+
```note
334+
The behavior of the `offset` configuration option has changed, the correct offset for standard
335+
AtomVM builds are determined by the partition table flashed to the device. Elixir supported builds
336+
are recognized and the correct offset will be used. When using a custom partition table it is
337+
necessary to supply the `app_partition` name. If an offset is given it will be compared to the
338+
address of the discovered `app_partition` and an error will be raised if they do not match. The
339+
partition name of the application partition for standard AtomVM partition tables is `main.avm`, but
340+
it is not necessary to supply the name when using a release image or one of the standard partition
341+
tables.
342+
```
343+
325344
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.
326345

327346
### 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: 163 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@
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) *deprecated, use app_partition.\n"
37+
"When given, a warning will be issued if the partition name does not match the\n"
38+
"expected name, as long as the offset aligns with a valid application partition."},
39+
{app_partition, $a, "app_partition", string, "Application partition name (default main.avm)"}
3640
]).
3741

3842
-define(DEFAULT_OPTS, #{
3943
esptool => "esptool.py",
4044
chip => "auto",
41-
port => "/dev/ttyUSB0",
42-
baud => 115200,
43-
offset => "0x210000"
45+
port => "auto",
46+
baud => "115200",
47+
offset => "auto",
48+
app_partition => "main.avm"
4449
}).
4550

4651
%%
@@ -81,16 +86,25 @@ do(State) ->
8186
maps:get(chip, Opts),
8287
maps:get(port, Opts),
8388
maps:get(baud, Opts),
84-
maps:get(offset, Opts)
89+
integer_from_string(maps:get(offset, Opts)),
90+
maps:get(app_partition, Opts),
91+
State
8592
),
8693
{ok, State}
8794
catch
88-
C:E:S ->
95+
C:rebar_abort:S ->
8996
rebar_api:error(
90-
"An error occurred in the ~p task. Error=~p",
91-
[?PROVIDER, E]
97+
"A fatal error occurred in the ~p task.",
98+
[?PROVIDER]
9299
),
100+
rebar_api:debug("Class=~p, Error=~p~nSTACKTRACE:~n~p~n", [C, rebar_abort, S]),
101+
{error, rebar_abort};
102+
C:E:S ->
93103
rebar_api:debug("Class=~p, Error=~p~nSTACKTRACE:~n~p~n", [C, E, S]),
104+
rebar_api:abort(
105+
"An unhandled error occurred in the ~p task. Error=~p",
106+
[?PROVIDER, E]
107+
),
94108
{error, E}
95109
end.
96110

@@ -105,6 +119,7 @@ format_error(Reason) ->
105119
%% @private
106120
get_opts(State) ->
107121
{ParsedArgs, _} = rebar_state:command_parsed_args(State),
122+
rebar_api:debug("ParsedArgs = ~w", [ParsedArgs]),
108123
RebarOpts = atomvm_rebar3_plugin:get_atomvm_rebar_provider_config(State, ?PROVIDER),
109124
ParsedOpts = atomvm_rebar3_plugin:proplist_to_map(ParsedArgs),
110125
maps:merge(
@@ -127,39 +142,86 @@ env_opts() ->
127142
"ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_PORT",
128143
maps:get(port, ?DEFAULT_OPTS)
129144
),
130-
baud => maybe_convert_string(
145+
baud => integer_from_string(
131146
os:getenv(
132147
"ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_BAUD",
133148
maps:get(baud, ?DEFAULT_OPTS)
134149
)
135150
),
136-
offset => os:getenv(
137-
"ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_OFFSET",
138-
maps:get(offset, ?DEFAULT_OPTS)
151+
offset => integer_from_string(
152+
os:getenv(
153+
"ATOMVM_REBAR3_PLUGIN_ESP32_FLASH_OFFSET",
154+
maps:get(offset, ?DEFAULT_OPTS)
155+
)
156+
),
157+
app_partition => os:getenv(
158+
"ATOMVM_REBAR3_PLUGIN_ESP32_APP_PARTITION",
159+
maps:get(app_partition, ?DEFAULT_OPTS)
139160
)
140161
}.
141162

142163
%% @private
143-
maybe_convert_string(S) when is_list(S) ->
144-
list_to_integer(S);
145-
maybe_convert_string(I) ->
146-
I.
164+
-spec integer_from_string(S :: list() | integer()) -> Value :: integer().
165+
integer_from_string("auto") ->
166+
-1;
167+
integer_from_string(I) when is_integer(I) ->
168+
I;
169+
integer_from_string(S) when is_list(S) ->
170+
S0 = string:trim(S),
171+
case lists:prefix("0x", S0) of
172+
true ->
173+
list_to_integer(lists:subtract(S0, "0x"), 16);
174+
false ->
175+
list_to_integer(S0)
176+
end.
147177

148178
%% @private
149-
do_flash(ProjectApps, EspTool, Chip, Port, Baud, Offset) ->
179+
do_flash(ProjectApps, EspTool, Chip, Port, Baud, Offset, Partition, State) ->
180+
TempFile = get_part_tempfile(State),
181+
Address =
182+
case Offset of
183+
-1 ->
184+
try
185+
read_flash_offset(EspTool, Port, Partition, TempFile)
186+
catch
187+
error:{partition_not_found, Partition} ->
188+
rebar_api:error("Partition ~s not found!", [Partition]);
189+
error:invalid_partition_table ->
190+
rebar_api:error(
191+
"Error parsing partition table, possible corruption during transfer", []
192+
)
193+
end;
194+
Val ->
195+
Offset0 = read_flash_offset(EspTool, Port, Partition, TempFile),
196+
case Val =:= Offset0 of
197+
true ->
198+
Offset0;
199+
false ->
200+
{ok, Offset1} = validate_offset(EspTool, Port, Val, Partition, TempFile),
201+
Offset1
202+
end
203+
end,
150204
[ProjectAppAVM | _] = [get_avm_file(ProjectApp) || ProjectApp <- ProjectApps],
151205
Portparam =
152206
case Port of
153207
"auto" -> "";
154-
_ -> ["--port ", Port]
208+
_ -> io_lib:format("--port ~s", [Port])
209+
end,
210+
Baudparam =
211+
case Baud of
212+
-1 -> "";
213+
_ -> io_lib:format("--baud ~w", [Baud])
214+
end,
215+
Chipparam =
216+
case Chip of
217+
"auto" -> "";
218+
_ -> io_lib:format("--chip ~s", [Chip])
155219
end,
156220
Cmd = lists:join(" ", [
157221
EspTool,
158-
"--chip",
159-
Chip,
222+
Chipparam,
160223
Portparam,
161-
"--baud",
162-
integer_to_list(Baud),
224+
Baudparam,
163225
"--before",
164226
"default_reset",
165227
"--after",
@@ -172,9 +234,12 @@ do_flash(ProjectApps, EspTool, Chip, Port, Baud, Offset) ->
172234
"keep",
173235
"--flash_size",
174236
"detect",
175-
Offset,
237+
"0x" ++ integer_to_list(Address, 16),
176238
ProjectAppAVM
177239
]),
240+
AVMApp = filename:basename(ProjectAppAVM),
241+
rebar_api:info("Flashing ~s to device.", [AVMApp]),
242+
%% The following log output is parsed by the tests and should not be changed or removed.
178243
rebar_api:info("~s~n", [Cmd]),
179244
rebar_api:console("~s", [os:cmd(Cmd)]),
180245
ok.
@@ -185,3 +250,77 @@ get_avm_file(App) ->
185250
Name = binary_to_list(rebar_app_info:name(App)),
186251
DirName = filename:dirname(OutDir),
187252
filename:join(DirName, Name ++ ".avm").
253+
254+
%% @private
255+
read_flash_offset(Esptool, Port, PartName, TempFile) ->
256+
rebar_api:info("Reading application partition offset from device...", []),
257+
try esp_part_dump:read_app_offset(Esptool, Port, PartName, TempFile) of
258+
Offset ->
259+
Offset
260+
catch
261+
error:invalid_partition_table ->
262+
rebar_api:error("Invalid partition data!", []);
263+
error:no_device ->
264+
rebar_api:error("No ESP32 device attached!", []);
265+
error:{partition_not_found, Partition} ->
266+
rebar_api:error("The partition ~s was not found in device partition table!", [Partition]),
267+
rebar_api:error(
268+
"When using a custom partition table always specify the 'app_partition' NAME.", []
269+
);
270+
error:{invalid_subtype, Type} ->
271+
rebar_api:error("The partition ~s was found, but used invalid subtype ~w.", [
272+
PartName, Type
273+
]);
274+
error:invalid_partition_type ->
275+
rebar_api:error("The partition ~s is not a data partition!~nOnly data partitions may be used for AtomVM applications", [
276+
PartName
277+
]);
278+
error:corrupt_partition_data ->
279+
rebar_api:error("Fatal error! Corrupt partition table data!", []);
280+
error:no_data_partitions ->
281+
rebar_api:error("The partition table on the device contains no data partitions");
282+
error:Error ->
283+
rebar_api:error("Unexpected error reading partition table from device, ~p.", [Error])
284+
end.
285+
286+
validate_offset(Esptool, Port, Offset, Partition, TempFile) ->
287+
rebar_api:debug("Checking that offset aligns with partition from table on device...", []),
288+
try esp_part_dump:partition_at_offset(Esptool, Port, Offset, TempFile) of
289+
{error, {Name, Reason}} ->
290+
rebar_api:error(
291+
"The configured offset 0x~.16B contains partition ~s, which cannot be used for reason ~p.~n",
292+
[Offset, Name, Reason]
293+
);
294+
{Found, _Type} ->
295+
rebar_api:warn(
296+
"The discovered partition ~s at offset 0x~.16B will be used, not the expected partition named ~s.",
297+
[Found, Offset, Partition]
298+
),
299+
{ok, Offset}
300+
catch
301+
_:no_device ->
302+
rebar_api:error("No ESP32 device attached!", []);
303+
_:corrupt_partition_data ->
304+
rebar_api:error("Fatal error! Corrupt partition table data!", []);
305+
C:E:S ->
306+
rebar_api:debug("===== Crash Stacktrace:~n~p~n", [S]),
307+
rebar_api:error(
308+
"Unexpected error reading partition table from device. Class = ~p, Error = ~p.", [
309+
C, E
310+
]
311+
)
312+
end.
313+
314+
%% @private
315+
get_part_tempfile(State) ->
316+
OutDir = filename:absname(rebar_dir:base_dir(State)),
317+
TempFile = filename:absname_join(OutDir, "part.tmp"),
318+
case filelib:is_file(TempFile) of
319+
true ->
320+
rebar_api:debug("Removing possibly stale partition dump data ~s", [TempFile]),
321+
Cmd = lists:join(" ", ["rm", TempFile]),
322+
os:cmd(Cmd);
323+
false ->
324+
ok
325+
end,
326+
TempFile.

0 commit comments

Comments
 (0)