Verified Commit a1567d59 authored by schneider's avatar schneider Committed by Rahix
Browse files

feat(rtc): Initial Python RTC support

parent d4a24d51
......@@ -57,6 +57,8 @@ typedef unsigned int size_t;
#define API_FILE_SEEK 0x36
#define API_FILE_TELL 0x37
#define API_FILE_STAT 0x38
#define API_RTC_GET_SECONDS 0x40
/* clang-format on */
typedef uint32_t api_int_id_t;
......@@ -518,4 +520,16 @@ typedef struct epic_stat_t {
*/
API(API_FILE_STAT, int epic_file_stat(const char* path, epic_stat_t* stat));
/**
* RTC
* ===
*/
/**
* Read the current RTC value.
*
* :return: Unix time in seconds
*/
API(API_RTC_GET_SECONDS, uint32_t epic_rtc_get_seconds(void));
#endif /* _EPICARDIUM_H */
......@@ -8,4 +8,5 @@ module_sources = files(
'stream.c',
'vibra.c',
'light_sensor.c',
'rtc.c'
)
#include "rtc.h"
#include <stdint.h>
uint32_t epic_rtc_get_seconds(void)
{
return RTC_GetSecond();
}
......@@ -57,11 +57,17 @@ void card10_init(void)
pmic_set_led(2, 0);
TMR_Delay(MXC_TMR0, MSEC(1000), 0);
RTC_EnableRTCE(MXC_RTC);
// Enable 32 kHz output
RTC_SquareWave(
MXC_RTC, SQUARE_WAVE_ENABLED, F_32KHZ, NOISE_IMMUNE_MODE, NULL
);
if (RTC_GetSecond() < 1546300800UL) {
while (RTC_Init(MXC_RTC, 1546300800UL, 0, NULL) == E_BUSY)
;
}
// Enable SPI
sys_cfg_spi_t spi17y_master_cfg;
......
......@@ -177,6 +177,7 @@ micropython_additional_sources = files(
'micropython/lib/utils/stdout_helpers.c',
'micropython/lib/utils/pyexec.c',
'micropython/lib/mp-readline/readline.c',
'micropython/lib/timeutils/timeutils.c'
)
micropython_extmod_sources = files(
......
......@@ -21,6 +21,7 @@ Q(ticks_us)
Q(ticks_cpu)
Q(ticks_add)
Q(ticks_diff)
Q(localtime)
/* vibra */
Q(vibra)
......
#include "mxc_delay.h"
#include "epicardium.h"
#include "extmod/utime_mphal.h"
#include "py/mpconfig.h"
#include <sys/time.h>
#include <stdint.h>
// Needs to be after the stdint include ...
#include "lib/timeutils/timeutils.h"
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() - 946684800UL;
} 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),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
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_localtime), MP_ROM_PTR(&time_localtime_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
......
Supports Markdown
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