1 /*
2  * Copyright (c) 2020, 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 
42 #include "nrf_802154_sl_timer.h"
43 
44 static void timeout_handler(struct k_timer * timer_id);
45 
46 K_TIMER_DEFINE(timer, timeout_handler, NULL);
47 
nrf_802154_timer_coord_init(void)48 void nrf_802154_timer_coord_init(void)
49 {
50     // Intentionally empty
51 }
52 
nrf_802154_timer_coord_uninit(void)53 void nrf_802154_timer_coord_uninit(void)
54 {
55     // Intentionally empty
56 }
57 
nrf_802154_timer_coord_start(void)58 void nrf_802154_timer_coord_start(void)
59 {
60     // Intentionally empty
61 }
62 
nrf_802154_timer_coord_stop(void)63 void nrf_802154_timer_coord_stop(void)
64 {
65     // Intentionally empty
66 }
67 
nrf_802154_sl_timer_module_init(void)68 void nrf_802154_sl_timer_module_init(void)
69 {
70     // Intentionally empty
71 }
72 
nrf_802154_sl_timer_module_uninit(void)73 void nrf_802154_sl_timer_module_uninit(void)
74 {
75     // Intentionally empty
76 }
77 
nrf_802154_sl_timer_current_time_get(void)78 uint64_t nrf_802154_sl_timer_current_time_get(void)
79 {
80     int64_t ticks = k_uptime_ticks();
81 
82     return k_ticks_to_us_ceil64(ticks);
83 }
84 
nrf_802154_sl_timer_init(nrf_802154_sl_timer_t * p_timer)85 void nrf_802154_sl_timer_init(nrf_802154_sl_timer_t * p_timer)
86 {
87     // Intentionally empty
88 }
89 
nrf_802154_sl_timer_deinit(nrf_802154_sl_timer_t * p_timer)90 void nrf_802154_sl_timer_deinit(nrf_802154_sl_timer_t * p_timer)
91 {
92     // Intentionally empty
93 }
94 
nrf_802154_sl_timer_add(nrf_802154_sl_timer_t * p_timer)95 nrf_802154_sl_timer_ret_t nrf_802154_sl_timer_add(nrf_802154_sl_timer_t * p_timer)
96 {
97     uint64_t now    = nrf_802154_sl_timer_current_time_get();
98     int32_t  target = p_timer->trigger_time - now;
99 
100     target = MAX(target, 1);
101 
102     k_timer_user_data_set(&timer, p_timer); // Passing arguments is not supported.
103     k_timer_start(&timer, K_USEC(target), K_NO_WAIT);
104 
105     return NRF_802154_SL_TIMER_RET_SUCCESS;
106 }
107 
nrf_802154_sl_timer_remove(nrf_802154_sl_timer_t * p_timer)108 nrf_802154_sl_timer_ret_t nrf_802154_sl_timer_remove(nrf_802154_sl_timer_t * p_timer)
109 {
110     nrf_802154_sl_timer_ret_t ret;
111 
112     if (k_timer_status_get(&timer) > 0)
113     {
114         /* Timer has expired. */
115         ret = NRF_802154_SL_TIMER_RET_INACTIVE;
116     }
117     else if (k_timer_remaining_get(&timer) == 0)
118     {
119         /* Timer was stopped (by someone else) before expiring. */
120         ret = NRF_802154_SL_TIMER_RET_INACTIVE;
121     }
122     else
123     {
124         /* Timer is still running. */
125         k_timer_stop(&timer);
126 
127         ret = NRF_802154_SL_TIMER_RET_SUCCESS;
128     }
129 
130     return ret;
131 }
132 
timeout_handler(struct k_timer * timer_id)133 static void timeout_handler(struct k_timer * timer_id)
134 {
135     nrf_802154_sl_timer_t * p_timer =
136         (nrf_802154_sl_timer_t *)k_timer_user_data_get(timer_id);
137 
138     p_timer->action.callback.callback(p_timer);
139 }
140 
nrf_802154_platform_sl_lp_timer_init(void)141 void nrf_802154_platform_sl_lp_timer_init(void)
142 {
143     // Intentionally empty
144 }
145 
nrf_802154_platform_sl_lp_timer_deinit(void)146 void nrf_802154_platform_sl_lp_timer_deinit(void)
147 {
148     // Intentionally empty
149 }
150 
nrf_802154_platform_sl_lptimer_critical_section_enter(void)151 void nrf_802154_platform_sl_lptimer_critical_section_enter(void)
152 {
153     // Intentionally empty
154 }
155 
nrf_802154_platform_sl_lptimer_critical_section_exit(void)156 void nrf_802154_platform_sl_lptimer_critical_section_exit(void)
157 {
158     // Intentionally empty
159 }
160