nimble_npl_os.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "os/os.h"
25 #include "mcu/mcu.h"
26 
27 #if defined(CPU_FAM_NRF51) || defined(CPU_FAM_NRF52)
28 #include "nrf_clock.h"
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
39 #define BLE_NPL_OS_ALIGNMENT (OS_ALIGNMENT)
40 #define BLE_NPL_TIME_FOREVER (OS_WAIT_FOREVER)
46 typedef uint32_t ble_npl_time_t;
50 typedef int32_t ble_npl_stime_t;
51 
55 struct ble_npl_event {
56  struct os_event ev;
57 };
58 
63  struct os_eventq evq;
64 };
65 
70  uint32_t ticks;
71  struct os_callout co;
72 };
73 
77 struct ble_npl_mutex {
78  struct os_mutex mu;
79 };
80 
84 struct ble_npl_sem {
85  struct os_sem sem;
86 };
87 
93 static inline bool ble_npl_os_started(void)
94 {
95  return true;
96 }
97 
105 static inline void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
106  void *arg)
107 {
108  os_event_init(&ev->ev, (os_event_fn *)fn, arg);
109 }
110 
118 static inline bool ble_npl_event_is_queued(struct ble_npl_event *ev)
119 {
120  return os_event_is_queued(&ev->ev);
121 }
122 
128 static inline void *ble_npl_event_get_arg(struct ble_npl_event *ev)
129 {
130  return os_event_get_arg(&ev->ev);
131 }
132 
139 static inline void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
140 {
141  os_event_set_arg(&ev->ev, arg);
142 }
143 
149 static inline void ble_npl_event_run(struct ble_npl_event *ev)
150 {
151  os_event_run(&ev->ev);
152 }
153 
159 static inline void ble_npl_eventq_init(struct ble_npl_eventq *evq)
160 {
161  os_eventq_init(&evq->evq);
162 }
163 
169 static inline int ble_npl_eventq_inited(struct ble_npl_eventq *evq)
170 {
171  return os_eventq_inited(&evq->evq);
172 }
173 
181 static inline void ble_npl_eventq_deinit(struct ble_npl_eventq *evq)
182 {
183  (void)evq;
184  /* Can't deinit an eventq in RIOT */
185 }
186 
195 static inline struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq,
196  ble_npl_time_t tmo)
197 {
198  return (struct ble_npl_event *)os_eventq_get(&evq->evq, tmo);
199 }
200 
208 static inline struct ble_npl_event *ble_npl_eventq_get_no_wait(struct ble_npl_eventq *evq)
209 {
210  return (struct ble_npl_event *)os_eventq_get_no_wait(&evq->evq);
211 }
212 
219 static inline void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
220 {
221  os_eventq_put(&evq->evq, &ev->ev);
222 }
223 
230 static inline void ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
231 {
232  os_eventq_remove(&evq->evq, &ev->ev);
233 }
234 
240 static inline void ble_npl_eventq_run(struct ble_npl_eventq *evq)
241 {
242  os_eventq_run(&evq->evq);
243 }
244 
252 static inline bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
253 {
254  return os_eventq_is_empty(&evq->evq);
255 }
256 
262 static inline ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu)
263 {
264  return (ble_npl_error_t)os_mutex_init(&mu->mu);
265 }
266 
279 static inline ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
280 {
281  return (ble_npl_error_t)os_mutex_pend(&mu->mu, timeout);
282 }
283 
292 static inline ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu)
293 {
294  return (ble_npl_error_t)os_mutex_release(&mu->mu);
295 }
296 
307 static inline ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
308 {
309  return (ble_npl_error_t)os_sem_init(&sem->sem, tokens);
310 }
311 
326 static inline ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
327 {
328  return (ble_npl_error_t)os_sem_pend(&sem->sem, timeout);
329 }
330 
340 static inline ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem)
341 {
342  return (ble_npl_error_t)os_sem_release(&sem->sem);
343 }
344 
348 static inline uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem)
349 {
350  return os_sem_get_count(&sem->sem);
351 }
352 
367 static inline void ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *q,
368  ble_npl_event_fn *e_cb, void *e_arg)
369 {
370  os_callout_init(&c->co, &q->evq, (os_event_fn *)e_cb, e_arg);
371 }
372 
381 static inline ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *c, ble_npl_time_t ticks)
382 {
383  uint32_t state = os_hw_enter_critical();
384 
385  c->ticks = ztimer_now(ZTIMER_MSEC) + ticks;
386  os_callout_reset(&c->co, ticks);
387  os_hw_exit_critical(state);
388  return BLE_NPL_OK;
389 }
390 
396 static inline void ble_npl_callout_stop(struct ble_npl_callout *c)
397 {
398  os_callout_stop(&c->co);
399 }
400 
408 static inline bool ble_npl_callout_is_active(struct ble_npl_callout *c)
409 {
410  return ztimer_is_set(ZTIMER_MSEC, &c->co.timer);
411 }
412 
419 {
420  return co->ticks;
421 }
422 
432  ble_npl_time_t time)
433 {
434  (void)time;
436  return (ble_npl_time_t)(co->ticks - now);
437 }
438 
445 static inline void ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
446 {
447  co->co.c_ev.arg = arg;
448 }
449 
455 static inline ble_npl_time_t ble_npl_time_get(void)
456 {
457  return os_time_get();
458 }
459 
468 static inline ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
469 {
470  return (ble_npl_error_t)os_time_ms_to_ticks(ms, out_ticks);
471 }
472 
481 static inline ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
482 {
483  return (ble_npl_error_t)os_time_ticks_to_ms(ticks, out_ms);
484 }
485 
493 static inline ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms)
494 {
495  return os_time_ms_to_ticks32(ms);
496 }
497 
506 {
507  return os_time_ticks_to_ms32(ticks);
508 }
514 static inline void ble_npl_time_delay(ble_npl_time_t ticks)
515 {
516  return os_time_delay(ticks);
517 }
518 
524 static inline uint32_t ble_npl_hw_enter_critical(void)
525 {
526  return os_hw_enter_critical();
527 }
528 
534 static inline void ble_npl_hw_exit_critical(uint32_t ctx)
535 {
536  os_hw_exit_critical(ctx);
537 }
538 
544 static inline bool ble_npl_hw_is_in_critical(void)
545 {
546  return os_hw_is_in_critical();
547 }
548 
554 static inline void *ble_npl_get_current_task_id(void)
555 {
556  return (void *)(uint32_t)thread_getpid();
557 }
558 
565 static inline void ble_npl_hw_set_isr(int irqn, void (*addr)(void))
566 {
567  nrf5x_hw_set_isr(irqn, addr);
568 }
569 
570 /* XXX: these functions are required to build hal_timer.c, however with the
571 * default configuration they are never used... */
572 #if defined(CPU_FAM_NRF51) || defined(CPU_FAM_NRF52)
573 static inline void
574 nrf52_clock_hfxo_request(void)
575 {
577 }
578 
579 static inline void
580 nrf52_clock_hfxo_release(void)
581 {
583 }
584 #endif
585 
586 #ifdef __cplusplus
587 }
588 #endif
void clock_hfxo_request(void)
Request the external high frequency crystal (HFXO) as HF clock source.
void clock_hfxo_release(void)
Release the use of the HFXO.
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
Definition: thread.h:399
uint32_t ztimer_now_t
type for ztimer_now() result
Definition: ztimer.h:311
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition: ztimer.h:683
unsigned ztimer_is_set(const ztimer_clock_t *clock, const ztimer_t *timer)
Check if a timer is currently active.
ztimer_clock_t *const ZTIMER_MSEC
Default ztimer millisecond clock.
Abstraction layer for RIOT adaption.
void nrf5x_hw_set_isr(int irqn, void(*addr)(void))
Set nrf5x radio ISR callback.
time_point now()
Returns the current time saved in a time point.
Definition: chrono.hpp:104
static bool ble_npl_os_started(void)
Not used in RIOT.
Definition: nimble_npl_os.h:93
static void ble_npl_hw_exit_critical(uint32_t ctx)
Restores ISR context.
static void ble_npl_eventq_deinit(struct ble_npl_eventq *evq)
Deinitialize an event queue.
static void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
Set the vent arg.
static void ble_npl_event_run(struct ble_npl_event *ev)
Runs an event.
static struct ble_npl_event * ble_npl_eventq_get(struct ble_npl_eventq *evq, ble_npl_time_t tmo)
Get next event from event queue, blocking.
static uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem)
Get current semaphore's count.
static void ble_npl_eventq_init(struct ble_npl_eventq *evq)
Initialize the event queue.
int32_t ble_npl_stime_t
time type
Definition: nimble_npl_os.h:50
static ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms)
Converts the given number of milliseconds into cputime ticks.
static void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *arg)
Init a event.
static void * ble_npl_event_get_arg(struct ble_npl_event *ev)
Runs an event.
static uint32_t ble_npl_hw_enter_critical(void)
Disable ISRs.
static void ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
Remove an event from the queue.
static void ble_npl_hw_set_isr(int irqn, void(*addr)(void))
Set nrf5x radio ISR callback.
static void ble_npl_callout_stop(struct ble_npl_callout *c)
Stops the callout from firing.
static void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
Put an event on the event queue.
static bool ble_npl_callout_is_active(struct ble_npl_callout *c)
Check if callout is active.
static ble_npl_time_t ble_npl_time_get(void)
Returns the low 32 bits of cputime.
uint32_t ble_npl_time_t
time type
Definition: nimble_npl_os.h:46
static ble_npl_time_t ble_npl_callout_remaining_ticks(struct ble_npl_callout *co, ble_npl_time_t time)
Get the remaining ticks for callout expire.
static bool ble_npl_hw_is_in_critical(void)
Check if is in critical section.
static ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem)
Release a semaphore.
static ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
Initialize a semaphore.
static ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *c, ble_npl_time_t ticks)
Reset the callout to fire off in 'ticks' ticks.
static void ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *q, ble_npl_event_fn *e_cb, void *e_arg)
Initialize a callout.
static ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
Converts the given number of milliseconds into cputime ticks.
static int ble_npl_eventq_inited(struct ble_npl_eventq *evq)
Check whether the event queue is initialized.
static ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu)
Release a mutex.
static bool ble_npl_event_is_queued(struct ble_npl_event *ev)
Check if event is in queue.
static void ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
Set the callout event argument.
static ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
Convert the given number of ticks into milliseconds.
static void ble_npl_time_delay(ble_npl_time_t ticks)
Wait until the number of ticks has elapsed, BLOICKING.
static void ble_npl_eventq_run(struct ble_npl_eventq *evq)
Gets and runs an event from the queue callback.
static struct ble_npl_event * ble_npl_eventq_get_no_wait(struct ble_npl_eventq *evq)
Get next event from event queue, non-blocking.
static ble_npl_time_t ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
Convert the given number of ticks into milliseconds.
static ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu)
Initializes a mutex object.
static ble_npl_time_t ble_npl_callout_get_ticks(struct ble_npl_callout *co)
Get the callout set ticks.
static ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
Pend (wait) for a mutex.
static void * ble_npl_get_current_task_id(void)
Return current thread PID.
static ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
Pend (wait) for a semaphore.
static bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
Check if queue is empty.
Apache Mynewt Copyright 2015-2021 The Apache Software Foundation.
static bool os_hw_is_in_critical(void)
Check if is in critical section.
Definition: os.h:117
static void os_hw_exit_critical(uint32_t ctx)
Restores ISR context.
Definition: os.h:107
static uint32_t os_hw_enter_critical(void)
Disable ISRs.
Definition: os.h:96
static bool os_eventq_is_empty(struct os_eventq *evq)
Check if queue is empty.
Definition: os_eventq.h:226
static void os_event_init(struct os_event *ev, os_event_fn *fn, void *arg)
Init a event.
Definition: os_eventq.h:59
static void os_event_set_arg(struct os_event *ev, void *arg)
Set the event argument.
Definition: os_eventq.h:99
static int os_eventq_inited(struct os_eventq *evq)
Check whether the event queue is initialized.
Definition: os_eventq.h:129
static void * os_event_get_arg(struct os_event *ev)
Returns event argument.
Definition: os_eventq.h:88
static void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
Remove an event from the queue.
Definition: os_eventq.h:203
static bool os_event_is_queued(struct os_event *ev)
Check if event is in queue.
Definition: os_eventq.h:78
void os_event_fn(struct os_event *ev)
Event callback function.
Definition: os_eventq.h:50
static void os_event_run(struct os_event *ev)
Runs an event.
Definition: os_eventq.h:109
static void os_eventq_init(struct os_eventq *evq)
Initialize the event queue.
Definition: os_eventq.h:119
static struct os_event * os_eventq_get_no_wait(struct os_eventq *evq)
Get next event from event queue, non-blocking.
Definition: os_eventq.h:177
static struct os_event * os_eventq_get(struct os_eventq *evq, os_time_t tmo)
Get next event from event queue.
Definition: os_eventq.h:155
static void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
Put an event on the event queue.
Definition: os_eventq.h:192
static void os_eventq_run(struct os_eventq *evq)
Gets and runs an event from the queue callback.
Definition: os_eventq.h:213
static os_time_t os_time_ms_to_ticks32(uint32_t ms)
Converts the given number of milliseconds into cputime ticks.
Definition: os_time.h:79
static os_time_t os_time_get(void)
Returns the low 32 bits of cputime.
Definition: os_time.h:39
static void os_time_delay(os_time_t ticks)
Wait until the number of ticks has elapsed, BLOICKING.
Definition: os_time.h:101
static os_time_t os_time_ticks_to_ms32(os_time_t ticks)
Convert the given number of ticks into milliseconds.
Definition: os_time.h:91
static os_error_t os_time_ticks_to_ms(os_time_t ticks, uint32_t *out_ms)
Convert the given number of ticks into milliseconds.
Definition: os_time.h:66
static os_error_t os_time_ms_to_ticks(uint32_t ms, os_time_t *out_ticks)
Converts the given number of milliseconds into cputime ticks.
Definition: os_time.h:52
ble_npl callout wrapper
Definition: nimble_npl_os.h:69
uint32_t ticks
the callout set timeout
Definition: nimble_npl_os.h:70
struct os_callout co
the callout
Definition: nimble_npl_os.h:71
ble_npl event wrapper
Definition: nimble_npl_os.h:55
struct os_event ev
the event
Definition: nimble_npl_os.h:56
ble_npl event queue wrapper
Definition: nimble_npl_os.h:62
struct os_eventq evq
the event queue
Definition: nimble_npl_os.h:63
ble_npl mutex wrapper
Definition: nimble_npl_os.h:77
struct os_mutex mu
mutex
Definition: nimble_npl_os.h:78
ble_npl semaphore wrapper
Definition: nimble_npl_os.h:84
struct os_sem sem
semaphore
Definition: nimble_npl_os.h:85
Event wrapper.
Definition: os_eventq.h:34
Event queue wrapper.
Definition: os_eventq.h:43