Skip to content

Commit

Permalink
samd/modutime: Enable time.time() based on systick_ms().
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robert-hh authored and dpgeorge committed Oct 6, 2022
1 parent 7da7663 commit 37449df
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ports/samd/modutime.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@
#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) {
timeutils_struct_time_t tm;
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] = {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 37449df

Please sign in to comment.