1 /*
2  * Copyright (c) 2020 - 2023, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include "nrf_802154_sl_utils.h"
36 
37 #include <stdlib.h>
38 #include <stdint.h>
39 
40 #include <zephyr/kernel.h>
41 #include <assert.h>
42 
43 #include "nrf_802154_sl_timer.h"
44 
45 static void timeout_handler(struct k_timer * timer_id);
46 
47 K_TIMER_DEFINE(timer, timeout_handler, NULL);
48 
nrf_802154_timer_coord_init(void)49 void nrf_802154_timer_coord_init(void)
50 {
51     // Intentionally empty
52 }
53 
nrf_802154_timer_coord_uninit(void)54 void nrf_802154_timer_coord_uninit(void)
55 {
56     // Intentionally empty
57 }
58 
nrf_802154_timer_coord_start(void)59 void nrf_802154_timer_coord_start(void)
60 {
61     // Intentionally empty
62 }
63 
nrf_802154_timer_coord_stop(void)64 void nrf_802154_timer_coord_stop(void)
65 {
66     // Intentionally empty
67 }
68 
nrf_802154_sl_timer_module_init(void)69 void nrf_802154_sl_timer_module_init(void)
70 {
71     BUILD_ASSERT(CONFIG_SYS_CLOCK_TICKS_PER_SEC == NRF_802154_SL_RTC_FREQUENCY);
72 }
73 
nrf_802154_sl_timer_module_uninit(void)74 void nrf_802154_sl_timer_module_uninit(void)
75 {
76     // Intentionally empty
77 }
78 
nrf_802154_sl_timer_current_time_get(void)79 uint64_t nrf_802154_sl_timer_current_time_get(void)
80 {
81     return NRF_802154_SL_RTC_TICKS_TO_US(k_uptime_ticks());
82 }
83 
nrf_802154_sl_timer_init(nrf_802154_sl_timer_t * p_timer)84 void nrf_802154_sl_timer_init(nrf_802154_sl_timer_t * p_timer)
85 {
86     // Intentionally empty
87 }
88 
nrf_802154_sl_timer_deinit(nrf_802154_sl_timer_t * p_timer)89 void nrf_802154_sl_timer_deinit(nrf_802154_sl_timer_t * p_timer)
90 {
91     // Intentionally empty
92 }
93 
nrf_802154_sl_timer_add(nrf_802154_sl_timer_t * p_timer)94 nrf_802154_sl_timer_ret_t nrf_802154_sl_timer_add(nrf_802154_sl_timer_t * p_timer)
95 {
96     uint64_t now    = nrf_802154_sl_timer_current_time_get();
97     int32_t  target = p_timer->trigger_time - now;
98 
99     target = MAX(target, 1);
100 
101     k_timer_user_data_set(&timer, p_timer); // Passing arguments is not supported.
102     k_timer_start(&timer, K_USEC(target), K_NO_WAIT);
103 
104     return NRF_802154_SL_TIMER_RET_SUCCESS;
105 }
106 
nrf_802154_sl_timer_remove(nrf_802154_sl_timer_t * p_timer)107 nrf_802154_sl_timer_ret_t nrf_802154_sl_timer_remove(nrf_802154_sl_timer_t * p_timer)
108 {
109     nrf_802154_sl_timer_ret_t ret;
110 
111     if (k_timer_status_get(&timer) > 0)
112     {
113         /* Timer has expired. */
114         ret = NRF_802154_SL_TIMER_RET_INACTIVE;
115     }
116     else if (k_timer_remaining_get(&timer) == 0)
117     {
118         /* Timer was stopped (by someone else) before expiring. */
119         ret = NRF_802154_SL_TIMER_RET_INACTIVE;
120     }
121     else
122     {
123         /* Timer is still running. */
124         k_timer_stop(&timer);
125 
126         ret = NRF_802154_SL_TIMER_RET_SUCCESS;
127     }
128 
129     return ret;
130 }
131 
timeout_handler(struct k_timer * timer_id)132 static void timeout_handler(struct k_timer * timer_id)
133 {
134     nrf_802154_sl_timer_t * p_timer =
135         (nrf_802154_sl_timer_t *)k_timer_user_data_get(timer_id);
136 
137     p_timer->action.callback.callback(p_timer);
138 }
139 
nrf_802154_platform_sl_lp_timer_init(void)140 void nrf_802154_platform_sl_lp_timer_init(void)
141 {
142     // Intentionally empty
143 }
144 
nrf_802154_platform_sl_lp_timer_deinit(void)145 void nrf_802154_platform_sl_lp_timer_deinit(void)
146 {
147     // Intentionally empty
148 }
149 
nrf_802154_platform_sl_lptimer_critical_section_enter(void)150 void nrf_802154_platform_sl_lptimer_critical_section_enter(void)
151 {
152     // Intentionally empty
153 }
154 
nrf_802154_platform_sl_lptimer_critical_section_exit(void)155 void nrf_802154_platform_sl_lptimer_critical_section_exit(void)
156 {
157     // Intentionally empty
158 }
159