Skip to content

Commit d4b61b0

Browse files
committed
extmod/utime_mphal: Add generic utime.time_ns() function.
It requires mp_hal_time_ns() to be provided by a port. This function allows very accurate absolute timestamps. Enabled on unix, windows, stm32, esp8266 and esp32. Signed-off-by: Damien George <[email protected]>
1 parent 905a18a commit d4b61b0

File tree

9 files changed

+45
-2
lines changed

9 files changed

+45
-2
lines changed

docs/library/utime.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ Functions
216216
function returns number of seconds since a port-specific reference point in time (for
217217
embedded boards without a battery-backed RTC, usually since power up or reset). If you
218218
want to develop portable MicroPython application, you should not rely on this function
219-
to provide higher than second precision. If you need higher precision, use
220-
`ticks_ms()` and `ticks_us()` functions, if you need calendar time,
219+
to provide higher than second precision. If you need higher precision, absolute
220+
timestamps, use `time_ns()`. If relative times are acceptable then use the
221+
`ticks_ms()` and `ticks_us()` functions. If you need calendar time, `gmtime()` or
221222
`localtime()` without an argument is a better choice.
222223

223224
.. admonition:: Difference to CPython
@@ -233,3 +234,8 @@ Functions
233234
hardware also lacks battery-powered RTC, so returns number of seconds
234235
since last power-up or from other relative, hardware-specific point
235236
(e.g. reset).
237+
238+
.. function:: time_ns()
239+
240+
Similar to `time()` but returns nanoseconds since the Epoch, as an integer (usually
241+
a big integer, so will allocate on the heap).

extmod/utime_mphal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,10 @@ STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
9999
}
100100
MP_DEFINE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj, time_ticks_add);
101101

102+
// Returns the number of nanoseconds since the Epoch, as an integer.
103+
STATIC mp_obj_t time_time_ns(void) {
104+
return mp_obj_new_int_from_ull(mp_hal_time_ns());
105+
}
106+
MP_DEFINE_CONST_FUN_OBJ_0(mp_utime_time_ns_obj, time_time_ns);
107+
102108
#endif // MICROPY_PY_UTIME_MP_HAL

extmod/utime_mphal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj);
3737
MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj);
3838
MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj);
3939
MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj);
40+
MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_time_ns_obj);
4041

4142
#endif // MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H

ports/esp32/modutime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
9797
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
9898
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
9999
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
100+
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
100101
};
101102

102103
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

ports/esp8266/modutime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
120120
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
121121
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
122122
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
123+
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
123124
};
124125

125126
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

ports/stm32/modutime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
144144
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
145145
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
146146
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
147+
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
147148
};
148149

149150
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

ports/unix/modtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
219219
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
220220
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
221221
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
222+
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
222223
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mod_time_gmtime_obj) },
223224
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) },
224225
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mod_time_mktime_obj) },

tests/extmod/utime_time_ns.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# test utime.time_ns()
2+
3+
try:
4+
import utime
5+
6+
utime.sleep_us
7+
utime.time_ns
8+
except (ImportError, AttributeError):
9+
print("SKIP")
10+
raise SystemExit
11+
12+
13+
t0 = utime.time_ns()
14+
utime.sleep_us(1000)
15+
t1 = utime.time_ns()
16+
17+
# Check that time_ns increases.
18+
print(t0 < t1)
19+
20+
# Check that time_ns counts correctly, but be very lenient with the upper bound (50ms).
21+
if 950000 < t1 - t0 < 50000000:
22+
print(True)
23+
else:
24+
print(t0, t1, t1 - t0)

tests/extmod/utime_time_ns.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
True
2+
True

0 commit comments

Comments
 (0)