Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
rtimer-arch.c
1
/*
2
* Copyright (c) 2026, RISE Research Institutes of Sweden AB
3
* All rights reserved.
4
*
5
* Author: Joakim Eriksson <joakim.eriksson@ri.se>
6
*
7
* SPDX-License-Identifier: BSD-3-Clause
8
*
9
* Minimal rtimer implementation backed by the nRF54L15 GRTC syscounter.
10
*/
11
12
#include "
sys/rtimer.h
"
13
14
/*
15
* nrfx HAL conditionally redefines GRTC_IRQn based on NRF_APPLICATION:
16
* - MDK defines GRTC_IRQn as GRTC_0_IRQn
17
* - HAL redefines to GRTC_2_IRQn when NRF_APPLICATION && !NRF_TRUSTZONE_NONSECURE
18
* We use GRTC instance 0 explicitly in our implementation
19
*/
20
#include "nrfx_grtc.h"
21
22
#include <stdint.h>
23
#include <stdbool.h>
24
25
#define GRTC_IRQ_PRIORITY 6
26
27
static
nrfx_grtc_channel_t rtimer_channel;
28
static
bool
rtimer_channel_active;
29
/*---------------------------------------------------------------------------*/
30
static
void
31
rtimer_grtc_handler(int32_t
id
, uint64_t cc_value,
void
*context)
32
{
33
(void)
id
;
34
(void)cc_value;
35
(void)context;
36
37
rtimer_run_next
();
38
}
39
/*---------------------------------------------------------------------------*/
40
static
void
41
ensure_grtc_started(
void
)
42
{
43
if
(!nrfx_grtc_init_check()) {
44
nrfx_err_t err = nrfx_grtc_init(GRTC_IRQ_PRIORITY);
45
if
(err != NRFX_SUCCESS && err != NRFX_ERROR_ALREADY) {
46
return
;
47
}
48
}
49
}
50
/*---------------------------------------------------------------------------*/
51
void
52
rtimer_arch_init
(
void
)
53
{
54
ensure_grtc_started();
55
56
if
(rtimer_channel_active) {
57
return
;
58
}
59
60
uint8_t channel;
61
if
(nrfx_grtc_channel_alloc(&channel) != NRFX_SUCCESS) {
62
return
;
63
}
64
65
rtimer_channel.channel = channel;
66
rtimer_channel.handler = rtimer_grtc_handler;
67
rtimer_channel.p_context = NULL;
68
69
rtimer_channel_active =
true
;
70
}
71
/*---------------------------------------------------------------------------*/
72
rtimer_clock_t
73
rtimer_arch_now
(
void
)
74
{
75
uint64_t counter;
76
nrfx_grtc_syscounter_get(&counter);
77
return
(rtimer_clock_t)counter;
78
}
79
/*---------------------------------------------------------------------------*/
80
void
81
rtimer_arch_schedule
(rtimer_clock_t t)
82
{
83
if
(!rtimer_channel_active) {
84
return
;
85
}
86
87
uint64_t target = (uint64_t)t;
88
uint64_t now;
89
nrfx_grtc_syscounter_get(&now);
90
91
if
((int64_t)(target - now) <= 0) {
92
rtimer_run_next
();
93
return
;
94
}
95
96
nrfx_grtc_syscounter_cc_absolute_set(&rtimer_channel, target,
true
);
97
}
98
/*---------------------------------------------------------------------------*/
rtimer_arch_init
void rtimer_arch_init(void)
We don't need to explicitly initialise anything but this routine is required by the API.
Definition
rtimer-arch.c:59
rtimer_arch_now
rtimer_clock_t rtimer_arch_now()
Returns the current real-time clock time.
Definition
rtimer-arch.c:109
rtimer_arch_schedule
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition
rtimer-arch.c:65
rtimer_run_next
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition
rtimer.c:78
rtimer.h
Header file for the real-time timer module.
arch
cpu
nrf
nrf54l15
rtimer-arch.c
Generated on
for Contiki-NG by
1.17.0