From d183f12395bdc3eef17bb1edaed4fd1611ff3230 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 2 Sep 2018 19:16:24 +0200 Subject: [PATCH] nrf: fix sleep For some reason, the old behavior stopped working at some point (maybe at the nrfx update?). Change sleep behavior to be more correct. --- src/runtime/runtime_nrf.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/runtime/runtime_nrf.c b/src/runtime/runtime_nrf.c index ab8bb3fe..2927ac6a 100644 --- a/src/runtime/runtime_nrf.c +++ b/src/runtime/runtime_nrf.c @@ -11,8 +11,13 @@ static volatile bool rtc_wakeup; void rtc_sleep(uint32_t ticks) { NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk; rtc_wakeup = false; - NRF_RTC0->TASKS_CLEAR = 1; - NRF_RTC0->CC[0] = ticks; + if (ticks == 1) { + // Race condition (even in hardware) at ticks == 1. + // TODO: fix this in a better way by detecting it, like the manual + // describes. + ticks = 2; + } + NRF_RTC0->CC[0] = (NRF_RTC0->COUNTER + ticks) & 0x00ffffff; while (!rtc_wakeup) { __WFI(); }