Zephyr's k_sleep gets stuck on nRF52832

Kimberly Langland <KimL@...>

Hello All,


We were seeing an issue that occasionally, all tasks waiting for a timeout using k_sleep get stuck in the suspended state (while other tasks continue to run normally), and then after about 8 minutes full normal functionality returns.


After digging into this issue, is appears that either the RTC needs an N+3 buffer between the RTC->COUNTER and the RTC->CC value to guarantee to trigger the ISR (instead of an N+2 buffer as documented), or that there is a race condition in the nrf_rtc_timer driver where between the time of reading the RTC->COUNTER, and writing to RTC->CC, RTC->COUNTER has already advanced.


Has anyone else seen this, or know the appropriate fix/workaround?




Detailed description:

* Zephyr Version: 1.14.99

* Running on nR53832


Each time this behavior occurs, the first task in the timeout_list (in the kernel/timeout module) has a timeout time of 1 tick. Reading the RTC1 registers, directly as this occurs, the RTC1->COUNTER value is just above the RCT1->CC[0] value. Normal behavior returns about 8 mins later when the RTC1->COUNTER register passes the RTC1->CC[0] value a second time (this is a 24 bit register clocked at 32kHZ with no prescaler).


In Nordic's documentation for the RTC on nRF52832 it states: "If the COUNTER is N, writing N+2 to a CC register is guaranteed to trigger a COMPARE event at N+2". Zephyrs driver seems to address this case by writing N+2 to the RTC1->CC[0] register (instead on N+1) in the case that a one tick timeout is requested. However in this case we are still not receiving the ISR until the COUNTER has looped around.


I have tried setting the RTC1->CC[0] register instead to N+3 (in the 1 tick timeout case), and have not yet seen the issue reoccur with the following modification to the nrf_rtc_timer driver.


/* dt - is the delta between RTC->COUNTER and RTC->CC[0]

* cyc - is the value to write to RTC->CC[0] */

} else if (dt == 1) {

-    set_comparator(cyc + 1);

+    set_comparator(cyc + 2);





Kimberly Langland
Sr. Firmware Engineer
Product Innovation Center



This email message may contain confidential and privileged information.  Any unauthorized use is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.



Peter A. Bigot

Version 1.14.99 suggests you have a snapshot of the development series leading up to the recent 2.0 release.  The problem you're reporting was fixed in #17845 in upstream Zephyr 2019-08-01.  It may be worth updating to the 2.0 release for your development as many other problems were fixed in the latter part of August.