Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: add support for init / esp32boot.avm #657

Merged
merged 7 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ add_subdirectory(estdlib/src)
add_subdirectory(eavmlib/src)
add_subdirectory(alisp/src)
add_subdirectory(etest/src)
add_subdirectory(esp32boot)
add_subdirectory(esp32devmode/src)

if (Elixir_FOUND)
add_subdirectory(exavmlib/lib)
Expand Down
29 changes: 29 additions & 0 deletions libs/esp32boot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# This file is part of AtomVM.
#
# Copyright 2023 Davide Bettio <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

project(esp32boot)

include(BuildErlang)

if (Elixir_FOUND)
pack_runnable(esp32boot esp32init esp32devmode eavmlib estdlib alisp exavmlib)
else()
pack_runnable(esp32boot esp32init esp32devmode eavmlib estdlib alisp)
endif()
104 changes: 104 additions & 0 deletions libs/esp32boot/esp32init.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
%
% This file is part of AtomVM.
%
% Copyright 2023 Davide Bettio <[email protected]>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(esp32init).

-export([start/0]).

start() ->
console:print(<<"AtomVM init.\n">>),
boot().

is_dev_mode_enabled(SystemStatus) ->
case {SystemStatus, esp:nvs_get_binary(atomvm, dev_mode)} of
{_, <<"always">>} ->
true;
{_, <<"never">>} ->
false;
{ok, undefined} ->
false;
{app_exit, undefined} ->
false;
{app_fail, undefined} ->
true
end.

maybe_start_dev_mode(SystemStatus) ->
case is_dev_mode_enabled(SystemStatus) of
true -> esp32devmode:start_dev_mode();
false -> not_started
end.

% TODO: add support for multiple apps
% /dev/partition/by-name/app1.avm
% /dev/partition/by-name/app2.avm

boot() ->
BootPath = get_boot_path(),
case atomvm:add_avm_pack_file(BootPath, [{name, app}]) of
bettio marked this conversation as resolved.
Show resolved Hide resolved
ok ->
StartModule = get_start_module(),
DevOnExit = is_dev_mode_enabled(app_exit),
StartedDevMode = maybe_start_dev_mode(ok),

io:format("Starting application...~n"),
case DevOnExit of
true ->
try StartModule:start() of
Result -> io:format("Exited: ~p.~n", [Result])
catch
Error -> io:format("Crashed: ~p.~n", [Error])
end,
case StartedDevMode of
started -> ok;
_NotStarted -> maybe_start_dev_mode(app_exit)
end,
timer:sleep(infinity);
false ->
StartModule:start()
end;
{error, Reason} ->
io:format("Failed app start: ~p.~n", [Reason]),
maybe_start_dev_mode(app_fail),
timer:sleep(infinity)
end.
fadushin marked this conversation as resolved.
Show resolved Hide resolved

get_boot_path() ->
case esp:nvs_get_binary(atomvm, boot_path) of
undefined ->
"/dev/partition/by-name/main.avm";
Path ->
Path
end.

get_start_module() ->
case esp:nvs_get_binary(atomvm, start_module) of
undefined ->
case atomvm:get_start_beam(app) of
error ->
main;
{ok, ModuleNameWithExt} when is_binary(ModuleNameWithExt) ->
Len = byte_size(ModuleNameWithExt) - byte_size(<<".beam">>),
ModuleName = binary:part(ModuleNameWithExt, 0, Len),
erlang:binary_to_atom(ModuleName, latin1)
end;
Module ->
erlang:binary_to_atom(Module, latin1)
end.
25 changes: 25 additions & 0 deletions libs/esp32devmode/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# This file is part of AtomVM.
#
# Copyright 2023 Davide Bettio <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
#

project(esp32devmode)

include(BuildErlang)

pack_archive(esp32devmode esp32devmode)
Loading