diff --git a/tests/periph_timer/main.c b/tests/periph_timer/main.c index e703043352..8c1cb8503e 100644 --- a/tests/periph_timer/main.c +++ b/tests/periph_timer/main.c @@ -25,6 +25,8 @@ #include "atomic_utils.h" #include "clk.h" #include "periph/timer.h" +#include "test_utils/expect.h" +#include "time_units.h" /** * @brief Make sure, the maximum number of timers is defined @@ -49,6 +51,15 @@ static void cb(void *arg, int chan) fired++; } +static void cb_not_to_be_executed(void *arg, int chan) +{ + (void)arg; + (void)chan; + + puts("Spurious timer fired"); + expect(0); +} + static int test_timer(unsigned num) { int set = 0; @@ -117,6 +128,29 @@ static int test_timer(unsigned num) } } + /* test for spurious timer IRQs */ + expect(0 == timer_init(TIMER_DEV(num), TIMER_SPEED, cb_not_to_be_executed, NULL)); + + const unsigned duration = 2ULL * US_PER_MS * US_PER_SEC / TIMER_SPEED; + unsigned target = timer_read(TIMER_DEV(num)) + duration; + expect(0 == timer_set_absolute(TIMER_DEV(num), 0, target)); + expect(0 == timer_clear(TIMER_DEV(num), 0)); + while (timer_read(TIMER_DEV(num)) < target) { + /* busy waiting for the timer to reach it timeout. Timer must not fire, + * it was cleared */ + } + + /* checking again to make sure that any IRQ pending bit that may just was + * mask doesn't trigger a timer IRQ on the next set */ + target = timer_read(TIMER_DEV(num)) + duration; + timer_set_absolute(TIMER_DEV(num), 0, target); + timer_clear(TIMER_DEV(num), 0); + + while (timer_read(TIMER_DEV(num)) < target) { + /* busy waiting for the timer to reach it timeout. Timer must not fire, + * it was cleared */ + } + return 1; }