Skip to content

Commit 867592e

Browse files
committed
Merge pull request #657 from bettio/esp32kernel
ESP32: add support for init / `esp32boot.avm` Init takes care of launching startup module and eventually ALISP console and web server according to configuration / in case of failure. Closes #654 These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents e54a2e1 + 47caf88 commit 867592e

File tree

8 files changed

+488
-10
lines changed

8 files changed

+488
-10
lines changed

libs/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ add_subdirectory(estdlib/src)
2525
add_subdirectory(eavmlib/src)
2626
add_subdirectory(alisp/src)
2727
add_subdirectory(etest/src)
28+
add_subdirectory(esp32boot)
29+
add_subdirectory(esp32devmode/src)
2830

2931
if (Elixir_FOUND)
3032
add_subdirectory(exavmlib/lib)

libs/esp32boot/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# This file is part of AtomVM.
3+
#
4+
# Copyright 2023 Davide Bettio <[email protected]>
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
#
20+
21+
project(esp32boot)
22+
23+
include(BuildErlang)
24+
25+
if (Elixir_FOUND)
26+
pack_runnable(esp32boot esp32init esp32devmode eavmlib estdlib alisp exavmlib)
27+
else()
28+
pack_runnable(esp32boot esp32init esp32devmode eavmlib estdlib alisp)
29+
endif()

libs/esp32boot/esp32init.erl

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2023 Davide Bettio <[email protected]>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(esp32init).
22+
23+
-export([start/0]).
24+
25+
start() ->
26+
console:print(<<"AtomVM init.\n">>),
27+
boot().
28+
29+
is_dev_mode_enabled(SystemStatus) ->
30+
case {SystemStatus, esp:nvs_get_binary(atomvm, dev_mode)} of
31+
{_, <<"always">>} ->
32+
true;
33+
{_, <<"never">>} ->
34+
false;
35+
{ok, undefined} ->
36+
false;
37+
{app_exit, undefined} ->
38+
false;
39+
{app_fail, undefined} ->
40+
true
41+
end.
42+
43+
maybe_start_dev_mode(SystemStatus) ->
44+
case is_dev_mode_enabled(SystemStatus) of
45+
true -> esp32devmode:start_dev_mode();
46+
false -> not_started
47+
end.
48+
49+
% TODO: add support for multiple apps
50+
% /dev/partition/by-name/app1.avm
51+
% /dev/partition/by-name/app2.avm
52+
53+
boot() ->
54+
BootPath = get_boot_path(),
55+
case atomvm:add_avm_pack_file(BootPath, [{name, app}]) of
56+
ok ->
57+
StartModule = get_start_module(),
58+
DevOnExit = is_dev_mode_enabled(app_exit),
59+
StartedDevMode = maybe_start_dev_mode(ok),
60+
61+
io:format("Starting application...~n"),
62+
case DevOnExit of
63+
true ->
64+
try StartModule:start() of
65+
Result -> io:format("Exited: ~p.~n", [Result])
66+
catch
67+
Error -> io:format("Crashed: ~p.~n", [Error])
68+
end,
69+
case StartedDevMode of
70+
started -> ok;
71+
_NotStarted -> maybe_start_dev_mode(app_exit)
72+
end,
73+
timer:sleep(infinity);
74+
false ->
75+
StartModule:start()
76+
end;
77+
{error, Reason} ->
78+
io:format("Failed app start: ~p.~n", [Reason]),
79+
maybe_start_dev_mode(app_fail),
80+
timer:sleep(infinity)
81+
end.
82+
83+
get_boot_path() ->
84+
case esp:nvs_get_binary(atomvm, boot_path) of
85+
undefined ->
86+
"/dev/partition/by-name/main.avm";
87+
Path ->
88+
Path
89+
end.
90+
91+
get_start_module() ->
92+
case esp:nvs_get_binary(atomvm, start_module) of
93+
undefined ->
94+
case atomvm:get_start_beam(app) of
95+
error ->
96+
main;
97+
{ok, ModuleNameWithExt} when is_binary(ModuleNameWithExt) ->
98+
Len = byte_size(ModuleNameWithExt) - byte_size(<<".beam">>),
99+
ModuleName = binary:part(ModuleNameWithExt, 0, Len),
100+
erlang:binary_to_atom(ModuleName, latin1)
101+
end;
102+
Module ->
103+
erlang:binary_to_atom(Module, latin1)
104+
end.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# This file is part of AtomVM.
3+
#
4+
# Copyright 2023 Davide Bettio <[email protected]>
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
#
20+
21+
project(esp32devmode)
22+
23+
include(BuildErlang)
24+
25+
pack_archive(esp32devmode esp32devmode)

0 commit comments

Comments
 (0)