summaryrefslogtreecommitdiffstatshomepage
path: root/esp8266/modpybrtc.c
diff options
context:
space:
mode:
Diffstat (limited to 'esp8266/modpybrtc.c')
-rw-r--r--esp8266/modpybrtc.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/esp8266/modpybrtc.c b/esp8266/modpybrtc.c
index 594c34b157..e62dc88175 100644
--- a/esp8266/modpybrtc.c
+++ b/esp8266/modpybrtc.c
@@ -49,6 +49,10 @@ typedef struct _pyb_rtc_obj_t {
// singleton RTC object
STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}};
+// ALARM0 state
+uint32_t pyb_rtc_alarm0_wake; // see MACHINE_WAKE_xxx constants
+uint64_t pyb_rtc_alarm0_expiry; // in microseconds
+
void mp_hal_rtc_init(void) {
uint32_t magic;
@@ -61,6 +65,10 @@ void mp_hal_rtc_init(void) {
system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal));
system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta));
}
+
+ // reset ALARM0 state
+ pyb_rtc_alarm0_wake = 0;
+ pyb_rtc_alarm0_expiry = 0;
}
STATIC mp_obj_t pyb_rtc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
@@ -177,9 +185,49 @@ STATIC mp_obj_t pyb_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_memory_obj, 1, 2, pyb_rtc_memory);
+STATIC mp_obj_t pyb_rtc_alarm(mp_obj_t self_in, mp_obj_t alarm_id, mp_obj_t time_in) {
+ (void)self_in; // unused
+
+ // check we want alarm0
+ if (mp_obj_get_int(alarm_id) != 0) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm"));
+ }
+
+ // set expiry time (in microseconds)
+ pyb_rtc_alarm0_expiry = pyb_rtc_get_us_since_2000() + mp_obj_get_int(time_in) * 1000;
+
+ return mp_const_none;
+
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_rtc_alarm_obj, pyb_rtc_alarm);
+
+STATIC mp_obj_t pyb_rtc_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_trigger, ARG_wake };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_trigger, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ // check we want alarm0
+ if (args[ARG_trigger].u_int != 0) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid alarm"));
+ }
+
+ // set the wake value
+ pyb_rtc_alarm0_wake = args[ARG_wake].u_int;
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_rtc_irq_obj, 1, pyb_rtc_irq);
+
STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_memory), (mp_obj_t)&pyb_rtc_memory_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_alarm), (mp_obj_t)&pyb_rtc_alarm_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_irq), (mp_obj_t)&pyb_rtc_irq_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_ALARM0), MP_OBJ_NEW_SMALL_INT(0) },
};
STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);