Contiki-NG
Loading...
Searching...
No Matches
nrf_802154_platform_clock.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026, RISE Research Institutes of Sweden AB.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \file
33 * Clock abstraction layer for nrf_802154 on the nRF5340 network core.
34 *
35 * The radio requires the HFCLK to be sourced from the HFXO. On the
36 * nRF5340 (unlike the nRF54L) this is the classic
37 * TASKS_HFCLKSTART / EVENTS_HFCLKSTARTED sequence with no separate
38 * PLL step. The HFXO is pre-started in nrf_802154_clock_init() (with
39 * interrupts enabled) and then kept running, so the library's
40 * hfclk_start() -- which may be called from a critical section --
41 * never has to busy-wait. LFCLK is owned by clock-arch.c (RTC0), so
42 * we never touch it here.
43 */
44/*---------------------------------------------------------------------------*/
45#include "platform/nrf_802154_clock.h"
46#include "hal/nrf_clock.h"
47#include "nrf.h"
48
49#include <stdbool.h>
50
51/*
52 * Keep the HFXO running even while the library asks for it to be stopped
53 * (default). The library may call nrf_802154_clock_hfclk_start() from a
54 * critical section, where the ~360 us blocking HFXO startup would stall
55 * interrupt handling; keeping it running makes start a fast no-op. Set to
56 * 0 to actually stop the HFXO for lower idle power at the cost of that
57 * blocking restart (safe for the RTCs, which run off LFCLK). A proper
58 * async start via the CLOCK IRQ would remove the trade-off.
59 */
60#ifndef NRF_802154_PLATFORM_HFXO_ALWAYS_ON
61#define NRF_802154_PLATFORM_HFXO_ALWAYS_ON 1
62#endif
63
64static volatile bool hfclk_running;
65static volatile bool lfclk_running;
66/*---------------------------------------------------------------------------*/
67static void
68set_constant_latency(bool enable)
69{
70#if defined(POWER_TASKS_CONSTLAT_TASKS_CONSTLAT_Msk) && \
71 defined(POWER_TASKS_LOWPWR_TASKS_LOWPWR_Msk)
72 if(enable) {
73 NRF_POWER->TASKS_CONSTLAT = 1;
74 } else {
75 NRF_POWER->TASKS_LOWPWR = 1;
76 }
77#else
78 (void)enable;
79#endif
80}
81/*---------------------------------------------------------------------------*/
82static void
83hfxo_start_blocking(void)
84{
85 nrf_clock_event_clear(NRF_CLOCK, NRF_CLOCK_EVENT_HFCLKSTARTED);
86 nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTART);
87 while(!nrf_clock_event_check(NRF_CLOCK, NRF_CLOCK_EVENT_HFCLKSTARTED)) {
88 }
89 nrf_clock_event_clear(NRF_CLOCK, NRF_CLOCK_EVENT_HFCLKSTARTED);
90 hfclk_running = true;
91}
92/*---------------------------------------------------------------------------*/
93void
94nrf_802154_clock_init(void)
95{
96 lfclk_running = true; /* LFCLK is started by clock-arch.c for RTC0. */
97 set_constant_latency(false);
98
99 /* Pre-start the HFXO while interrupts are still enabled. */
100 hfxo_start_blocking();
101}
102/*---------------------------------------------------------------------------*/
103void
104nrf_802154_clock_deinit(void)
105{
106 /* Nothing to do. */
107}
108/*---------------------------------------------------------------------------*/
109void
110nrf_802154_clock_hfclk_start(void)
111{
112 /*
113 * The HFXO is pre-started in nrf_802154_clock_init(), so this is normally
114 * a fast path. The library may call this from a critical section, so the
115 * emergency start below should not occur in practice.
116 */
117 if(!hfclk_running) {
118 hfxo_start_blocking();
119 }
120 set_constant_latency(true);
121 nrf_802154_clock_hfclk_ready();
122}
123/*---------------------------------------------------------------------------*/
124void
125nrf_802154_clock_hfclk_stop(void)
126{
127 set_constant_latency(false);
128#if !NRF_802154_PLATFORM_HFXO_ALWAYS_ON
129 nrf_clock_task_trigger(NRF_CLOCK, NRF_CLOCK_TASK_HFCLKSTOP);
130 hfclk_running = false;
131#endif
132 /* With NRF_802154_PLATFORM_HFXO_ALWAYS_ON, the HFXO stays running so
133 * hfclk_start() never has to busy-wait inside a critical section; see
134 * the comment at the definition. */
135}
136/*---------------------------------------------------------------------------*/
137bool
138nrf_802154_clock_hfclk_is_running(void)
139{
140 return hfclk_running;
141}
142/*---------------------------------------------------------------------------*/
143void
144nrf_802154_clock_lfclk_start(void)
145{
146 /* LFCLK is already running (clock-arch.c). Do not touch the CLOCK
147 * peripheral; just report it ready. */
148 lfclk_running = true;
149 nrf_802154_clock_lfclk_ready();
150}
151/*---------------------------------------------------------------------------*/
152void
153nrf_802154_clock_lfclk_stop(void)
154{
155 /* Never stop LFCLK -- the system clock (RTC0) and the lptimer (RTC1)
156 * depend on it. */
157}
158/*---------------------------------------------------------------------------*/
159bool
160nrf_802154_clock_lfclk_is_running(void)
161{
162 return lfclk_running;
163}
164/*---------------------------------------------------------------------------*/