17
17
#include <haly/nrfy_rtc.h>
18
18
#include <zephyr/irq.h>
19
19
20
+ /* Ensure that selected counter bit width is within its maximum hardware width. */
21
+ BUILD_ASSERT (CONFIG_NRF_RTC_COUNTER_BIT_WIDTH <= 24 , "Counter bit width exceeds maximum width." );
22
+
23
+ #if (CONFIG_NRF_RTC_COUNTER_BIT_WIDTH < 24 )
24
+ #define CUSTOM_COUNTER_BIT_WIDTH 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)
@@ -33,7 +43,7 @@ BUILD_ASSERT(CHAN_COUNT <= CHAN_COUNT_MAX, "Not enough compare channels");
33
43
BUILD_ASSERT (DT_NODE_HAS_STATUS (DT_NODELABEL (RTC_LABEL ), disabled ),
34
44
"Counter for RTC1 must be disabled" );
35
45
36
- #define COUNTER_BIT_WIDTH 24U
46
+ #define COUNTER_BIT_WIDTH CONFIG_NRF_RTC_COUNTER_BIT_WIDTH
37
47
#define COUNTER_SPAN BIT(COUNTER_BIT_WIDTH)
38
48
#define COUNTER_MAX (COUNTER_SPAN - 1U)
39
49
#define COUNTER_HALF_SPAN (COUNTER_SPAN / 2U)
@@ -422,6 +432,12 @@ uint64_t z_nrf_rtc_timer_read(void)
422
432
423
433
uint32_t cntr = counter ();
424
434
435
+ #if CUSTOM_COUNTER_BIT_WIDTH
436
+ if ((cntr == COUNTER_MAX ) && (val > anchor )) {
437
+ cntr = 0 ;
438
+ }
439
+ #endif
440
+
425
441
val += cntr ;
426
442
427
443
if (cntr < OVERFLOW_RISK_RANGE_END ) {
@@ -560,8 +576,13 @@ void rtc_nrf_isr(const void *arg)
560
576
rtc_pretick_rtc1_isr_hook ();
561
577
}
562
578
563
- if (nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_OVERFLOW_MASK ) &&
564
- nrfy_rtc_events_process (RTC , NRF_RTC_INT_OVERFLOW_MASK )) {
579
+ if ((nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_OVERFLOW_MASK ) &&
580
+ nrfy_rtc_events_process (RTC , NRF_RTC_INT_OVERFLOW_MASK )) ||
581
+ #if CUSTOM_COUNTER_BIT_WIDTH
582
+ (nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_COMPARE1_MASK ) &&
583
+ nrfy_rtc_events_process (RTC , NRF_RTC_INT_COMPARE1_MASK )) ||
584
+ #endif
585
+ 0 ) {
565
586
overflow_cnt ++ ;
566
587
}
567
588
@@ -697,7 +718,9 @@ uint32_t sys_clock_cycle_get_32(void)
697
718
static void int_event_disable_rtc (void )
698
719
{
699
720
uint32_t mask = NRF_RTC_INT_TICK_MASK |
721
+ #if !CUSTOM_COUNTER_BIT_WIDTH
700
722
NRF_RTC_INT_OVERFLOW_MASK |
723
+ #endif
701
724
NRF_RTC_INT_COMPARE0_MASK |
702
725
NRF_RTC_INT_COMPARE1_MASK |
703
726
NRF_RTC_INT_COMPARE2_MASK |
@@ -729,7 +752,9 @@ static int sys_clock_driver_init(void)
729
752
nrfy_rtc_int_enable (RTC , NRF_RTC_CHANNEL_INT_MASK (chan ));
730
753
}
731
754
755
+ #if !CUSTOM_COUNTER_BIT_WIDTH
732
756
nrfy_rtc_int_enable (RTC , NRF_RTC_INT_OVERFLOW_MASK );
757
+ #endif
733
758
734
759
NVIC_ClearPendingIRQ (RTC_IRQn );
735
760
@@ -742,7 +767,7 @@ static int sys_clock_driver_init(void)
742
767
743
768
int_mask = BIT_MASK (CHAN_COUNT );
744
769
if (CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT ) {
745
- alloc_mask = BIT_MASK (EXT_CHAN_COUNT ) << 1 ;
770
+ alloc_mask = BIT_MASK (EXT_CHAN_COUNT ) << ( 1 + CUSTOM_COUNTER_BIT_WIDTH ) ;
746
771
}
747
772
748
773
uint32_t initial_timeout = IS_ENABLED (CONFIG_TICKLESS_KERNEL ) ?
@@ -761,6 +786,28 @@ static int sys_clock_driver_init(void)
761
786
z_nrf_clock_control_lf_on (mode );
762
787
#endif
763
788
789
+ #if CUSTOM_COUNTER_BIT_WIDTH
790
+ /* Use channel 1 for wrapping. */
791
+ uint8_t chan = 1 ;
792
+ nrf_rtc_event_t evt = NRF_RTC_CHANNEL_EVENT_ADDR (chan );
793
+ nrfx_err_t result ;
794
+ nrf_ppi_channel_t ch ;
795
+
796
+ nrfy_rtc_event_enable (RTC , NRF_RTC_CHANNEL_INT_MASK (chan ));
797
+ nrfy_rtc_cc_set (RTC , chan , COUNTER_MAX );
798
+ uint32_t evt_addr ;
799
+ uint32_t task_addr ;
800
+
801
+ evt_addr = nrfy_rtc_event_address_get (RTC , evt );
802
+ task_addr = nrfy_rtc_task_address_get (RTC , NRF_RTC_TASK_CLEAR );
803
+
804
+ result = nrfx_ppi_channel_alloc (& ch );
805
+ if (result != NRFX_SUCCESS ) {
806
+ return - ENODEV ;
807
+ }
808
+ (void )nrfx_ppi_channel_assign (ch , evt_addr , task_addr );
809
+ (void )nrfx_ppi_channel_enable (ch );
810
+ #endif
764
811
return 0 ;
765
812
}
766
813
0 commit comments