diff --git a/cpu/stm32/periph/timer.c b/cpu/stm32/periph/timer.c index edf80488c43d..fb94897eed46 100644 --- a/cpu/stm32/periph/timer.c +++ b/cpu/stm32/periph/timer.c @@ -195,11 +195,13 @@ int timer_set(tim_t tim, int channel, unsigned int timeout) } #endif - /* clear spurious IRQs */ - dev(tim)->SR &= ~(TIM_SR_CC1IF << channel); - TIM_CHAN(tim, channel) = value; + /* clear spurious IRQs + * note: This might also clear the IRQ just set, but that is handled below + * anyway. */ + dev(tim)->SR &= ~(TIM_SR_CC1IF << channel); + /* enable IRQ */ dev(tim)->DIER |= (TIM_DIER_CC1IE << channel); @@ -264,7 +266,13 @@ int timer_clear(tim_t tim, int channel) } unsigned irqstate = irq_disable(); + + /* disable IRQ */ dev(tim)->DIER &= ~(TIM_DIER_CC1IE << channel); + + /* clear spurious IRQs */ + dev(tim)->SR &= ~(TIM_SR_CC1IF << channel); + irq_restore(irqstate); #ifdef MODULE_PERIPH_TIMER_PERIODIC