1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
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 https://github.com/RIOT-OS/RIOT/pull/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 <marian.buschsieweke@ovgu.de>
This commit is contained in:
bors[bot] 2023-01-04 19:04:26 +00:00 committed by GitHub
commit e35c7adb73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -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
*/

View File

@ -22,6 +22,7 @@
#include "cpu.h"
#include "periph/timer.h"
#include <sys/_intsup.h>
/**
* @brief Interrupt context for each configured timer
@ -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)
{