summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2022-06-09 14:33:31 +0200
committerDamien George <damien@micropython.org>2022-10-06 22:57:11 +1100
commit37449df821a47698779b48e6c101b084cfbdb6c7 (patch)
tree499303384e84e101038c4e59e73cb6afe476a44b
parent7da76639027057245cfee043733193b3265e7a54 (diff)
samd/modutime: Enable time.time() based on systick_ms().
Allowing to set a time and retrieve the time. It is based on systick_ms() with the precision of the MCU clock. Unless that is based on a crystal, the error seen was about 0.5% at room temperature.
-rw-r--r--ports/samd/modutime.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/ports/samd/modutime.c b/ports/samd/modutime.c
index bbeedcfda..a54544e62 100644
--- a/ports/samd/modutime.c
+++ b/ports/samd/modutime.c
@@ -27,6 +27,9 @@
#include "py/runtime.h"
#include "extmod/utime_mphal.h"
#include "shared/timeutils/timeutils.h"
+#include "mphalport.h"
+
+static uint32_t time_offset = 0;
// localtime([secs])
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
@@ -34,9 +37,10 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
mp_int_t seconds;
if (n_args == 0 || args[0] == mp_const_none) {
// seconds = pyb_rtc_get_us_since_epoch() / 1000 / 1000;
- seconds = mp_obj_get_int(args[0]);
+ seconds = mp_hal_ticks_ms_64() / 1000 + time_offset;
} else {
seconds = mp_obj_get_int(args[0]);
+ time_offset = seconds - mp_hal_ticks_ms_64() / 1000;
}
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
mp_obj_t tuple[8] = {
@@ -72,7 +76,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
// time()
STATIC mp_obj_t time_time(void) {
- mp_raise_NotImplementedError("time");
+ return mp_obj_new_int_from_uint(mp_hal_ticks_ms_64() / 1000 + time_offset);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);