29
29
30
30
#define CHAN_COUNT NRFX_GRTC_CONFIG_NUM_OF_CC_CHANNELS
31
31
#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)
38
32
39
33
#ifndef GRTC_SYSCOUNTERL_VALUE_Msk
40
34
#define GRTC_SYSCOUNTERL_VALUE_Msk GRTC_SYSCOUNTER_SYSCOUNTERL_VALUE_Msk
55
49
56
50
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
57
51
58
- /* The maximum SYSCOUNTERVALID settling time equals 1x32k cycles + 20x1MHz cycles. */
59
- #define GRTC_SYSCOUNTERVALID_SETTLE_MAX_TIME_US 51
60
-
61
52
#if defined(CONFIG_TEST )
62
53
const int32_t z_sys_timer_irq_for_test = DT_IRQN (GRTC_NODE );
63
54
#endif
@@ -78,36 +69,6 @@ static nrfx_grtc_channel_t system_clock_channel_data = {
78
69
__ASSERT_NO_MSG((NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK & (1UL << (chan))) && \
79
70
((chan) != system_clock_channel_data.channel))
80
71
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
-
111
72
static inline uint64_t counter_sub (uint64_t a , uint64_t b )
112
73
{
113
74
return (a - b );
@@ -116,10 +77,7 @@ static inline uint64_t counter_sub(uint64_t a, uint64_t b)
116
77
static inline uint64_t counter (void )
117
78
{
118
79
uint64_t now ;
119
-
120
- grtc_wakeup ();
121
80
nrfx_grtc_syscounter_get (& now );
122
- grtc_sleep ();
123
81
return now ;
124
82
}
125
83
@@ -141,10 +99,8 @@ static inline uint64_t get_comparator(uint32_t chan)
141
99
static void system_timeout_set (uint64_t value )
142
100
{
143
101
if (value <= NRF_GRTC_SYSCOUNTER_CCADD_MASK ) {
144
- grtc_wakeup ();
145
102
nrfx_grtc_syscounter_cc_relative_set (& system_clock_channel_data , value , true,
146
103
NRFX_GRTC_CC_RELATIVE_SYSCOUNTER );
147
- grtc_sleep ();
148
104
} else {
149
105
nrfx_grtc_syscounter_cc_absolute_set (& system_clock_channel_data , value + counter (),
150
106
true);
@@ -373,6 +329,7 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
373
329
*/
374
330
375
331
uint64_t capt_time ;
332
+ nrfx_err_t result ;
376
333
377
334
IS_CHANNEL_ALLOWED_ASSERT (chan );
378
335
@@ -383,8 +340,10 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
383
340
*/
384
341
return - EBUSY ;
385
342
}
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
+ }
388
347
389
348
__ASSERT_NO_MSG (capt_time < COUNTER_SPAN );
390
349
@@ -399,16 +358,22 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
399
358
nrfx_err_t err_code ;
400
359
static uint8_t systemoff_channel ;
401
360
uint64_t now = counter ();
361
+ nrfx_grtc_sleep_config_t sleep_cfg ;
402
362
/* 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 ;
406
364
uint32_t chan ;
407
365
int ret ;
408
366
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
+
409
373
if (minimum_latency_us > wake_time_us ) {
410
374
return - EINVAL ;
411
375
}
376
+
412
377
k_spinlock_key_t key = k_spin_lock (& lock );
413
378
414
379
err_code = nrfx_grtc_channel_alloc (& systemoff_channel );
@@ -417,7 +382,9 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
417
382
return - ENOMEM ;
418
383
}
419
384
(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 );
421
388
if (ret < 0 ) {
422
389
k_spin_unlock (& lock , key );
423
390
return ret ;
@@ -433,7 +400,7 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
433
400
}
434
401
435
402
/* 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 )) {
437
404
k_spin_unlock (& lock , key );
438
405
return - EINVAL ;
439
406
}
@@ -444,7 +411,7 @@ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
444
411
MAX_CC_LATCH_WAIT_TIME_US ;
445
412
k_busy_wait (wait_time );
446
413
#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 );
448
415
#endif
449
416
k_spin_unlock (& lock , key );
450
417
return 0 ;
@@ -485,16 +452,9 @@ static int sys_clock_driver_init(void)
485
452
#if defined(CONFIG_NRF_GRTC_TIMER_CLOCK_MANAGEMENT ) && \
486
453
(defined(NRF_GRTC_HAS_CLKSEL ) && (NRF_GRTC_HAS_CLKSEL == 1 ))
487
454
/* 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 );
489
456
#endif
490
457
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
-
498
458
IRQ_CONNECT (DT_IRQN (GRTC_NODE ), DT_IRQ (GRTC_NODE , priority ), nrfx_grtc_irq_handler , 0 , 0 );
499
459
500
460
err_code = nrfx_grtc_init (0 );
@@ -507,20 +467,13 @@ static int sys_clock_driver_init(void)
507
467
if (err_code != NRFX_SUCCESS ) {
508
468
return err_code == NRFX_ERROR_NO_MEM ? - ENOMEM : - EPERM ;
509
469
}
510
- if (IS_ENABLED (CONFIG_NRF_GRTC_SLEEP_ALLOWED )) {
511
- nrfy_grtc_sys_counter_auto_mode_set (NRF_GRTC , false);
512
- }
513
470
#else
514
471
err_code = nrfx_grtc_channel_alloc (& system_clock_channel_data .channel );
515
472
if (err_code != NRFX_SUCCESS ) {
516
473
return - ENOMEM ;
517
474
}
518
475
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
519
476
520
- if (!IS_ENABLED (CONFIG_NRF_GRTC_SLEEP_ALLOWED )) {
521
- grtc_active_set ();
522
- }
523
-
524
477
int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK ;
525
478
if (!IS_ENABLED (CONFIG_TICKLESS_KERNEL )) {
526
479
system_timeout_set (CYC_PER_TICK );
0 commit comments