Skip to content

Commit 139789b

Browse files
nordic-krchrlubos
authored andcommitted
[nrf fromlist] drivers: timer: nrf_rtc: Fix custom wrapping
Use channel 0 for RTC wrapping. Skip that channel when processing channels. Fix algorithm that prevents setting CC=0 when custom wrapping is used. Upstream PR #: 94259 Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 2158e6b commit 139789b

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

drivers/timer/nrf_rtc_timer.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121

2222
#if (CONFIG_NRF_RTC_COUNTER_BIT_WIDTH < RTC_BIT_WIDTH)
2323
#define CUSTOM_COUNTER_BIT_WIDTH 1
24-
#define WRAP_CH 1
24+
#define WRAP_CH 0
25+
#define SYS_CLOCK_CH 1
2526
#include "nrfx_ppi.h"
2627
#else
2728
#define CUSTOM_COUNTER_BIT_WIDTH 0
29+
#define SYS_CLOCK_CH 0
2830
#endif
2931

3032
#define RTC_PRETICK (IS_ENABLED(CONFIG_SOC_NRF53_RTC_PRETICK) && \
@@ -37,7 +39,6 @@
3739
#define RTC_IRQn NRFX_IRQ_NUMBER_GET(RTC)
3840
#define RTC_LABEL rtc1
3941
#define CHAN_COUNT_MAX (RTC1_CC_NUM - (RTC_PRETICK ? 1 : 0))
40-
#define SYS_CLOCK_CH 0
4142

4243
BUILD_ASSERT(CHAN_COUNT <= CHAN_COUNT_MAX, "Not enough compare channels");
4344
/* Ensure that counter driver for RTC1 is not enabled. */
@@ -275,15 +276,6 @@ static int set_alarm(int32_t chan, uint32_t req_cc, bool exact)
275276
*/
276277
enum { MIN_CYCLES_FROM_NOW = 3 };
277278
uint32_t cc_val = req_cc;
278-
279-
#if CUSTOM_COUNTER_BIT_WIDTH
280-
/* If a CC value is 0 when a CLEAR task is set, this will not
281-
* trigger a COMAPRE event. Need to use 1 instead.
282-
*/
283-
if (cc_val % COUNTER_MAX == 0) {
284-
cc_val = 1;
285-
}
286-
#endif
287279
uint32_t cc_inc = MIN_CYCLES_FROM_NOW;
288280

289281
/* Disable event routing for the channel to avoid getting a COMPARE
@@ -299,6 +291,14 @@ static int set_alarm(int32_t chan, uint32_t req_cc, bool exact)
299291
for (;;) {
300292
uint32_t now;
301293

294+
#if CUSTOM_COUNTER_BIT_WIDTH
295+
/* If a CC value is 0 when a CLEAR task is set, this will not
296+
* trigger a COMAPRE event. Need to use 1 instead.
297+
*/
298+
if ((cc_val & COUNTER_MAX) == 0) {
299+
cc_val = 1;
300+
}
301+
#endif
302302
set_comparator(chan, cc_val);
303303
/* Enable event routing after the required CC value was set.
304304
* Even though the above operation may get repeated (see below),
@@ -597,16 +597,16 @@ void rtc_nrf_isr(const void *arg)
597597
}
598598

599599
#if CUSTOM_COUNTER_BIT_WIDTH
600-
if (nrfy_rtc_int_enable_check(RTC, NRF_RTC_INT_COMPARE1_MASK) &&
601-
nrfy_rtc_events_process(RTC, NRF_RTC_INT_COMPARE1_MASK)) {
600+
if (nrfy_rtc_int_enable_check(RTC, NRF_RTC_CHANNEL_INT_MASK(WRAP_CH)) &&
601+
nrfy_rtc_events_process(RTC, NRF_RTC_CHANNEL_INT_MASK(WRAP_CH))) {
602602
#else
603603
if (nrfy_rtc_int_enable_check(RTC, NRF_RTC_INT_OVERFLOW_MASK) &&
604604
nrfy_rtc_events_process(RTC, NRF_RTC_INT_OVERFLOW_MASK)) {
605605
#endif
606606
overflow_cnt++;
607607
}
608608

609-
for (int32_t chan = 0; chan < CHAN_COUNT; chan++) {
609+
for (int32_t chan = SYS_CLOCK_CH; chan < CHAN_COUNT; chan++) {
610610
process_channel(chan);
611611
}
612612
}

0 commit comments

Comments
 (0)