17
17
#include <haly/nrfy_rtc.h>
18
18
#include <zephyr/irq.h>
19
19
20
+ /* Ensure that selected counter bit width is withing 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)
@@ -560,8 +570,13 @@ void rtc_nrf_isr(const void *arg)
560
570
rtc_pretick_rtc1_isr_hook ();
561
571
}
562
572
563
- if (nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_OVERFLOW_MASK ) &&
564
- nrfy_rtc_events_process (RTC , NRF_RTC_INT_OVERFLOW_MASK )) {
573
+ if ((nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_OVERFLOW_MASK ) &&
574
+ nrfy_rtc_events_process (RTC , NRF_RTC_INT_OVERFLOW_MASK )) ||
575
+ #if CUSTOM_COUNTER_BIT_WIDTH
576
+ (nrfy_rtc_int_enable_check (RTC , NRF_RTC_INT_COMPARE1_MASK ) &&
577
+ nrfy_rtc_events_process (RTC , NRF_RTC_INT_COMPARE1_MASK )) ||
578
+ #endif
579
+ 0 ) {
565
580
overflow_cnt ++ ;
566
581
}
567
582
@@ -697,7 +712,9 @@ uint32_t sys_clock_cycle_get_32(void)
697
712
static void int_event_disable_rtc (void )
698
713
{
699
714
uint32_t mask = NRF_RTC_INT_TICK_MASK |
715
+ #if !CUSTOM_COUNTER_BIT_WIDTH
700
716
NRF_RTC_INT_OVERFLOW_MASK |
717
+ #endif
701
718
NRF_RTC_INT_COMPARE0_MASK |
702
719
NRF_RTC_INT_COMPARE1_MASK |
703
720
NRF_RTC_INT_COMPARE2_MASK |
@@ -729,7 +746,9 @@ static int sys_clock_driver_init(void)
729
746
nrfy_rtc_int_enable (RTC , NRF_RTC_CHANNEL_INT_MASK (chan ));
730
747
}
731
748
749
+ #if !CUSTOM_COUNTER_BIT_WIDTH
732
750
nrfy_rtc_int_enable (RTC , NRF_RTC_INT_OVERFLOW_MASK );
751
+ #endif
733
752
734
753
NVIC_ClearPendingIRQ (RTC_IRQn );
735
754
@@ -742,7 +761,7 @@ static int sys_clock_driver_init(void)
742
761
743
762
int_mask = BIT_MASK (CHAN_COUNT );
744
763
if (CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT ) {
745
- alloc_mask = BIT_MASK (EXT_CHAN_COUNT ) << 1 ;
764
+ alloc_mask = BIT_MASK (EXT_CHAN_COUNT ) << ( 1 + CUSTOM_COUNTER_BIT_WIDTH ) ;
746
765
}
747
766
748
767
uint32_t initial_timeout = IS_ENABLED (CONFIG_TICKLESS_KERNEL ) ?
@@ -761,6 +780,28 @@ static int sys_clock_driver_init(void)
761
780
z_nrf_clock_control_lf_on (mode );
762
781
#endif
763
782
783
+ #if CUSTOM_COUNTER_BIT_WIDTH
784
+ /* Use channel 1 for wrapping. */
785
+ uint8_t chan = 1 ;
786
+ nrf_rtc_event_t evt = NRF_RTC_CHANNEL_EVENT_ADDR (chan );
787
+ nrfx_err_t result ;
788
+ nrf_ppi_channel_t ch ;
789
+
790
+ nrfy_rtc_event_enable (RTC , NRF_RTC_CHANNEL_INT_MASK (chan ));
791
+ nrfy_rtc_cc_set (RTC , chan , COUNTER_MAX );
792
+ uint32_t evt_addr ;
793
+ uint32_t task_addr ;
794
+
795
+ evt_addr = nrfy_rtc_event_address_get (RTC , evt );
796
+ task_addr = nrfy_rtc_task_address_get (RTC , NRF_RTC_TASK_CLEAR );
797
+
798
+ result = nrfx_ppi_channel_alloc (& ch );
799
+ if (result != NRFX_SUCCESS ) {
800
+ return - ENODEV ;
801
+ }
802
+ (void )nrfx_ppi_channel_assign (ch , evt_addr , task_addr );
803
+ (void )nrfx_ppi_channel_enable (ch );
804
+ #endif
764
805
return 0 ;
765
806
}
766
807
0 commit comments