mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge #19031
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:
commit
e35c7adb73
@ -34,6 +34,11 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
#define TIMER_CHANNEL_NUMOF (4U)
|
#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
|
* @brief Define a macro for accessing a timer channel
|
||||||
*/
|
*/
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include "cpu.h"
|
#include "cpu.h"
|
||||||
#include "periph/timer.h"
|
#include "periph/timer.h"
|
||||||
|
#include <sys/_intsup.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Interrupt context for each configured timer
|
* @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;
|
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
|
#ifdef MODULE_PERIPH_TIMER_PERIODIC
|
||||||
int timer_set_periodic(tim_t tim, int channel, unsigned int value, uint8_t flags)
|
int timer_set_periodic(tim_t tim, int channel, unsigned int value, uint8_t flags)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user