Contiki-NG
Loading...
Searching...
No Matches
clock-arch.c
1/*
2 * Copyright (c) 2026, RISE Research Institutes of Sweden AB
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include "contiki.h"
9#include "sys/clock.h"
10#include "nrf.h"
11#include <stdint.h>
12
13/* GRTC runs at 1 MHz on nRF54L15 (matches the M33 clock-arch
14 * GRTC_TICK_FREQUENCY_HZ). 1 GRTC tick = 1 us. */
15#define GRTC_TICK_HZ 1000000UL
16
17/* The HAL exposes GRTC_SYSCOUNTER = SYSCOUNTER[NRF_GRTC_DOMAIN_INDEX], where
18 * NRF_GRTC_DOMAIN_INDEX = GRTC_IRQ_GROUP. With NRF_FLPR defined,
19 * GRTC_IRQ_GROUP = 0, so we read SYSCOUNTER[0] - the FLPR's dedicated read
20 * port. The underlying 52-bit counter is shared with the M33. */
21
22static inline uint64_t
23grtc_syscounter_now(void)
24{
25 uint32_t lo, hi;
26 do {
27 lo = NRF_GRTC_S->SYSCOUNTER[0].SYSCOUNTERL;
28 hi = NRF_GRTC_S->SYSCOUNTER[0].SYSCOUNTERH;
29 } while(hi & GRTC_SYSCOUNTER_SYSCOUNTERH_BUSY_Msk);
30 return ((uint64_t)(hi & GRTC_SYSCOUNTER_SYSCOUNTERH_VALUE_Msk) << 32) | lo;
31}
32
33void
35{
36 /* GRTC is already running - the M33 application brings it up before
37 * releasing the VPR. Nothing to do here. */
38}
39
40clock_time_t
42{
43 return (clock_time_t)(grtc_syscounter_now() / (GRTC_TICK_HZ / CLOCK_SECOND));
44}
45
46unsigned long
48{
49 return (unsigned long)(grtc_syscounter_now() / GRTC_TICK_HZ);
50}
51
52void
53clock_wait(clock_time_t t)
54{
55 clock_time_t end = clock_time() + t;
56 while(clock_time() < end) { }
57}
58
59void
60clock_delay_usec(uint16_t us)
61{
62 uint64_t end = grtc_syscounter_now() + us;
63 while(grtc_syscounter_now() < end) { }
64}
65
66void
67clock_delay(unsigned int us)
68{
69 clock_delay_usec((uint16_t)us);
70}
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:105
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition clock-arch.c:98
void clock_init(void)
Initialize the clock library.
Definition clock-arch.c:77
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition clock-arch.c:114
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition clock-arch.c:104
clock_time_t clock_time(void)
Get the current clock time.
Definition clock-arch.c:92
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition clock-arch.c:139