summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2025-03-26 09:24:19 +0100
committerDamien George <damien@micropython.org>2025-05-07 16:17:07 +1000
commitff9e01782b7e0f21d8d085bdc4c917a7faf6c17e (patch)
tree4cadf4eb7964d0d7754d4ee7efcedf265cf1c359
parent2fda4bbe05799ccf387051acec600c6ac2e243e2 (diff)
samd/modtime: Change time.time_ns() to follow the RTC time.
That is done by adding the offset to epoch, following the scheme from the RP2 port. RTC and `ticks_us()` are not precisely in sync, and so the difference between `time.time_ns()/1e9` and `time.time()` will increase by more than 9 seconds/24h. So applications should avoid using `time.time()` and `time.time_ns()` in the same context. Signed-off-by: robert-hh <robert@hammelrath.com>
-rw-r--r--ports/samd/machine_rtc.c1
-rw-r--r--ports/samd/main.c2
-rw-r--r--ports/samd/modtime.c14
-rw-r--r--ports/samd/mphalport.h5
4 files changed, 18 insertions, 4 deletions
diff --git a/ports/samd/machine_rtc.c b/ports/samd/machine_rtc.c
index 74c2266d6..4b03488b7 100644
--- a/ports/samd/machine_rtc.c
+++ b/ports/samd/machine_rtc.c
@@ -133,6 +133,7 @@ static mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args,
}
#endif
+ mp_hal_time_ns_set_from_rtc();
return mp_const_none;
}
}
diff --git a/ports/samd/main.c b/ports/samd/main.c
index 2bbaf63e6..a7da95582 100644
--- a/ports/samd/main.c
+++ b/ports/samd/main.c
@@ -28,6 +28,7 @@
#include "py/runtime.h"
#include "py/gc.h"
#include "py/mperrno.h"
+#include "py/mphal.h"
#include "py/stackctrl.h"
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
@@ -45,6 +46,7 @@ extern void sercom_deinit_all(void);
void samd_main(void) {
mp_stack_set_top(&_estack);
mp_stack_set_limit(&_estack - &_sstack - 1024);
+ mp_hal_time_ns_set_from_rtc();
for (;;) {
gc_init(&_sheap, &_eheap);
diff --git a/ports/samd/modtime.c b/ports/samd/modtime.c
index 83072dd16..0bed3cb83 100644
--- a/ports/samd/modtime.c
+++ b/ports/samd/modtime.c
@@ -28,6 +28,8 @@
#include "shared/timeutils/timeutils.h"
#include "modmachine.h"
+static uint64_t time_us_64_offset_from_epoch;
+
// Return the localtime as an 8-tuple.
static mp_obj_t mp_time_localtime_get(void) {
timeutils_struct_time_t tm;
@@ -54,3 +56,15 @@ static mp_obj_t mp_time_time_get(void) {
return mp_obj_new_int_from_uint(timeutils_mktime(
tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec));
}
+
+void mp_hal_time_ns_set_from_rtc(void) {
+ timeutils_struct_time_t tm;
+ rtc_gettime(&tm);
+ uint64_t time_us = (uint64_t)timeutils_mktime(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,
+ tm.tm_min, tm.tm_sec) * 1000000ULL;
+ time_us_64_offset_from_epoch = time_us - mp_hal_ticks_us_64();
+}
+
+uint64_t mp_hal_time_ns(void) {
+ return (time_us_64_offset_from_epoch + mp_hal_ticks_us_64()) * 1000ULL;
+}
diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h
index 6c9e525bb..69e2b6064 100644
--- a/ports/samd/mphalport.h
+++ b/ports/samd/mphalport.h
@@ -47,6 +47,7 @@ extern int mp_interrupt_char;
extern ringbuf_t stdin_ringbuf;
extern volatile uint32_t systick_ms;
uint64_t mp_hal_ticks_us_64(void);
+void mp_hal_time_ns_set_from_rtc(void);
void mp_hal_set_interrupt_char(int c);
@@ -94,10 +95,6 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
}
#endif
-static inline uint64_t mp_hal_time_ns(void) {
- return mp_hal_ticks_us_64() * 1000;
-}
-
// C-level pin HAL
#include "py/obj.h"