17
17
#include <haly/nrfy_rtc.h>
18
18
#include <zephyr/irq.h>
19
19
20
+ #define RTC_BIT_WIDTH 24
21
+
22
+ #if (CONFIG_NRF_RTC_COUNTER_BIT_WIDTH < RTC_BIT_WIDTH )
23
+ #define CUSTOM_COUNTER_BIT_WIDTH 1
24
+ #define WRAP_CH 1
25
+ #include "nrfx_ppi.h"
26
+ #else
27
+ #define CUSTOM_COUNTER_BIT_WIDTH 0
28
+ #endif
29
+
20
30
#define RTC_PRETICK (IS_ENABLED(CONFIG_SOC_NRF53_RTC_PRETICK) && \
21
31
IS_ENABLED(CONFIG_SOC_NRF5340_CPUNET))
22
32
23
33
#define EXT_CHAN_COUNT CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT
24
- #define CHAN_COUNT (EXT_CHAN_COUNT + 1)
34
+ #define CHAN_COUNT (EXT_CHAN_COUNT + 1 + CUSTOM_COUNTER_BIT_WIDTH )
25
35
26
36
#define RTC NRF_RTC1
27
37
#define RTC_IRQn NRFX_IRQ_NUMBER_GET(RTC)
28
38
#define RTC_LABEL rtc1
29
39
#define CHAN_COUNT_MAX (RTC1_CC_NUM - (RTC_PRETICK ? 1 : 0))
40
+ #define SYS_CLOCK_CH 0
30
41
31
42
BUILD_ASSERT (CHAN_COUNT <= CHAN_COUNT_MAX , "Not enough compare channels" );
32
43
/* Ensure that counter driver for RTC1 is not enabled. */
33
44
BUILD_ASSERT (DT_NODE_HAS_STATUS (DT_NODELABEL (RTC_LABEL ), disabled ),
34
45
"Counter for RTC1 must be disabled" );
35
46
36
- #define COUNTER_BIT_WIDTH 24U
47
+ #define COUNTER_BIT_WIDTH CONFIG_NRF_RTC_COUNTER_BIT_WIDTH
37
48
#define COUNTER_SPAN BIT(COUNTER_BIT_WIDTH)
38
49
#define COUNTER_MAX (COUNTER_SPAN - 1U)
39
50
#define COUNTER_HALF_SPAN (COUNTER_SPAN / 2U)
@@ -139,7 +150,7 @@ uint32_t z_nrf_rtc_timer_capture_task_address_get(int32_t chan)
139
150
{
140
151
#if defined(RTC_TASKS_CAPTURE_TASKS_CAPTURE_Msk )
141
152
__ASSERT_NO_MSG (chan >= 0 && chan < CHAN_COUNT );
142
- if (chan == 0 ) {
153
+ if (chan == SYS_CLOCK_CH ) {
143
154
return 0 ;
144
155
}
145
156
@@ -259,6 +270,15 @@ static int set_alarm(int32_t chan, uint32_t req_cc, bool exact)
259
270
*/
260
271
enum { MIN_CYCLES_FROM_NOW = 3 };
261
272
uint32_t cc_val = req_cc ;
273
+
274
+ #if CUSTOM_COUNTER_BIT_WIDTH
275
+ /* If a CC value is 0 when a CLEAR task is set, this will not
276
+ * trigger a COMAPRE event. Need to use 1 instead.
277
+ */
278
+ if (cc_val % COUNTER_MAX == 0 ) {
279
+ cc_val = 1 ;
280
+ }
281
+ #endif
262
282
uint32_t cc_inc = MIN_CYCLES_FROM_NOW ;
263
283
264
284
/* Disable event routing for the channel to avoid getting a COMPARE
@@ -422,6 +442,17 @@ uint64_t z_nrf_rtc_timer_read(void)
422
442
423
443
uint32_t cntr = counter ();
424
444
445
+ #if CUSTOM_COUNTER_BIT_WIDTH
446
+ /* If counter is equal to it maximum value while val is greater
447
+ * than anchor, then we can assume that overflow has been recorded
448
+ * in the overflow_cnt, but clear task has not been triggered yet.
449
+ * Treat counter as if it has been cleared.
450
+ */
451
+ if ((cntr == COUNTER_MAX ) && (val > anchor )) {
452
+ cntr = 0 ;
453
+ }
454
+ #endif
455
+
425
456
val += cntr ;
426
457
427
458
if (cntr < OVERFLOW_RISK_RANGE_END ) {
@@ -560,8 +591,13 @@ void rtc_nrf_isr(const void *arg)
560
591
rtc_pretick_rtc1_isr_hook ();
561
592
}
562
593
594
+ #if CUSTOM_COUNTER_BIT_WIDTH
595
+ if (nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_COMPARE1_MASK ) &&
596
+ nrfy_rtc_events_process (RTC , NRF_RTC_INT_COMPARE1_MASK )) {
597
+ #else
563
598
if (nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_OVERFLOW_MASK ) &&
564
599
nrfy_rtc_events_process (RTC , NRF_RTC_INT_OVERFLOW_MASK )) {
600
+ #endif
565
601
overflow_cnt ++ ;
566
602
}
567
603
@@ -620,7 +656,7 @@ int z_nrf_rtc_timer_trigger_overflow(void)
620
656
uint64_t now = z_nrf_rtc_timer_read ();
621
657
622
658
if (err == 0 ) {
623
- sys_clock_timeout_handler (0 , now , NULL );
659
+ sys_clock_timeout_handler (SYS_CLOCK_CH , now , NULL );
624
660
}
625
661
bail :
626
662
full_int_unlock (mcu_critical_state );
@@ -677,7 +713,7 @@ void sys_clock_set_timeout(int32_t ticks, bool idle)
677
713
678
714
uint64_t target_time = cyc + last_count ;
679
715
680
- compare_set (0 , target_time , sys_clock_timeout_handler , NULL , false);
716
+ compare_set (SYS_CLOCK_CH , target_time , sys_clock_timeout_handler , NULL , false);
681
717
}
682
718
683
719
uint32_t sys_clock_elapsed (void )
@@ -697,7 +733,9 @@ uint32_t sys_clock_cycle_get_32(void)
697
733
static void int_event_disable_rtc (void )
698
734
{
699
735
uint32_t mask = NRF_RTC_INT_TICK_MASK |
736
+ #if !CUSTOM_COUNTER_BIT_WIDTH
700
737
NRF_RTC_INT_OVERFLOW_MASK |
738
+ #endif
701
739
NRF_RTC_INT_COMPARE0_MASK |
702
740
NRF_RTC_INT_COMPARE1_MASK |
703
741
NRF_RTC_INT_COMPARE2_MASK |
@@ -729,7 +767,9 @@ static int sys_clock_driver_init(void)
729
767
nrfy_rtc_int_enable (RTC , NRF_RTC_CHANNEL_INT_MASK (chan ));
730
768
}
731
769
770
+ #if !CUSTOM_COUNTER_BIT_WIDTH
732
771
nrfy_rtc_int_enable (RTC , NRF_RTC_INT_OVERFLOW_MASK );
772
+ #endif
733
773
734
774
NVIC_ClearPendingIRQ (RTC_IRQn );
735
775
@@ -742,13 +782,13 @@ static int sys_clock_driver_init(void)
742
782
743
783
int_mask = BIT_MASK (CHAN_COUNT );
744
784
if (CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT ) {
745
- alloc_mask = BIT_MASK (EXT_CHAN_COUNT ) << 1 ;
785
+ alloc_mask = BIT_MASK (CHAN_COUNT ) & ~ BIT ( SYS_CLOCK_CH ) ;
746
786
}
747
787
748
788
uint32_t initial_timeout = IS_ENABLED (CONFIG_TICKLESS_KERNEL ) ?
749
789
MAX_CYCLES : CYC_PER_TICK ;
750
790
751
- compare_set (0 , initial_timeout , sys_clock_timeout_handler , NULL , false);
791
+ compare_set (SYS_CLOCK_CH , initial_timeout , sys_clock_timeout_handler , NULL , false);
752
792
753
793
#if defined(CONFIG_CLOCK_CONTROL_NRF )
754
794
static const enum nrf_lfclk_start_mode mode =
@@ -761,6 +801,29 @@ static int sys_clock_driver_init(void)
761
801
z_nrf_clock_control_lf_on (mode );
762
802
#endif
763
803
804
+ #if CUSTOM_COUNTER_BIT_WIDTH
805
+ /* WRAP_CH reserved for wrapping. */
806
+ alloc_mask &= ~BIT (WRAP_CH );
807
+
808
+ nrf_rtc_event_t evt = NRF_RTC_CHANNEL_EVENT_ADDR (WRAP_CH );
809
+ nrfx_err_t result ;
810
+ nrf_ppi_channel_t ch ;
811
+
812
+ nrfy_rtc_event_enable (RTC , NRF_RTC_CHANNEL_INT_MASK (WRAP_CH ));
813
+ nrfy_rtc_cc_set (RTC , WRAP_CH , COUNTER_MAX );
814
+ uint32_t evt_addr ;
815
+ uint32_t task_addr ;
816
+
817
+ evt_addr = nrfy_rtc_event_address_get (RTC , evt );
818
+ task_addr = nrfy_rtc_task_address_get (RTC , NRF_RTC_TASK_CLEAR );
819
+
820
+ result = nrfx_ppi_channel_alloc (& ch );
821
+ if (result != NRFX_SUCCESS ) {
822
+ return - ENODEV ;
823
+ }
824
+ (void )nrfx_ppi_channel_assign (ch , evt_addr , task_addr );
825
+ (void )nrfx_ppi_channel_enable (ch );
826
+ #endif
764
827
return 0 ;
765
828
}
766
829
0 commit comments