Skip to content

Commit caf5c40

Browse files
committed
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.
1 parent 44bb616 commit caf5c40

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

stmhal/timer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,18 +648,18 @@ STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
648648
default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer %d does not exist", tim->tim_id));
649649
}
650650

651+
// set the global variable for interrupt callbacks
652+
if (tim->tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) {
653+
MP_STATE_PORT(pyb_timer_obj_all)[tim->tim_id - 1] = tim;
654+
}
655+
651656
if (n_args > 1 || n_kw > 0) {
652657
// start the peripheral
653658
mp_map_t kw_args;
654659
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
655660
pyb_timer_init_helper(tim, n_args - 1, args + 1, &kw_args);
656661
}
657662

658-
// set the global variable for interrupt callbacks
659-
if (tim->tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) {
660-
MP_STATE_PORT(pyb_timer_obj_all)[tim->tim_id - 1] = tim;
661-
}
662-
663663
return (mp_obj_t)tim;
664664
}
665665

@@ -1043,9 +1043,9 @@ STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback) {
10431043
self->callback = mp_const_none;
10441044
} else if (mp_obj_is_callable(callback)) {
10451045
self->callback = callback;
1046-
HAL_NVIC_EnableIRQ(self->irqn);
10471046
// start timer, so that it interrupts on overflow
10481047
HAL_TIM_Base_Start_IT(&self->tim);
1048+
HAL_NVIC_EnableIRQ(self->irqn);
10491049
} else {
10501050
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "callback must be None or a callable object"));
10511051
}

0 commit comments

Comments
 (0)