2929
3030#define CHAN_COUNT NRFX_GRTC_CONFIG_NUM_OF_CC_CHANNELS
3131#define EXT_CHAN_COUNT (CHAN_COUNT - 1)
32- /* The reset value of waketime is 1, which doesn't seem to work.
33- * It's being looked into, but for the time being use 4.
34- * Timeout must always be higher than waketime, so setting that to 5.
35- */
36- #define WAKETIME (4)
37- #define TIMEOUT (WAKETIME + 1)
3832
3933#ifndef GRTC_SYSCOUNTERL_VALUE_Msk
4034#define GRTC_SYSCOUNTERL_VALUE_Msk GRTC_SYSCOUNTER_SYSCOUNTERL_VALUE_Msk
5549
5650#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
5751
58- /* The maximum SYSCOUNTERVALID settling time equals 1x32k cycles + 20x1MHz cycles. */
59- #define GRTC_SYSCOUNTERVALID_SETTLE_MAX_TIME_US 51
60-
6152#if defined(CONFIG_TEST )
6253const int32_t z_sys_timer_irq_for_test = DT_IRQN (GRTC_NODE );
6354#endif
@@ -78,36 +69,6 @@ static nrfx_grtc_channel_t system_clock_channel_data = {
7869 __ASSERT_NO_MSG((NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK & (1UL << (chan))) && \
7970 ((chan) != system_clock_channel_data.channel))
8071
81- static inline void grtc_active_set (void )
82- {
83- #if defined(NRF_GRTC_HAS_SYSCOUNTER_ARRAY ) && (NRF_GRTC_HAS_SYSCOUNTER_ARRAY == 1 )
84- nrfy_grtc_sys_counter_active_set (NRF_GRTC , true);
85- while (!nrfy_grtc_sys_conter_ready_check (NRF_GRTC )) {
86- }
87- #else
88- nrfy_grtc_sys_counter_active_state_request_set (NRF_GRTC , true);
89- k_busy_wait (GRTC_SYSCOUNTERVALID_SETTLE_MAX_TIME_US );
90- #endif
91- }
92-
93- static inline void grtc_wakeup (void )
94- {
95- if (IS_ENABLED (CONFIG_NRF_GRTC_SLEEP_ALLOWED )) {
96- grtc_active_set ();
97- }
98- }
99-
100- static inline void grtc_sleep (void )
101- {
102- if (IS_ENABLED (CONFIG_NRF_GRTC_SLEEP_ALLOWED )) {
103- #if defined(NRF_GRTC_HAS_SYSCOUNTER_ARRAY ) && (NRF_GRTC_HAS_SYSCOUNTER_ARRAY == 1 )
104- nrfy_grtc_sys_counter_active_set (NRF_GRTC , false);
105- #else
106- nrfy_grtc_sys_counter_active_state_request_set (NRF_GRTC , false);
107- #endif
108- }
109- }
110-
11172static inline uint64_t counter_sub (uint64_t a , uint64_t b )
11273{
11374 return (a - b );
@@ -116,10 +77,7 @@ static inline uint64_t counter_sub(uint64_t a, uint64_t b)
11677static inline uint64_t counter (void )
11778{
11879 uint64_t now ;
119-
120- grtc_wakeup ();
12180 nrfx_grtc_syscounter_get (& now );
122- grtc_sleep ();
12381 return now ;
12482}
12583
@@ -141,10 +99,8 @@ static inline uint64_t get_comparator(uint32_t chan)
14199static void system_timeout_set (uint64_t value )
142100{
143101 if (value <= NRF_GRTC_SYSCOUNTER_CCADD_MASK ) {
144- grtc_wakeup ();
145102 nrfx_grtc_syscounter_cc_relative_set (& system_clock_channel_data , value , true,
146103 NRFX_GRTC_CC_RELATIVE_SYSCOUNTER );
147- grtc_sleep ();
148104 } else {
149105 nrfx_grtc_syscounter_cc_absolute_set (& system_clock_channel_data , value + counter (),
150106 true);
@@ -373,6 +329,7 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
373329 */
374330
375331 uint64_t capt_time ;
332+ nrfx_err_t result ;
376333
377334 IS_CHANNEL_ALLOWED_ASSERT (chan );
378335
@@ -383,8 +340,10 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
383340 */
384341 return - EBUSY ;
385342 }
386-
387- capt_time = nrfy_grtc_sys_counter_cc_get (NRF_GRTC , chan );
343+ result = nrfx_grtc_syscounter_cc_value_read (chan , & capt_time );
344+ if (result != NRFX_SUCCESS ) {
345+ return - EPERM ;
346+ }
388347
389348 __ASSERT_NO_MSG (capt_time < COUNTER_SPAN );
390349
@@ -399,16 +358,22 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
399358 nrfx_err_t err_code ;
400359 static uint8_t systemoff_channel ;
401360 uint64_t now = counter ();
361+ nrfx_grtc_sleep_config_t sleep_cfg ;
402362 /* Minimum time that ensures valid execution of system-off procedure. */
403- uint32_t minimum_latency_us = nrfy_grtc_waketime_get (NRF_GRTC ) +
404- nrfy_grtc_timeout_get (NRF_GRTC ) +
405- CONFIG_NRF_GRTC_SLEEP_MINIMUM_LATENCY ;
363+ uint32_t minimum_latency_us ;
406364 uint32_t chan ;
407365 int ret ;
408366
367+ nrfx_grtc_sleep_configuration_get (& sleep_cfg );
368+ minimum_latency_us = (sleep_cfg .waketime + sleep_cfg .timeout ) * USEC_PER_SEC / 32768 +
369+ CONFIG_NRF_GRTC_SYSCOUNTER_SLEEP_MINIMUM_LATENCY ;
370+ sleep_cfg .auto_mode = false;
371+ nrfx_grtc_sleep_configure (& sleep_cfg );
372+
409373 if (minimum_latency_us > wake_time_us ) {
410374 return - EINVAL ;
411375 }
376+
412377 k_spinlock_key_t key = k_spin_lock (& lock );
413378
414379 err_code = nrfx_grtc_channel_alloc (& systemoff_channel );
@@ -417,7 +382,9 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
417382 return - ENOMEM ;
418383 }
419384 (void )nrfx_grtc_syscounter_cc_int_disable (systemoff_channel );
420- ret = compare_set (systemoff_channel , now + wake_time_us , NULL , NULL );
385+ ret = compare_set (systemoff_channel ,
386+ now + wake_time_us * sys_clock_hw_cycles_per_sec () / USEC_PER_SEC , NULL ,
387+ NULL );
421388 if (ret < 0 ) {
422389 k_spin_unlock (& lock , key );
423390 return ret ;
@@ -433,7 +400,7 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
433400 }
434401
435402 /* Make sure that wake_time_us was not triggered yet. */
436- if (nrfy_grtc_sys_counter_compare_event_check ( NRF_GRTC , systemoff_channel )) {
403+ if (nrfx_grtc_syscounter_compare_event_check ( systemoff_channel )) {
437404 k_spin_unlock (& lock , key );
438405 return - EINVAL ;
439406 }
@@ -444,7 +411,7 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
444411 MAX_CC_LATCH_WAIT_TIME_US ;
445412 k_busy_wait (wait_time );
446413#if NRF_GRTC_HAS_CLKSEL
447- nrfy_grtc_clksel_set ( NRF_GRTC , NRF_GRTC_CLKSEL_LFXO );
414+ nrfx_grtc_clock_source_set ( NRF_GRTC_CLKSEL_LFXO );
448415#endif
449416 k_spin_unlock (& lock , key );
450417 return 0 ;
@@ -485,16 +452,9 @@ static int sys_clock_driver_init(void)
485452#if defined(CONFIG_NRF_GRTC_TIMER_CLOCK_MANAGEMENT ) && \
486453 (defined(NRF_GRTC_HAS_CLKSEL ) && (NRF_GRTC_HAS_CLKSEL == 1 ))
487454 /* Use System LFCLK as the low-frequency clock source. */
488- nrfy_grtc_clksel_set ( NRF_GRTC , NRF_GRTC_CLKSEL_LFCLK );
455+ nrfx_grtc_clock_source_set ( NRF_GRTC_CLKSEL_LFCLK );
489456#endif
490457
491- #if defined(CONFIG_NRF_GRTC_START_SYSCOUNTER )
492- /* SYSCOUNTER needs to be turned off before initialization. */
493- nrfy_grtc_sys_counter_set (NRF_GRTC , false);
494- nrfy_grtc_timeout_set (NRF_GRTC , TIMEOUT );
495- nrfy_grtc_waketime_set (NRF_GRTC , WAKETIME );
496- #endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
497-
498458 IRQ_CONNECT (DT_IRQN (GRTC_NODE ), DT_IRQ (GRTC_NODE , priority ), nrfx_isr ,
499459 nrfx_grtc_irq_handler , 0 );
500460
@@ -508,20 +468,13 @@ static int sys_clock_driver_init(void)
508468 if (err_code != NRFX_SUCCESS ) {
509469 return err_code == NRFX_ERROR_NO_MEM ? - ENOMEM : - EPERM ;
510470 }
511- if (IS_ENABLED (CONFIG_NRF_GRTC_SLEEP_ALLOWED )) {
512- nrfy_grtc_sys_counter_auto_mode_set (NRF_GRTC , false);
513- }
514471#else
515472 err_code = nrfx_grtc_channel_alloc (& system_clock_channel_data .channel );
516473 if (err_code != NRFX_SUCCESS ) {
517474 return - ENOMEM ;
518475 }
519476#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
520477
521- if (!IS_ENABLED (CONFIG_NRF_GRTC_SLEEP_ALLOWED )) {
522- grtc_active_set ();
523- }
524-
525478 int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK ;
526479 if (!IS_ENABLED (CONFIG_TICKLESS_KERNEL )) {
527480 system_timeout_set (CYC_PER_TICK );
0 commit comments