Skip to content

Commit

Permalink
Merge #19031
Browse files Browse the repository at this point in the history
19031: cpu/stm32/periph_timer: implement timer_set() r=benpicco a=maribu

### Contribution description

The fallback implementation of timer_set() in `drivers/periph_common` is known to fail on short relative sets. This adds a robust implementation.

### Testing procedure

Run `tests/periph_timer_short_relative_set` at least a few dozen times (or use #19030 to have a few dozen repetitions of the test case in a single run of the test application). It should now succeed.

### Issues/PRs references

None

Co-authored-by: Marian Buschsieweke <[email protected]>
  • Loading branch information
bors[bot] and Marian Buschsieweke authored Jan 4, 2023
2 parents 9a45f4b + b8cc222 commit e35c7ad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cpu/stm32/include/periph/cpu_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ extern "C" {
*/
#define TIMER_CHANNEL_NUMOF (4U)

/**
* @brief The driver provides a relative set function
*/
#define PERIPH_TIMER_PROVIDES_SET

/**
* @brief Define a macro for accessing a timer channel
*/
Expand Down
41 changes: 41 additions & 0 deletions cpu/stm32/periph/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "cpu.h"
#include "periph/timer.h"
#include <sys/_intsup.h>

/**
* @brief Interrupt context for each configured timer
Expand Down Expand Up @@ -146,6 +147,46 @@ int timer_set_absolute(tim_t tim, int channel, unsigned int value)
return 0;
}

int timer_set(tim_t tim, int channel, unsigned int timeout)
{
if (channel >= (int)TIMER_CHANNEL_NUMOF) {
return -1;
}

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

/* calculate time till timeout */
value = (value - dev(tim)->CNT) & timer_config[tim].max;

if (value > timeout) {
/* time till timeout is larger than requested --> timer already expired
* ==> let's make sure we have an IRQ pending :) */
dev(tim)->CR1 &= ~(TIM_CR1_CEN);
TIM_CHAN(tim, channel) = dev(tim)->CNT;
dev(tim)->CR1 |= TIM_CR1_CEN;
}

irq_restore(irqstate);

return 0;
}

#ifdef MODULE_PERIPH_TIMER_PERIODIC
int timer_set_periodic(tim_t tim, int channel, unsigned int value, uint8_t flags)
{
Expand Down

0 comments on commit e35c7ad

Please sign in to comment.