|
| 1 | +# |
| 2 | +# This file is part of AtomVM. |
| 3 | +# |
| 4 | +# Copyright 2026 Masatoshi Nishiguchi |
| 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 | +defmodule EspRtcMemory do |
| 22 | + @moduledoc """ |
| 23 | + Demonstrates ESP32 RTC slow memory usage by persisting a restart counter. |
| 24 | + """ |
| 25 | + |
| 26 | + @compile {:no_warn_undefined, :atomvm} |
| 27 | + @compile {:no_warn_undefined, :esp} |
| 28 | + |
| 29 | + @restart_delay_ms 10_000 |
| 30 | + |
| 31 | + defstruct count: 0 |
| 32 | + |
| 33 | + def start do |
| 34 | + case verify_platform(:atomvm.platform()) do |
| 35 | + :ok -> |
| 36 | + load_state() |
| 37 | + |> increment_state() |
| 38 | + |> save_state() |
| 39 | + |
| 40 | + IO.puts("Restarting in 10 seconds ...") |
| 41 | + Process.sleep(@restart_delay_ms) |
| 42 | + :esp.restart() |
| 43 | + |
| 44 | + error -> |
| 45 | + error |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + defp load_state do |
| 50 | + IO.puts("Loading count from RTC slow memory ...") |
| 51 | + |
| 52 | + encoded_term = |
| 53 | + try do |
| 54 | + :esp.rtc_slow_get_binary() |
| 55 | + catch |
| 56 | + :error, :badarg -> |
| 57 | + IO.puts("This device has not recorded the number of starts. Initializing to 0.") |
| 58 | + :erlang.term_to_binary(%__MODULE__{}) |
| 59 | + end |
| 60 | + |
| 61 | + case safe_binary_to_term(encoded_term) do |
| 62 | + {:ok, %__MODULE__{count: count} = state} when is_integer(count) and count >= 0 -> |
| 63 | + state |
| 64 | + |
| 65 | + _ -> |
| 66 | + IO.puts("Error: bad value, resetting to 0.") |
| 67 | + %__MODULE__{} |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + defp safe_binary_to_term(encoded_term) when is_binary(encoded_term) do |
| 72 | + try do |
| 73 | + {:ok, :erlang.binary_to_term(encoded_term)} |
| 74 | + catch |
| 75 | + :error, _ -> |
| 76 | + :error |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + defp save_state(current_state) do |
| 81 | + IO.puts("Saving count to RTC slow memory ...") |
| 82 | + |
| 83 | + current_state |
| 84 | + |> :erlang.term_to_binary() |
| 85 | + |> :esp.rtc_slow_set_binary() |
| 86 | + end |
| 87 | + |
| 88 | + defp increment_state(%__MODULE__{count: count} = state) when is_integer(count) and count >= 0 do |
| 89 | + new_count = count + 1 |
| 90 | + IO.puts("This device has restarted #{new_count} time(s).") |
| 91 | + %__MODULE__{state | count: new_count} |
| 92 | + end |
| 93 | + |
| 94 | + defp increment_state(_) do |
| 95 | + IO.puts("Error: bad value, resetting to 0.") |
| 96 | + %__MODULE__{} |
| 97 | + end |
| 98 | + |
| 99 | + defp verify_platform(:esp32), do: :ok |
| 100 | + defp verify_platform(platform), do: {:error, {:unsupported_platform, platform}} |
| 101 | +end |
| 102 | + |
0 commit comments