xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/freertos/include/nimble/nimble_npl_os.h (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _NIMBLE_NPL_OS_H_
21 #define _NIMBLE_NPL_OS_H_
22 
23 #include <assert.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include "FreeRTOS.h"
27 #include "queue.h"
28 #include "semphr.h"
29 #include "task.h"
30 #include "timers.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #define BLE_NPL_OS_ALIGNMENT    4
37 
38 #define BLE_NPL_TIME_FOREVER    portMAX_DELAY
39 
40 /* This should be compatible with TickType_t */
41 typedef uint32_t ble_npl_time_t;
42 typedef int32_t ble_npl_stime_t;
43 
44 struct ble_npl_event {
45     bool queued;
46     ble_npl_event_fn *fn;
47     void *arg;
48 };
49 
50 struct ble_npl_eventq {
51     QueueHandle_t q;
52 };
53 
54 struct ble_npl_callout {
55     TimerHandle_t handle;
56     struct ble_npl_eventq *evq;
57     struct ble_npl_event ev;
58 };
59 
60 struct ble_npl_mutex {
61     SemaphoreHandle_t handle;
62 };
63 
64 struct ble_npl_sem {
65     SemaphoreHandle_t handle;
66 };
67 
68 /*
69  * Simple APIs are just defined as static inline below, but some are a bit more
70  * complex or require some global state variables and thus are defined in .c
71  * file instead and static inline wrapper just calls proper implementation.
72  * We need declarations of these functions and they are defined in header below.
73  */
74 #include "npl_freertos.h"
75 
76 static inline bool
ble_npl_os_started(void)77 ble_npl_os_started(void)
78 {
79     return xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
80 }
81 
82 static inline void *
ble_npl_get_current_task_id(void)83 ble_npl_get_current_task_id(void)
84 {
85     return xTaskGetCurrentTaskHandle();
86 }
87 
88 static inline void
ble_npl_eventq_init(struct ble_npl_eventq * evq)89 ble_npl_eventq_init(struct ble_npl_eventq *evq)
90 {
91     evq->q = xQueueCreate(32, sizeof(struct ble_npl_eventq *));
92 }
93 
94 static inline struct ble_npl_event *
ble_npl_eventq_get(struct ble_npl_eventq * evq,ble_npl_time_t tmo)95 ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
96 {
97     return npl_freertos_eventq_get(evq, tmo);
98 }
99 
100 static inline void
ble_npl_eventq_put(struct ble_npl_eventq * evq,struct ble_npl_event * ev)101 ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
102 {
103     npl_freertos_eventq_put(evq, ev);
104 }
105 
106 static inline void
ble_npl_eventq_remove(struct ble_npl_eventq * evq,struct ble_npl_event * ev)107 ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
108 {
109     npl_freertos_eventq_remove(evq, ev);
110 }
111 
112 static inline void
ble_npl_event_run(struct ble_npl_event * ev)113 ble_npl_event_run(struct ble_npl_event *ev)
114 {
115     ev->fn(ev);
116 }
117 
118 static inline bool
ble_npl_eventq_is_empty(struct ble_npl_eventq * evq)119 ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
120 {
121     return xQueueIsQueueEmptyFromISR(evq->q);
122 }
123 
124 static inline void
ble_npl_event_init(struct ble_npl_event * ev,ble_npl_event_fn * fn,void * arg)125 ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
126                    void *arg)
127 {
128     memset(ev, 0, sizeof(*ev));
129     ev->fn = fn;
130     ev->arg = arg;
131 }
132 
133 static inline bool
ble_npl_event_is_queued(struct ble_npl_event * ev)134 ble_npl_event_is_queued(struct ble_npl_event *ev)
135 {
136     return ev->queued;
137 }
138 
139 static inline void *
ble_npl_event_get_arg(struct ble_npl_event * ev)140 ble_npl_event_get_arg(struct ble_npl_event *ev)
141 {
142     return ev->arg;
143 }
144 
145 static inline void
ble_npl_event_set_arg(struct ble_npl_event * ev,void * arg)146 ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
147 {
148     ev->arg = arg;
149 }
150 
151 static inline ble_npl_error_t
ble_npl_mutex_init(struct ble_npl_mutex * mu)152 ble_npl_mutex_init(struct ble_npl_mutex *mu)
153 {
154     return npl_freertos_mutex_init(mu);
155 }
156 
157 static inline ble_npl_error_t
ble_npl_mutex_pend(struct ble_npl_mutex * mu,ble_npl_time_t timeout)158 ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
159 {
160     return npl_freertos_mutex_pend(mu, timeout);
161 }
162 
163 static inline ble_npl_error_t
ble_npl_mutex_release(struct ble_npl_mutex * mu)164 ble_npl_mutex_release(struct ble_npl_mutex *mu)
165 {
166     return npl_freertos_mutex_release(mu);
167 }
168 
169 static inline ble_npl_error_t
ble_npl_sem_init(struct ble_npl_sem * sem,uint16_t tokens)170 ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
171 {
172     return npl_freertos_sem_init(sem, tokens);
173 }
174 
175 static inline ble_npl_error_t
ble_npl_sem_pend(struct ble_npl_sem * sem,ble_npl_time_t timeout)176 ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
177 {
178     return npl_freertos_sem_pend(sem, timeout);
179 }
180 
181 static inline ble_npl_error_t
ble_npl_sem_release(struct ble_npl_sem * sem)182 ble_npl_sem_release(struct ble_npl_sem *sem)
183 {
184     return npl_freertos_sem_release(sem);
185 }
186 
187 static inline uint16_t
ble_npl_sem_get_count(struct ble_npl_sem * sem)188 ble_npl_sem_get_count(struct ble_npl_sem *sem)
189 {
190     return uxSemaphoreGetCount(sem->handle);
191 }
192 
193 static inline void
ble_npl_callout_init(struct ble_npl_callout * co,struct ble_npl_eventq * evq,ble_npl_event_fn * ev_cb,void * ev_arg)194 ble_npl_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
195                      ble_npl_event_fn *ev_cb, void *ev_arg)
196 {
197     npl_freertos_callout_init(co, evq, ev_cb, ev_arg);
198 }
199 
200 static inline ble_npl_error_t
ble_npl_callout_reset(struct ble_npl_callout * co,ble_npl_time_t ticks)201 ble_npl_callout_reset(struct ble_npl_callout *co, ble_npl_time_t ticks)
202 {
203     return npl_freertos_callout_reset(co, ticks);
204 }
205 
206 static inline void
ble_npl_callout_stop(struct ble_npl_callout * co)207 ble_npl_callout_stop(struct ble_npl_callout *co)
208 {
209     xTimerStop(co->handle, portMAX_DELAY);
210 }
211 
212 static inline bool
ble_npl_callout_is_active(struct ble_npl_callout * co)213 ble_npl_callout_is_active(struct ble_npl_callout *co)
214 {
215     return xTimerIsTimerActive(co->handle) == pdTRUE;
216 }
217 
218 static inline ble_npl_time_t
ble_npl_callout_get_ticks(struct ble_npl_callout * co)219 ble_npl_callout_get_ticks(struct ble_npl_callout *co)
220 {
221     return xTimerGetExpiryTime(co->handle);
222 }
223 
224 static inline uint32_t
ble_npl_callout_remaining_ticks(struct ble_npl_callout * co,ble_npl_time_t time)225 ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
226                                 ble_npl_time_t time)
227 {
228     return npl_freertos_callout_remaining_ticks(co, time);
229 }
230 
231 static inline void
ble_npl_callout_set_arg(struct ble_npl_callout * co,void * arg)232 ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
233 {
234     co->ev.arg = arg;
235 }
236 
237 static inline uint32_t
ble_npl_time_get(void)238 ble_npl_time_get(void)
239 {
240     return xTaskGetTickCountFromISR();
241 }
242 
243 static inline ble_npl_error_t
ble_npl_time_ms_to_ticks(uint32_t ms,ble_npl_time_t * out_ticks)244 ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
245 {
246     return npl_freertos_time_ms_to_ticks(ms, out_ticks);
247 }
248 
249 static inline ble_npl_error_t
ble_npl_time_ticks_to_ms(ble_npl_time_t ticks,uint32_t * out_ms)250 ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
251 {
252     return ble_npl_time_ticks_to_ms(ticks, out_ms);
253 }
254 
255 static inline ble_npl_time_t
ble_npl_time_ms_to_ticks32(uint32_t ms)256 ble_npl_time_ms_to_ticks32(uint32_t ms)
257 {
258     return ms * configTICK_RATE_HZ / 1000;
259 }
260 
261 static inline uint32_t
ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)262 ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
263 {
264     return ticks * 1000 / configTICK_RATE_HZ;
265 }
266 
267 static inline void
ble_npl_time_delay(ble_npl_time_t ticks)268 ble_npl_time_delay(ble_npl_time_t ticks)
269 {
270     vTaskDelay(ticks);
271 }
272 
273 #if NIMBLE_CFG_CONTROLLER
274 static inline void
ble_npl_hw_set_isr(int irqn,void (* addr)(void))275 ble_npl_hw_set_isr(int irqn, void (*addr)(void))
276 {
277     npl_freertos_hw_set_isr(irqn, addr);
278 }
279 #endif
280 
281 static inline uint32_t
ble_npl_hw_enter_critical(void)282 ble_npl_hw_enter_critical(void)
283 {
284     vPortEnterCritical();
285     return 0;
286 }
287 
288 static inline void
ble_npl_hw_exit_critical(uint32_t ctx)289 ble_npl_hw_exit_critical(uint32_t ctx)
290 {
291     vPortExitCritical();
292 
293 }
294 
295 #ifdef __cplusplus
296 }
297 #endif
298 
299 #endif  /* _NPL_H_ */
300