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 
20 #ifndef NIMBLE_NIMBLE_NPL_OS_H
21 #define NIMBLE_NIMBLE_NPL_OS_H
22 
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include "os/os.h"
26 #include "mcu/mcu.h"
27 
28 #if defined(CPU_FAM_NRF51) || defined(CPU_FAM_NRF52)
29 #include "nrf_clock.h"
30 #endif
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
40 #define BLE_NPL_OS_ALIGNMENT (OS_ALIGNMENT)
41 #define BLE_NPL_TIME_FOREVER (OS_WAIT_FOREVER)
47 typedef uint32_t ble_npl_time_t;
51 typedef int32_t ble_npl_stime_t;
52 
56 struct ble_npl_event {
57  struct os_event ev;
58 };
59 
64  struct os_eventq evq;
65 };
66 
71  uint32_t ticks;
72  struct os_callout co;
73 };
74 
78 struct ble_npl_mutex {
79  struct os_mutex mu;
80 };
81 
85 struct ble_npl_sem {
86  struct os_sem sem;
87 };
88 
94 static inline bool ble_npl_os_started(void)
95 {
96  return true;
97 }
98 
106 static inline void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
107  void *arg)
108 {
109  os_event_init(&ev->ev, (os_event_fn *)fn, arg);
110 }
111 
119 static inline bool ble_npl_event_is_queued(struct ble_npl_event *ev)
120 {
121  return os_event_is_queued(&ev->ev);
122 }
123 
129 static inline void *ble_npl_event_get_arg(struct ble_npl_event *ev)
130 {
131  return os_event_get_arg(&ev->ev);
132 }
133 
140 static inline void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
141 {
142  os_event_set_arg(&ev->ev, arg);
143 }
144 
150 static inline void ble_npl_event_run(struct ble_npl_event *ev)
151 {
152  os_event_run(&ev->ev);
153 }
154 
160 static inline void ble_npl_eventq_init(struct ble_npl_eventq *evq)
161 {
162  os_eventq_init(&evq->evq);
163 }
164 
170 static inline int ble_npl_eventq_inited(struct ble_npl_eventq *evq)
171 {
172  return os_eventq_inited(&evq->evq);
173 }
174 
182 static inline void ble_npl_eventq_deinit(struct ble_npl_eventq *evq)
183 {
184  (void)evq;
185  /* Can't deinit an eventq in RIOT */
186 }
187 
196 static inline struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq,
197  ble_npl_time_t tmo)
198 {
199  return (struct ble_npl_event *)os_eventq_get(&evq->evq, tmo);
200 }
201 
209 static inline struct ble_npl_event *ble_npl_eventq_get_no_wait(struct ble_npl_eventq *evq)
210 {
211  return (struct ble_npl_event *)os_eventq_get_no_wait(&evq->evq);
212 }
213 
220 static inline void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
221 {
222  os_eventq_put(&evq->evq, &ev->ev);
223 }
224 
231 static inline void ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
232 {
233  os_eventq_remove(&evq->evq, &ev->ev);
234 }
235 
241 static inline void ble_npl_eventq_run(struct ble_npl_eventq *evq)
242 {
243  os_eventq_run(&evq->evq);
244 }
245 
253 static inline bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
254 {
255  return os_eventq_is_empty(&evq->evq);
256 }
257 
263 static inline ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu)
264 {
265  return (ble_npl_error_t)os_mutex_init(&mu->mu);
266 }
267 
280 static inline ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
281 {
282  return (ble_npl_error_t)os_mutex_pend(&mu->mu, timeout);
283 }
284 
293 static inline ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu)
294 {
295  return (ble_npl_error_t)os_mutex_release(&mu->mu);
296 }
297 
308 static inline ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
309 {
310  return (ble_npl_error_t)os_sem_init(&sem->sem, tokens);
311 }
312 
327 static inline ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
328 {
329  return (ble_npl_error_t)os_sem_pend(&sem->sem, timeout);
330 }
331 
341 static inline ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem)
342 {
343  return (ble_npl_error_t)os_sem_release(&sem->sem);
344 }
345 
349 static inline uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem)
350 {
351  return os_sem_get_count(&sem->sem);
352 }
353 
368 static inline void ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *q,
369  ble_npl_event_fn *e_cb, void *e_arg)
370 {
371  os_callout_init(&c->co, &q->evq, (os_event_fn *)e_cb, e_arg);
372 }
373 
382 static inline ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *c, ble_npl_time_t ticks)
383 {
384  uint32_t state = os_hw_enter_critical();
385 
386  c->ticks = ztimer_now(ZTIMER_MSEC) + ticks;
387  os_callout_reset(&c->co, ticks);
388  os_hw_exit_critical(state);
389  return BLE_NPL_OK;
390 }
391 
397 static inline void ble_npl_callout_stop(struct ble_npl_callout *c)
398 {
399  os_callout_stop(&c->co);
400 }
401 
409 static inline bool ble_npl_callout_is_active(struct ble_npl_callout *c)
410 {
411  return ztimer_is_set(ZTIMER_MSEC, &c->co.timer);
412 }
413 
420 {
421  return co->ticks;
422 }
423 
433  ble_npl_time_t time)
434 {
435  (void)time;
437  return (ble_npl_time_t)(co->ticks - now);
438 }
439 
446 static inline void ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
447 {
448  co->co.c_ev.arg = arg;
449 }
450 
456 static inline ble_npl_time_t ble_npl_time_get(void)
457 {
458  return os_time_get();
459 }
460 
469 static inline ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
470 {
471  return (ble_npl_error_t)os_time_ms_to_ticks(ms, out_ticks);
472 }
473 
482 static inline ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
483 {
484  return (ble_npl_error_t)os_time_ticks_to_ms(ticks, out_ms);
485 }
486 
494 static inline ble_npl_time_t ble_npl_time_ms_to_ticks32(uint32_t ms)
495 {
496  return os_time_ms_to_ticks32(ms);
497 }
498 
507 {
508  return os_time_ticks_to_ms32(ticks);
509 }
515 static inline void ble_npl_time_delay(ble_npl_time_t ticks)
516 {
517  return os_time_delay(ticks);
518 }
519 
525 static inline uint32_t ble_npl_hw_enter_critical(void)
526 {
527  return os_hw_enter_critical();
528 }
529 
535 static inline void ble_npl_hw_exit_critical(uint32_t ctx)
536 {
537  os_hw_exit_critical(ctx);
538 }
539 
545 static inline bool ble_npl_hw_is_in_critical(void)
546 {
547  return os_hw_is_in_critical();
548 }
549 
555 static inline void *ble_npl_get_current_task_id(void)
556 {
557  return (void *)(uint32_t)thread_getpid();
558 }
559 
566 static inline void ble_npl_hw_set_isr(int irqn, void (*addr)(void))
567 {
568  nrf5x_hw_set_isr(irqn, addr);
569 }
570 
571 /* XXX: these functions are required to build hal_timer.c, however with the
572 * default configuration they are never used... */
573 #if defined(CPU_FAM_NRF51) || defined(CPU_FAM_NRF52)
574 static inline void
575 nrf52_clock_hfxo_request(void)
576 {
578 }
579 
580 static inline void
581 nrf52_clock_hfxo_release(void)
582 {
584 }
585 #endif
586 
587 #ifdef __cplusplus
588 }
589 #endif
590 
591 #endif /* NIMBLE_NIMBLE_NPL_OS_H */
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:393
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:94
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:51
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:47
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:118
static void os_hw_exit_critical(uint32_t ctx)
Restores ISR context.
Definition: os.h:108
static uint32_t os_hw_enter_critical(void)
Disable ISRs.
Definition: os.h:97
static bool os_eventq_is_empty(struct os_eventq *evq)
Check if queue is empty.
Definition: os_eventq.h:227
static void os_event_init(struct os_event *ev, os_event_fn *fn, void *arg)
Init a event.
Definition: os_eventq.h:60
static void os_event_set_arg(struct os_event *ev, void *arg)
Set the event argument.
Definition: os_eventq.h:100
static int os_eventq_inited(struct os_eventq *evq)
Check whether the event queue is initialized.
Definition: os_eventq.h:130
static void * os_event_get_arg(struct os_event *ev)
Returns event argument.
Definition: os_eventq.h:89
static void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
Remove an event from the queue.
Definition: os_eventq.h:204
static bool os_event_is_queued(struct os_event *ev)
Check if event is in queue.
Definition: os_eventq.h:79
void os_event_fn(struct os_event *ev)
Event callback function.
Definition: os_eventq.h:51
static void os_event_run(struct os_event *ev)
Runs an event.
Definition: os_eventq.h:110
static void os_eventq_init(struct os_eventq *evq)
Initialize the event queue.
Definition: os_eventq.h:120
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:178
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:156
static void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
Put an event on the event queue.
Definition: os_eventq.h:193
static void os_eventq_run(struct os_eventq *evq)
Gets and runs an event from the queue callback.
Definition: os_eventq.h:214
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:80
static os_time_t os_time_get(void)
Returns the low 32 bits of cputime.
Definition: os_time.h:40
static void os_time_delay(os_time_t ticks)
Wait until the number of ticks has elapsed, BLOICKING.
Definition: os_time.h:102
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:92
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:67
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:53
ble_npl callout wrapper
Definition: nimble_npl_os.h:70
uint32_t ticks
the callout set timeout
Definition: nimble_npl_os.h:71
struct os_callout co
the callout
Definition: nimble_npl_os.h:72
ble_npl event wrapper
Definition: nimble_npl_os.h:56
struct os_event ev
the event
Definition: nimble_npl_os.h:57
ble_npl event queue wrapper
Definition: nimble_npl_os.h:63
struct os_eventq evq
the event queue
Definition: nimble_npl_os.h:64
ble_npl mutex wrapper
Definition: nimble_npl_os.h:78
struct os_mutex mu
mutex
Definition: nimble_npl_os.h:79
ble_npl semaphore wrapper
Definition: nimble_npl_os.h:85
struct os_sem sem
semaphore
Definition: nimble_npl_os.h:86
Event wrapper.
Definition: os_eventq.h:35
Event queue wrapper.
Definition: os_eventq.h:44