diff --git a/cpu/stm32/periph/timer.c b/cpu/stm32/periph/timer.c index 35eaab276b..6f5004177a 100644 --- a/cpu/stm32/periph/timer.c +++ b/cpu/stm32/periph/timer.c @@ -128,8 +128,6 @@ int timer_set_absolute(tim_t tim, int channel, unsigned int value) unsigned irqstate = irq_disable(); set_oneshot(tim, channel); - TIM_CHAN(tim, channel) = (value & timer_config[tim].max); - #ifdef MODULE_PERIPH_TIMER_PERIODIC if (dev(tim)->ARR == TIM_CHAN(tim, channel)) { dev(tim)->ARR = timer_config[tim].max; @@ -139,6 +137,8 @@ int timer_set_absolute(tim_t tim, int channel, unsigned int value) /* clear spurious IRQs */ dev(tim)->SR &= ~(TIM_SR_CC1IF << channel); + TIM_CHAN(tim, channel) = (value & timer_config[tim].max); + /* enable IRQ */ dev(tim)->DIER |= (TIM_DIER_CC1IE << channel); irq_restore(irqstate); @@ -148,6 +148,8 @@ int timer_set_absolute(tim_t tim, int channel, unsigned int value) int timer_set(tim_t tim, int channel, unsigned int timeout) { + unsigned value = (dev(tim)->CNT + timeout) & timer_config[tim].max; + if (channel >= (int)TIMER_CHANNEL_NUMOF) { return -1; } @@ -155,21 +157,20 @@ int timer_set(tim_t tim, int channel, unsigned int timeout) unsigned irqstate = irq_disable(); set_oneshot(tim, channel); - /* clear spurious IRQs */ - dev(tim)->SR &= ~(TIM_SR_CC1IF << channel); - - unsigned value = (dev(tim)->CNT + timeout) & timer_config[tim].max; - TIM_CHAN(tim, channel) = value; - - /* enable IRQ */ - dev(tim)->DIER |= (TIM_DIER_CC1IE << channel); - #ifdef MODULE_PERIPH_TIMER_PERIODIC if (dev(tim)->ARR == TIM_CHAN(tim, channel)) { dev(tim)->ARR = timer_config[tim].max; } #endif + /* clear spurious IRQs */ + dev(tim)->SR &= ~(TIM_SR_CC1IF << channel); + + TIM_CHAN(tim, channel) = value; + + /* enable IRQ */ + dev(tim)->DIER |= (TIM_DIER_CC1IE << channel); + /* calculate time till timeout */ value = (value - dev(tim)->CNT) & timer_config[tim].max;