Commit 9ed27465 authored by Hinerk's avatar Hinerk
Browse files

utime method for milliseconds since epoche

parent ea85141f
......@@ -78,6 +78,7 @@ typedef _Bool bool;
#define API_RTC_GET_SECONDS 0x50
#define API_RTC_SCHEDULE_ALARM 0x51
#define API_RTC_SET_MILLISECONDS 0x52
#define API_RTC_GET_MILLISECONDS 0x53
#define API_LEDS_SET 0x60
#define API_LEDS_SET_HSV 0x61
......@@ -1565,6 +1566,13 @@ API(API_FILE_MKDIR, int epic_file_mkdir(const char *dirname));
*/
API(API_RTC_GET_SECONDS, uint32_t epic_rtc_get_seconds(void));
/**
* Read the current RTC value.
*
* :return: Unix time in milliseconds
*/
API(API_RTC_GET_MILLISECONDS, uint64_t epic_rtc_get_milliseconds(void));
/**
* Sets the current RTC time in milliseconds
*/
......
......@@ -25,6 +25,16 @@ uint32_t epic_rtc_get_seconds(void)
return sec;
}
uint64_t epic_rtc_get_milliseconds(void)
{
uint32_t sec, subsec;
while (RTC_GetTime(&sec, &subsec) == E_BUSY) {
vTaskDelay(pdMS_TO_TICKS(4));
}
return subsec * 1000ULL / 4096 + sec * 1000ULL;
}
void epic_rtc_set_milliseconds(uint64_t milliseconds)
{
uint32_t sec, subsec;
......
......@@ -47,6 +47,7 @@ Q(ticks_diff)
Q(localtime)
Q(mktime)
Q(time)
Q(time_ms)
Q(set_time)
Q(set_unix_time)
......
......@@ -42,6 +42,16 @@ static mp_obj_t time_time(void)
}
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 + 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;
......@@ -120,6 +130,7 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_alarm_obj, 1, 2, time_alarm);
static const mp_rom_map_elem_t time_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
{ MP_ROM_QSTR(MP_QSTR_time_ms), MP_ROM_PTR(&time_time_ms_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_time), MP_ROM_PTR(&time_set_time_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_unix_time),
MP_ROM_PTR(&time_set_unix_time_obj) },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment