summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/timer.c
diff options
context:
space:
mode:
authorDave Hylands <dhylands@gmail.com>2015-03-09 23:44:26 -0700
committerDamien George <damien.p.george@gmail.com>2015-03-20 23:40:50 +0000
commit49d8e5ebaa195b0227aa04332749f7910d72d1ff (patch)
treee19a7dcc0ff2dea8ebd1b6d2bc6f6f0e3125194d /stmhal/timer.c
parent3cc17c69ffd64dca55fe613f6e931585d8893c0d (diff)
downloadmicropython-49d8e5ebaa195b0227aa04332749f7910d72d1ff.tar.gz
micropython-49d8e5ebaa195b0227aa04332749f7910d72d1ff.zip
stmhal: Fix a bug related to unhandled channel interrupts.
This also cleans up spurious interrupts which happen at timer initilaization time.
Diffstat (limited to 'stmhal/timer.c')
-rw-r--r--stmhal/timer.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/stmhal/timer.c b/stmhal/timer.c
index c20e89f4bb..6068526595 100644
--- a/stmhal/timer.c
+++ b/stmhal/timer.c
@@ -988,8 +988,17 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
&& self->tim.Instance != TIM8 ) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "encoder not supported on timer %d", self->tim_id));
}
+
+ // Disable & clear the timer interrupt so that we don't trigger
+ // an interrupt by initializing the timer.
+ __HAL_TIM_DISABLE_IT(&self->tim, TIM_IT_UPDATE);
HAL_TIM_Encoder_Init(&self->tim, &enc_config);
__HAL_TIM_SetCounter(&self->tim, 0);
+ if (self->callback != mp_const_none) {
+ __HAL_TIM_CLEAR_FLAG(&self->tim, TIM_IT_UPDATE);
+ __HAL_TIM_ENABLE_IT(&self->tim, TIM_IT_UPDATE);
+ }
+ HAL_TIM_Encoder_Start(&self->tim, TIM_CHANNEL_ALL);
break;
}
@@ -1095,9 +1104,12 @@ STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback) {
__HAL_TIM_DISABLE_IT(&self->tim, TIM_IT_UPDATE);
self->callback = mp_const_none;
} else if (mp_obj_is_callable(callback)) {
+ __HAL_TIM_DISABLE_IT(&self->tim, TIM_IT_UPDATE);
self->callback = callback;
- // start timer, so that it interrupts on overflow
- HAL_TIM_Base_Start_IT(&self->tim);
+ // start timer, so that it interrupts on overflow, but clear any
+ // pending interrupts which may have been set by initializing it.
+ __HAL_TIM_CLEAR_FLAG(&self->tim, TIM_IT_UPDATE);
+ HAL_TIM_Base_Start_IT(&self->tim); // This will re-enable the IRQ
HAL_NVIC_EnableIRQ(self->irqn);
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "callback must be None or a callable object"));
@@ -1330,7 +1342,7 @@ void timer_irq_handler(uint tim_id) {
// Finally, clear any remaining interrupt sources. Otherwise we'll
// just get called continuously.
- uint32_t unhandled = __HAL_TIM_GET_ITSTATUS(&tim->tim, 0xff & ~handled);
+ uint32_t unhandled = tim->tim.Instance->DIER & 0xff & ~handled;
if (unhandled != 0) {
__HAL_TIM_CLEAR_IT(&tim->tim, unhandled);
printf("Unhandled interrupt SR=0x%02lx (now disabled)\n", unhandled);