From 989b8c728b296969e6489908b8e94ed23625a1e8 Mon Sep 17 00:00:00 2001 From: yn386 Date: Thu, 1 Sep 2022 18:08:18 +0900 Subject: [PATCH] stm32/timer: Fix use of timer channel callback() method on L4 MCUs. Since L4 HAL version 1.17.0, HAL_TIM_IC_Start_IT() checks whether specified channel of timer is busy or not, which is the case if this function is called more than once without first calling HAL_TIM_IC_Stop_IT(). The fix in this commit is to call the stop function before calling start. The PWM and OC modes have the same issue with the same fix. Fixes issue #8732. --- ports/stm32/timer.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c index 0cef60cb62..518a2e23b4 100644 --- a/ports/stm32/timer.c +++ b/ports/stm32/timer.c @@ -1576,6 +1576,7 @@ STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback) switch (self->mode) { case CHANNEL_MODE_PWM_NORMAL: case CHANNEL_MODE_PWM_INVERTED: + HAL_TIM_PWM_Stop_IT(&self->timer->tim, TIMER_CHANNEL(self)); HAL_TIM_PWM_Start_IT(&self->timer->tim, TIMER_CHANNEL(self)); break; case CHANNEL_MODE_OC_TIMING: @@ -1584,9 +1585,11 @@ STATIC mp_obj_t pyb_timer_channel_callback(mp_obj_t self_in, mp_obj_t callback) case CHANNEL_MODE_OC_TOGGLE: case CHANNEL_MODE_OC_FORCED_ACTIVE: case CHANNEL_MODE_OC_FORCED_INACTIVE: + HAL_TIM_OC_Stop_IT(&self->timer->tim, TIMER_CHANNEL(self)); HAL_TIM_OC_Start_IT(&self->timer->tim, TIMER_CHANNEL(self)); break; case CHANNEL_MODE_IC: + HAL_TIM_IC_Stop_IT(&self->timer->tim, TIMER_CHANNEL(self)); HAL_TIM_IC_Start_IT(&self->timer->tim, TIMER_CHANNEL(self)); break; }