Merge pull request #329 from dhylands/exti-cleanup

Remove param from python API of ext_register
This commit is contained in:
Damien George 2014-02-27 09:33:31 +00:00
commit 7ae69c6dc4
3 changed files with 20 additions and 22 deletions

View File

@ -24,8 +24,8 @@
// arbitrary port. So line 0 can map to Px0 where x is A, B, C, ... and // 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, ... // line 1 can map to Px1 where x is A, B, C, ...
// //
// def callback(line, param): // def callback(line):
// print("line =", line, "param =", param) // print("line =", line)
// //
// # Configure the pin as a GPIO input. // # Configure the pin as a GPIO input.
// pin = pyb.Pin.board.X1 // pin = pyb.Pin.board.X1
@ -84,7 +84,7 @@ typedef struct {
typedef struct { typedef struct {
mp_obj_t callback_obj; mp_obj_t callback_obj;
mp_obj_t param_obj; void *param;
EXTIMode_TypeDef mode; EXTIMode_TypeDef mode;
} exti_vector_t; } 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 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; const pin_obj_t *pin = NULL;
uint v_line; 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); SYSCFG_EXTILineConfig(pin->port, v_line);
} }
v->callback_obj = callback_obj; v->callback_obj = callback_obj;
v->param_obj = param_obj; v->param = param;
v->mode = mode; v->mode = mode;
if (v->callback_obj != mp_const_none) { if (v->callback_obj != mp_const_none) {
// The EXTI_Init function isn't atomic. It uses |= and &=. // The EXTI_Init function isn't atomic. It uses |= and &=.
// We use bit band operations to make it atomic. // We use bit band operations to make it atomic.
exti_disable(v_line);
EXTI_EDGE_BB(EXTI_Trigger_Rising, v_line) = EXTI_EDGE_BB(EXTI_Trigger_Rising, v_line) =
trigger == EXTI_Trigger_Rising || trigger == EXTI_Trigger_Rising_Falling; trigger == EXTI_Trigger_Rising || trigger == EXTI_Trigger_Rising_Falling;
EXTI_EDGE_BB(EXTI_Trigger_Falling, v_line) = 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) { 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 // 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); exti_obj_t *self = m_new_obj(exti_obj_t);
self->base.type = type_in; 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 mode_obj = args[1];
mp_obj_t trigger_obj = args[2]; mp_obj_t trigger_obj = args[2];
mp_obj_t callback_obj = args[3]; mp_obj_t callback_obj = args[3];
mp_obj_t param_obj = mp_const_none; self->line = exti_register(line_obj, mode_obj, trigger_obj, callback_obj, NULL);
if (n_args > 4) {
param_obj = args[4];
}
self->line = exti_register(line_obj, mode_obj, trigger_obj, callback_obj, param_obj);
return self; return self;
} }
@ -326,7 +323,7 @@ static const mp_obj_type_t exti_obj_type = {
void exti_init_early(void) { void exti_init_early(void) {
for (exti_vector_t *v = exti_vector; v < &exti_vector[EXTI_NUM_VECTORS]; v++) { for (exti_vector_t *v = exti_vector; v < &exti_vector[EXTI_NUM_VECTORS]; v++) {
v->callback_obj = mp_const_none; v->callback_obj = mp_const_none;
v->param_obj = mp_const_none; v->param = NULL;
v->mode = EXTI_Mode_Interrupt; v->mode = EXTI_Mode_Interrupt;
} }
} }
@ -341,7 +338,7 @@ static void Handle_EXTI_Irq(uint32_t line) {
if (line < EXTI_NUM_VECTORS) { if (line < EXTI_NUM_VECTORS) {
exti_vector_t *v = &exti_vector[line]; exti_vector_t *v = &exti_vector[line];
if (v->callback_obj != mp_const_none) { 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));
} }
} }
} }

View File

@ -16,7 +16,7 @@
void exti_init_early(void); void exti_init_early(void);
void exti_init(mp_obj_t mod); 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_enable(uint line);
void exti_disable(uint line); void exti_disable(uint line);
@ -24,6 +24,6 @@ void exti_swint(uint line);
typedef struct { typedef struct {
mp_obj_t callback; mp_obj_t callback;
mp_obj_t param; void *param;
} exti_t; } exti_t;

View File

@ -19,24 +19,25 @@
// //
// pyb.switch(callback) will register a callback to be called when the user // pyb.switch(callback) will register a callback to be called when the user
// switch is pressed. // switch is pressed.
//
// pyb.switch(None) will remove the callback. // pyb.switch(None) will remove the callback.
// //
// Example: // Example:
// //
// def callback(): // def switch_pressed():
// print("User 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_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) { if (switch_user_callback_obj != mp_const_none) {
rt_call_function_0(switch_user_callback_obj); rt_call_function_0(switch_user_callback_obj);
} }
return mp_const_none; 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) { void switch_init(void) {
switch_user_callback_obj = mp_const_none; 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(EXTI_Mode_Interrupt),
MP_OBJ_NEW_SMALL_INT(USRSW_EXTI_EDGE), MP_OBJ_NEW_SMALL_INT(USRSW_EXTI_EDGE),
(mp_obj_t)&switch_callback_obj, (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)); pyb_gpio_input((mp_obj_t)&USRSW_PIN, MP_OBJ_NEW_SMALL_INT(USRSW_PUPD));
} }