Skip to content

Commit

Permalink
ztimer: periph_rtt: ensure RTT_MAX_VALUE is honored
Browse files Browse the repository at this point in the history
Previously, ztimer would happily set an absolute RTT alarm value that exceeds
RTT's maximum value (though not a longer interval), as the `val` was
simply added to `rtt_get_counter()`.
This commit ensures that the target value wraps around RTT_MAX_VALUE.

Fixes #13920.
  • Loading branch information
kaspar030 committed Apr 23, 2020
1 parent 3e922f8 commit d8c129f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sys/ztimer/periph_rtt.c
Expand Up @@ -19,6 +19,7 @@
*
* @}
*/
#include "assert.h"
#include "periph/rtt.h"
#include "ztimer/periph_rtt.h"

Expand Down Expand Up @@ -50,7 +51,16 @@ static void _ztimer_periph_rtt_set(ztimer_clock_t *clock, uint32_t val)
}

unsigned state = irq_disable();
rtt_set_alarm(rtt_get_counter() + val, _ztimer_periph_rtt_callback, clock);

/* ensure RTT_MAX_VALUE is (2^n - 1) */
static_assert((RTT_MAX_VALUE == UINT32_MAX) ||
!((RTT_MAX_VALUE + 1) & (RTT_MAX_VALUE)),
"RTT_MAX_VALUE needs to be (2^n) - 1");

rtt_set_alarm(
(rtt_get_counter() + val) & RTT_MAX_VALUE, _ztimer_periph_rtt_callback,
clock);

irq_restore(state);
}

Expand Down

0 comments on commit d8c129f

Please sign in to comment.