8000 nrf5/modules: Adding support for periodic RTC callback. · godlygeek/circuitpython@3debb43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3debb43

Browse files
committed
nrf5/modules: Adding support for periodic RTC callback.
1 parent c301cca commit 3debb43

File tree

1 file changed

+17
-1
lines changed
  • nrf5/modules/machine

1 file changed

+17
-1
lines changed

nrf5/modules/machine/rtc.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ typedef struct _machine_rtc_obj_t {
3939
mp_obj_base_t base;
4040
RTC_HandleTypeDef *rtc;
4141
mp_obj_t callback;
42+
mp_int_t period;
43+
mp_int_t type;
4244
} machine_rtc_obj_t;
4345

4446
RTC_HandleTypeDef RTCHandle0 = {.config.p_instance = NULL, .id = 0};
@@ -53,6 +55,11 @@ STATIC void hal_interrupt_handle(NRF_RTC_Type * p_instance) {
5355
if (p_instance == RTC0) {
5456
const machine_rtc_obj_t *self = &machine_rtc_obj[0];
5557
mp_call_function_0(self->callback);
58+
59+
hal_rtc_stop(&self->rtc->config);
60+
if (self->type == 1) {
61+
hal_rtc_start(&self->rtc->config, self->period);
62+
}
5663
} else if (p_instance == RTC1) {
5764
const machine_rtc_obj_t *self = &machine_rtc_obj[1];
5865
mp_call_function_0(self->callback);
@@ -111,7 +118,7 @@ STATIC void rtc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind)
111118
from machine import RTC
112119
def cb():
113120
print("Callback")
114-
r = RTC(0, 8, cb)
121+
r = RTC(0, 8, cb, type=RTC.PERIODIC)
115122
r.start(16)
116123
*/
117124

@@ -120,6 +127,7 @@ STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
120127
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
121128
{ MP_QSTR_frequency, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
122129
{ MP_QSTR_callback, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
130+
{ MP_QSTR_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
123131
};
124132

125133
// parse args
@@ -150,6 +158,8 @@ STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
150158
self->callback = args[2].u_obj;
151159
}
152160

161+
self->type = args[3].u_int;
162+
153163
hal_rtc_init(&self->rtc->config);
154164

155165
return MP_OBJ_FROM_PTR(self);
@@ -163,6 +173,8 @@ STATIC mp_obj_t machine_rtc_start(mp_obj_t self_in, mp_obj_t period_in) {
163173
machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
164174
mp_int_t period = mp_obj_get_int(period_in);
165175

176+
self->period = mp_obj_get_int(period_in);
177+
166178
hal_rtc_start(&self->rtc->config, period);
167179

168180
return mp_const_none;
@@ -185,6 +197,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_stop_obj, machine_rtc_stop);
185197
STATIC const mp_map_elem_t machine_rtc_locals_dict_table[] = {
186198
{ MP_OBJ_NEW_QSTR(MP_QSTR_start), (mp_obj_t)(&machine_rtc_start_obj) },
187199
{ MP_OBJ_NEW_QSTR(MP_QSTR_stop), (mp_obj_t)(&machine_rtc_stop_obj) },
200+
201+
// constants
202+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ONESHOT), MP_OBJ_NEW_SMALL_INT(0) },
203+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERIODIC), MP_OBJ_NEW_SMALL_INT(1) },
188204
};
189205

190206
STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);

0 commit comments

Comments
 (0)
0