diff --git a/stm/exti.c b/stm/exti.c index 13269e7286..08a5da6b2a 100644 --- a/stm/exti.c +++ b/stm/exti.c @@ -24,8 +24,8 @@ // arbitrary port. So line 0 can map to Px0 where x is A, B, C, ... and // line 1 can map to Px1 where x is A, B, C, ... // -// def callback(line, param): -// print("line =", line, "param =", param) +// def callback(line): +// print("line =", line) // // # Configure the pin as a GPIO input. // pin = pyb.Pin.board.X1 @@ -84,7 +84,7 @@ typedef struct { typedef struct { mp_obj_t callback_obj; - mp_obj_t param_obj; + void *param; EXTIMode_TypeDef mode; } exti_vector_t; @@ -98,7 +98,9 @@ static const uint8_t nvic_irq_channel[EXTI_NUM_VECTORS] = { OTG_HS_WKUP_IRQn, TAMP_STAMP_IRQn, RTC_WKUP_IRQn }; -uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp_obj_t callback_obj, mp_obj_t param_obj) { +// NOTE: param is for C callers. Python can use closure to get an object bound +// with the function. +uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp_obj_t callback_obj, void *param) { const pin_obj_t *pin = NULL; uint v_line; @@ -145,13 +147,12 @@ uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp SYSCFG_EXTILineConfig(pin->port, v_line); } v->callback_obj = callback_obj; - v->param_obj = param_obj; + v->param = param; v->mode = mode; if (v->callback_obj != mp_const_none) { // The EXTI_Init function isn't atomic. It uses |= and &=. // We use bit band operations to make it atomic. - exti_disable(v_line); EXTI_EDGE_BB(EXTI_Trigger_Rising, v_line) = trigger == EXTI_Trigger_Rising || trigger == EXTI_Trigger_Rising_Falling; EXTI_EDGE_BB(EXTI_Trigger_Falling, v_line) = @@ -276,12 +277,12 @@ static void exti_load_attr(mp_obj_t self_in, qstr attr_qstr, mp_obj_t *dest) { } } -// line_obj = pyb.Exti(pin, mode, trigger, callback, [param]) +// line_obj = pyb.Exti(pin, mode, trigger, callback) static mp_obj_t exti_call(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { // type_in == exti_obj_type - rt_check_nargs(n_args, 4, 5, n_kw, 0); + rt_check_nargs(n_args, 4, 4, n_kw, 0); exti_obj_t *self = m_new_obj(exti_obj_t); self->base.type = type_in; @@ -289,11 +290,7 @@ static mp_obj_t exti_call(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj mp_obj_t mode_obj = args[1]; mp_obj_t trigger_obj = args[2]; mp_obj_t callback_obj = args[3]; - mp_obj_t param_obj = mp_const_none; - if (n_args > 4) { - param_obj = args[4]; - } - self->line = exti_register(line_obj, mode_obj, trigger_obj, callback_obj, param_obj); + self->line = exti_register(line_obj, mode_obj, trigger_obj, callback_obj, NULL); return self; } @@ -326,7 +323,7 @@ static const mp_obj_type_t exti_obj_type = { void exti_init_early(void) { for (exti_vector_t *v = exti_vector; v < &exti_vector[EXTI_NUM_VECTORS]; v++) { v->callback_obj = mp_const_none; - v->param_obj = mp_const_none; + v->param = NULL; v->mode = EXTI_Mode_Interrupt; } } @@ -341,7 +338,7 @@ static void Handle_EXTI_Irq(uint32_t line) { if (line < EXTI_NUM_VECTORS) { exti_vector_t *v = &exti_vector[line]; if (v->callback_obj != mp_const_none) { - rt_call_function_2(v->callback_obj, MP_OBJ_NEW_SMALL_INT(line), v->param_obj); + rt_call_function_1(v->callback_obj, MP_OBJ_NEW_SMALL_INT(line)); } } } diff --git a/stm/exti.h b/stm/exti.h index f5b11d0496..39d49d4af3 100644 --- a/stm/exti.h +++ b/stm/exti.h @@ -16,7 +16,7 @@ void exti_init_early(void); void exti_init(mp_obj_t mod); -uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp_obj_t callback_obj, mp_obj_t param_obj); +uint exti_register(mp_obj_t pin_obj, mp_obj_t mode_obj, mp_obj_t trigger_obj, mp_obj_t callback_obj, void *param); void exti_enable(uint line); void exti_disable(uint line); @@ -24,6 +24,6 @@ void exti_swint(uint line); typedef struct { mp_obj_t callback; - mp_obj_t param; + void *param; } exti_t; diff --git a/stm/usrsw.c b/stm/usrsw.c index 53c6a1470b..98db562e63 100644 --- a/stm/usrsw.c +++ b/stm/usrsw.c @@ -19,24 +19,25 @@ // // pyb.switch(callback) will register a callback to be called when the user // switch is pressed. +// // pyb.switch(None) will remove the callback. // // Example: // -// def callback(): +// def switch_pressed(): // print("User Switch pressed") // -// pyb.switch(callback) +// pyb.switch(switch_pressed) static mp_obj_t switch_user_callback_obj; -static mp_obj_t switch_callback(mp_obj_t line, mp_obj_t param) { +static mp_obj_t switch_callback(mp_obj_t line) { if (switch_user_callback_obj != mp_const_none) { rt_call_function_0(switch_user_callback_obj); } return mp_const_none; } -static MP_DEFINE_CONST_FUN_OBJ_2(switch_callback_obj, switch_callback); +static MP_DEFINE_CONST_FUN_OBJ_1(switch_callback_obj, switch_callback); void switch_init(void) { switch_user_callback_obj = mp_const_none; @@ -44,7 +45,7 @@ void switch_init(void) { MP_OBJ_NEW_SMALL_INT(EXTI_Mode_Interrupt), MP_OBJ_NEW_SMALL_INT(USRSW_EXTI_EDGE), (mp_obj_t)&switch_callback_obj, - mp_const_none); + NULL); pyb_gpio_input((mp_obj_t)&USRSW_PIN, MP_OBJ_NEW_SMALL_INT(USRSW_PUPD)); }