Skip to content

Commit b6f419e

Browse files
committed
Add esp.timer_get_time
Signed-off-by: schnittchen <[email protected]>
1 parent d4877b9 commit b6f419e

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: 18 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,13 @@ 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(argc);
841+
842+
return term_make_maybe_boxed_int64(esp_timer_get_time(), &ctx->heap);
843+
}
844+
837845
//
838846
// NIF structures and dispatch
839847
//
@@ -980,6 +988,12 @@ static const struct Nif esp_task_wdt_delete_user_nif =
980988
};
981989
#endif
982990

991+
static const struct Nif esp_timer_get_time_nif =
992+
{
993+
.base.type = NIFFunctionType,
994+
.nif_ptr = nif_esp_timer_get_time
995+
};
996+
983997
const struct Nif *platform_nifs_get_nif(const char *nifname)
984998
{
985999
if (strcmp("atomvm:random/0", nifname) == 0) {
@@ -1106,6 +1120,10 @@ const struct Nif *platform_nifs_get_nif(const char *nifname)
11061120
return &esp_task_wdt_delete_user_nif;
11071121
}
11081122
#endif
1123+
if (strcmp("esp:timer_get_time/0", nifname) == 0) {
1124+
TRACE("Resolved platform nif %s ...\n", nifname);
1125+
return &esp_timer_get_time_nif;
1126+
}
11091127
const struct Nif *nif = nif_collection_resolve_nif(nifname);
11101128
if (nif) {
11111129
return nif;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function(compile_erlang module_name)
3838
endfunction()
3939

4040
compile_erlang(test_esp_partition)
41+
compile_erlang(test_esp_timer_get_time)
4142
compile_erlang(test_file)
4243
compile_erlang(test_wifi_example)
4344
compile_erlang(test_list_to_atom)
@@ -60,6 +61,7 @@ add_custom_command(
6061
COMMAND HostAtomVM-prefix/src/HostAtomVM-build/tools/packbeam/PackBEAM -i esp32_test_modules.avm
6162
HostAtomVM-prefix/src/HostAtomVM-build/libs/atomvmlib.avm
6263
test_esp_partition.beam
64+
test_esp_timer_get_time.beam
6365
test_file.beam
6466
test_wifi_example.beam
6567
test_list_to_atom.beam
@@ -79,6 +81,7 @@ add_custom_command(
7981
DEPENDS
8082
HostAtomVM
8183
"${CMAKE_CURRENT_BINARY_DIR}/test_esp_partition.beam"
84+
"${CMAKE_CURRENT_BINARY_DIR}/test_esp_timer_get_time.beam"
8285
"${CMAKE_CURRENT_BINARY_DIR}/test_wifi_example.beam"
8386
"${CMAKE_CURRENT_BINARY_DIR}/test_file.beam"
8487
"${CMAKE_CURRENT_BINARY_DIR}/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 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(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
@@ -180,6 +180,12 @@ TEST_CASE("test_esp_partition", "[test_run]")
180180
TEST_ASSERT(term_to_int(ret_value) == 0);
181181
}
182182

183+
TEST_CASE("test_esp_timer_get_time", "[test_run]")
184+
{
185+
term ret_value = avm_test_case("test_esp_timer_get_time.beam");
186+
TEST_ASSERT(ret_value == OK_ATOM);
187+
}
188+
183189
// SDMMC works all esp-idf versions for esp32 - still no support c3.
184190
// only run in QEMU (eg. OPENETH configured)
185191
#if !CONFIG_IDF_TARGET_ESP32C3 && CONFIG_ETH_USE_OPENETH

0 commit comments

Comments
 (0)