stmhal: Fix problem when passing callback= to timer init function.

In particular, make sure that the globals are all initialized
before enabling the interrupt, and also make sure that the timer
interrupt has been initialied before enabling the NVIC.
This commit is contained in:
Dave Hylands 2015-02-22 19:58:51 -08:00
parent 44bb616b53
commit caf5c40c19

View file

@ -648,6 +648,11 @@ STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d does not exist", tim->tim_id));
}
// set the global variable for interrupt callbacks
if (tim->tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) {
MP_STATE_PORT(pyb_timer_obj_all)[tim->tim_id - 1] = tim;
}
if (n_args > 1 || n_kw > 0) {
// start the peripheral
mp_map_t kw_args;
@ -655,11 +660,6 @@ STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
pyb_timer_init_helper(tim, n_args - 1, args + 1, &kw_args);
}
// set the global variable for interrupt callbacks
if (tim->tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) {
MP_STATE_PORT(pyb_timer_obj_all)[tim->tim_id - 1] = tim;
}
return (mp_obj_t)tim;
}
@ -1043,9 +1043,9 @@ STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback) {
self->callback = mp_const_none;
} else if (mp_obj_is_callable(callback)) {
self->callback = callback;
HAL_NVIC_EnableIRQ(self->irqn);
// start timer, so that it interrupts on overflow
HAL_TIM_Base_Start_IT(&self->tim);
HAL_NVIC_EnableIRQ(self->irqn);
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "callback must be None or a callable object"));
}