Contiki-NG
Loading...
Searching...
No Matches
nrf_802154_platform_hp_timer.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 * High Precision Timer platform for nrf_802154 on nRF54L15.
10 * Uses TIMER10 and matches Nordic's upstream HP timer contract:
11 * CC[1] = current-time capture, CC[2] = sync capture, CC[3] = event timestamp.
12 */
13
14#include "platform/nrf_802154_hp_timer.h"
15#include "hal/nrf_timer.h"
16#include "nrf.h"
17
18#define HP_TIMER NRF_TIMER10
19
20/* Upstream nrf_802154 HP timer channel layout. */
21#define CAPTURE_CC 1
22#define SYNC_CC 2
23#define TIMESTAMP_CC 3
24
25static uint32_t unexpected_sync;
26/*---------------------------------------------------------------------------*/
27static inline uint32_t
28timer_time_get(void)
29{
30 nrf_timer_task_trigger(HP_TIMER, (nrf_timer_task_t)NRF_TIMER_TASK_CAPTURE1);
31 return nrf_timer_cc_get(HP_TIMER, (nrf_timer_cc_channel_t)CAPTURE_CC);
32}
33/*---------------------------------------------------------------------------*/
34void
35nrf_802154_hp_timer_init(void)
36{
37 nrf_timer_bit_width_set(HP_TIMER, NRF_TIMER_BIT_WIDTH_32);
38 nrf_timer_prescaler_set(HP_TIMER, NRF_TIMER_FREQ_1MHz);
39 nrf_timer_mode_set(HP_TIMER, NRF_TIMER_MODE_TIMER);
40}
41/*---------------------------------------------------------------------------*/
42void
43nrf_802154_hp_timer_deinit(void)
44{
45#if NRF_TIMER_HAS_SHUTDOWN
46 nrf_timer_task_trigger(HP_TIMER, NRF_TIMER_TASK_SHUTDOWN);
47#else
48 HP_TIMER->TASKS_STOP = 1;
49 HP_TIMER->TASKS_CLEAR = 1;
50#endif
51}
52/*---------------------------------------------------------------------------*/
53void
54nrf_802154_hp_timer_start(void)
55{
56 nrf_timer_task_trigger(HP_TIMER, NRF_TIMER_TASK_START);
57}
58/*---------------------------------------------------------------------------*/
59void
60nrf_802154_hp_timer_stop(void)
61{
62#if NRF_TIMER_HAS_SHUTDOWN
63 nrf_timer_task_trigger(HP_TIMER, NRF_TIMER_TASK_SHUTDOWN);
64#else
65 nrf_timer_task_trigger(HP_TIMER, NRF_TIMER_TASK_STOP);
66 nrf_timer_task_trigger(HP_TIMER, NRF_TIMER_TASK_CLEAR);
67#endif
68}
69/*---------------------------------------------------------------------------*/
70uint32_t
71nrf_802154_hp_timer_current_time_get(void)
72{
73 return timer_time_get();
74}
75/*---------------------------------------------------------------------------*/
76uint32_t
77nrf_802154_hp_timer_sync_task_get(void)
78{
79 return nrf_timer_task_address_get(HP_TIMER, (nrf_timer_task_t)NRF_TIMER_TASK_CAPTURE2);
80}
81/*---------------------------------------------------------------------------*/
82void
83nrf_802154_hp_timer_sync_prepare(void)
84{
85 uint32_t past_time = timer_time_get() - 1;
86
87 unexpected_sync = past_time;
88 nrf_timer_cc_set(HP_TIMER, (nrf_timer_cc_channel_t)SYNC_CC, past_time);
89}
90/*---------------------------------------------------------------------------*/
91bool
92nrf_802154_hp_timer_sync_time_get(uint32_t *p_timestamp)
93{
94 uint32_t sync_time = nrf_timer_cc_get(HP_TIMER, (nrf_timer_cc_channel_t)SYNC_CC);
95
96 if(sync_time != unexpected_sync) {
97 *p_timestamp = sync_time;
98 return true;
99 }
100
101 return false;
102}
103/*---------------------------------------------------------------------------*/
104uint32_t
105nrf_802154_hp_timer_timestamp_task_get(void)
106{
107 return nrf_timer_task_address_get(HP_TIMER, (nrf_timer_task_t)NRF_TIMER_TASK_CAPTURE3);
108}
109/*---------------------------------------------------------------------------*/
110uint32_t
111nrf_802154_hp_timer_timestamp_get(void)
112{
113 return nrf_timer_cc_get(HP_TIMER, (nrf_timer_cc_channel_t)TIMESTAMP_CC);
114}
115/*---------------------------------------------------------------------------*/