Skip to content

Commit 86b2963

Browse files
committed
Merge pull request #1977 from schnittchen/esp-uptime
Add `esp:timer_get_time/0` 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 6e48612 + e75196a commit 86b2963

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6464
- Added `proc_lib`
6565
- Added gen_server support for timeout tuples in callback return actions introduced in OTP-28.
6666
- Added `sys`
67+
- Added `esp:timer_get_time/0`
6768

6869
### Changed
6970

libs/eavmlib/src/esp.erl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
task_wdt_deinit/0,
6262
task_wdt_add_user/1,
6363
task_wdt_reset_user/1,
64-
task_wdt_delete_user/1
64+
task_wdt_delete_user/1,
65+
timer_get_time/0
6566
]).
6667

6768
-deprecated([
@@ -677,3 +678,13 @@ task_wdt_reset_user(_UserHandle) ->
677678
-spec task_wdt_delete_user(UserHandle :: task_wdt_user_handle()) -> ok | {error, any()}.
678679
task_wdt_delete_user(_UserHandle) ->
679680
erlang:nif_error(undefined).
681+
682+
%%-----------------------------------------------------------------------------
683+
%% @returns integer
684+
%% @doc Get time in microseconds since boot or wakeup from deep sleep
685+
%% Available with ESP-IDF 5.0 or higher.
686+
%% @end
687+
%%-----------------------------------------------------------------------------
688+
-spec timer_get_time() -> integer().
689+
timer_get_time() ->
690+
erlang:nif_error(undefined).

src/platforms/esp32/components/avm_sys/platform_nifs.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "esp_log.h"
3636
#include "esp_mac.h"
37+
#include "esp_timer.h"
3738
#include <esp_partition.h>
3839
#include <esp_sleep.h>
3940
#include <esp_system.h>
@@ -834,6 +835,14 @@ static term nif_esp_task_wdt_delete_user(Context *ctx, int argc, term argv[])
834835
}
835836
#endif
836837

838+
static term nif_esp_timer_get_time(Context *ctx, int argc, term argv[])
839+
{
840+
UNUSED(argv);
841+
UNUSED(argc);
842+
843+
return term_make_maybe_boxed_int64(esp_timer_get_time(), &ctx->heap);
844+
}
845+
837846
//
838847
// NIF structures and dispatch
839848
//
@@ -980,6 +989,12 @@ static const struct Nif esp_task_wdt_delete_user_nif =
980989
};
981990
#endif
982991

992+
static const struct Nif esp_timer_get_time_nif =
993+
{
994+
.base.type = NIFFunctionType,
995+
.nif_ptr = nif_esp_timer_get_time
996+
};
997+
983998
const struct Nif *platform_nifs_get_nif(const char *nifname)
984999
{
9851000
if (strcmp("atomvm:random/0", nifname) == 0) {
@@ -1106,6 +1121,10 @@ const struct Nif *platform_nifs_get_nif(const char *nifname)
11061121
return &esp_task_wdt_delete_user_nif;
11071122
}
11081123
#endif
1124+
if (strcmp("esp:timer_get_time/0", nifname) == 0) {
1125+
TRACE("Resolved platform nif %s ...\n", nifname);
1126+
return &esp_timer_get_time_nif;
1127+
}
11091128
const struct Nif *nif = nif_collection_resolve_nif(nifname);
11101129
if (nif) {
11111130
return nif;

src/platforms/esp32/test/main/test_erl_sources/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function(compile_erlang module_name)
5959
endfunction()
6060

6161
compile_erlang(test_esp_partition)
62+
compile_erlang(test_esp_timer_get_time)
6263
compile_erlang(test_file)
6364
compile_erlang(test_wifi_example)
6465
compile_erlang(test_list_to_atom)
@@ -78,6 +79,7 @@ compile_erlang(test_tz)
7879

7980
set(erlang_test_beams
8081
test_esp_partition.beam
82+
test_esp_timer_get_time.beam
8183
test_file.beam
8284
test_wifi_example.beam
8385
test_list_to_atom.beam
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2025 schnittchen <schnittchen@das-labor.org>
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(test_esp_timer_get_time).
22+
23+
-export([start/0]).
24+
25+
start() ->
26+
T = esp:timer_get_time(),
27+
test_non_neg_int(T).
28+
29+
test_non_neg_int(X) when is_integer(X) andalso X >= 0 ->
30+
ok;
31+
test_non_neg_int(_X) ->
32+
error.

src/platforms/esp32/test/main/test_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ TEST_CASE("test_esp_partition", "[test_run]")
186186
TEST_ASSERT(term_to_int(ret_value) == 0);
187187
}
188188

189+
TEST_CASE("test_esp_timer_get_time", "[test_run]")
190+
{
191+
term ret_value = avm_test_case("test_esp_timer_get_time.beam");
192+
TEST_ASSERT(ret_value == OK_ATOM);
193+
}
194+
189195
// SDMMC works all esp-idf versions for esp32 - still no support c3.
190196
// only run in QEMU (eg. OPENETH configured)
191197
#if !CONFIG_IDF_TARGET_ESP32C3 && CONFIG_ETH_USE_OPENETH

0 commit comments

Comments
 (0)