Select Git revision
Forked from
card10 / firmware
911 commits behind the upstream repository.
Ferdinand Bachmann authored
utime.c 4.98 KiB
#include "interrupt.h"
#include "epicardium.h"
#include "mxc_delay.h"
#include "py/mpconfig.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "extmod/utime_mphal.h"
#include <stdint.h>
// Needs to be after the stdint include ...
#include "lib/timeutils/timeutils.h"
/* MicroPython has its epoch at 2000-01-01. Our RTC is in UTC */
#define EPOCH_OFFSET 946684800UL
#define TZONE_OFFSET 7200UL
static mp_obj_t time_set_time(mp_obj_t secs)
{
uint64_t timestamp = mp_obj_get_int(secs) * 1000ULL +
EPOCH_OFFSET * 1000ULL - TZONE_OFFSET * 1000ULL;
epic_rtc_set_milliseconds(timestamp);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(time_set_time_obj, time_set_time);
static mp_obj_t time_set_unix_time(mp_obj_t secs)
{
uint64_t timestamp = mp_obj_get_int(secs) * 1000ULL;
epic_rtc_set_milliseconds(timestamp);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(time_set_unix_time_obj, time_set_unix_time);
static mp_obj_t time_time(void)
{
mp_int_t seconds;
seconds = epic_rtc_get_seconds() - EPOCH_OFFSET + TZONE_OFFSET;
return mp_obj_new_int(seconds);
}
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
static mp_obj_t time_time_ms(void)
{
uint64_t milliseconds;
milliseconds = epic_rtc_get_milliseconds() - EPOCH_OFFSET * 1000ULL +
TZONE_OFFSET * 1000ULL;
return mp_obj_new_int_from_ull(milliseconds);
}
MP_DEFINE_CONST_FUN_OBJ_0(time_time_ms_obj, time_time_ms);
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 = epic_rtc_get_seconds() - EPOCH_OFFSET + TZONE_OFFSET;
} else {
seconds = mp_obj_get_int(args[0]);
}
timeutils_struct_time_t tm;
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),