1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

ztimer: periph_rtt: ensure RTT_MAX_VALUE is honored

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.
This commit is contained in:
Kaspar Schleiser 2020-04-22 10:02:17 +02:00
parent 3e922f878a
commit d8c129fc47

View File

@ -19,6 +19,7 @@
*
* @}
*/
#include "assert.h"
#include "periph/rtt.h"
#include "ztimer/periph_rtt.h"
@ -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);
}