Loading...
Searching...
No Matches
nimble_npl_os.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Inria
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
18
19#include <stdint.h>
20#include <stdbool.h>
21#include "os/os.h"
22#include "mcu/mcu.h"
23
24#if defined(CPU_FAM_NRF51) || defined(CPU_FAM_NRF52)
25#include "nrf_clock.h"
26#endif
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
36#define BLE_NPL_OS_ALIGNMENT (OS_ALIGNMENT)
37#define BLE_NPL_TIME_FOREVER (OS_WAIT_FOREVER)
39
43typedef uint32_t ble_npl_time_t;
47typedef int32_t ble_npl_stime_t;
48
53 struct os_event ev;
54};
55
60 struct os_eventq evq;
61};
62
67 uint32_t ticks;
68 struct os_callout co;
69};
70
75 struct os_mutex mu;
76};
77
82 struct os_sem sem;
83};
84
90static inline bool ble_npl_os_started(void)
91{
92 return true;
93}
94
102static inline void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
103 void *arg)
104{
105 os_event_init(&ev->ev, (os_event_fn *)fn, arg);
106}
107
115static inline bool ble_npl_event_is_queued(struct ble_npl_event *ev)
116{
117 return os_event_is_queued(&ev->ev);
118}
119
125static inline void *ble_npl_event_get_arg(struct ble_npl_event *ev)
126{
127 return os_event_get_arg(&ev->ev);
128}
129
136static inline void ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
137{
138 os_event_set_arg(&ev->ev, arg);
139}
140
146static inline void ble_npl_event_run(struct ble_npl_event *ev)
147{
148 os_event_run(&ev->ev);
149}
150
156static inline void ble_npl_eventq_init(struct ble_npl_eventq *evq)
157{
158 os_eventq_init(&evq->evq);
159}
160
166static inline int ble_npl_eventq_inited(struct ble_npl_eventq *evq)
167{
168 return os_eventq_inited(&evq->evq);
169}
170
178static inline void ble_npl_eventq_deinit(struct ble_npl_eventq *evq)
179{
180 (void)evq;
181 /* Can't deinit an eventq in RIOT */
182}
183
192static inline struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *evq,
193 ble_npl_time_t tmo)
194{
195 return (struct ble_npl_event *)os_eventq_get(&evq->evq, tmo);
196}
197
206{
207 return (struct ble_npl_event *)os_eventq_get_no_wait(&evq->evq);
208}
209
216static inline void ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
217{
218 os_eventq_put(&evq->evq, &ev->ev);
219}
220
227static inline void ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
228{
229 os_eventq_remove(&evq->evq, &ev->ev);
230}
231
237static inline void ble_npl_eventq_run(struct ble_npl_eventq *evq)
238{
239 os_eventq_run(&evq->evq);
240}
241
249static inline bool ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
250{
251 return os_eventq_is_empty(&evq->evq);
252}
253
259static inline ble_npl_error_t ble_npl_mutex_init(struct ble_npl_mutex *mu)
260{
261 return (ble_npl_error_t)os_mutex_init(&mu->mu);
262}
263
276static inline ble_npl_error_t ble_npl_mutex_pend(struct ble_npl_mutex *mu, ble_npl_time_t timeout)
277{
278 return (ble_npl_error_t)os_mutex_pend(&mu->mu, timeout);
279}
280
289static inline ble_npl_error_t ble_npl_mutex_release(struct ble_npl_mutex *mu)
290{
291 return (ble_npl_error_t)os_mutex_release(&mu->mu);
292}
293
304static inline ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
305{
306 return (ble_npl_error_t)os_sem_init(&sem->sem, tokens);
307}
308
323static inline ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout)
324{
325 return (ble_npl_error_t)os_sem_pend(&sem->sem, timeout);
326}
327
337static inline ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem)
338{
339 return (ble_npl_error_t)os_sem_release(&sem->sem);
340}
341
345static inline uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem)
346{
347 return os_sem_get_count(&sem->sem);
348}
349
364static inline void ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *q,
365 ble_npl_event_fn *e_cb, void *e_arg)
366{
367 os_callout_init(&c->co, &q->evq, (os_event_fn *)e_cb, e_arg);
368}
369
378static inline ble_npl_error_t ble_npl_callout_reset(struct ble_npl_callout *c, ble_npl_time_t ticks)
379{
380 uint32_t state = os_hw_enter_critical();
381
382 c->ticks = ztimer_now(ZTIMER_MSEC) + ticks;
383 os_callout_reset(&c->co, ticks);
384 os_hw_exit_critical(state);
385 return BLE_NPL_OK;
386}
387
393static inline void ble_npl_callout_stop(struct ble_npl_callout *c)
394{
395 os_callout_stop(&c->co);
396}
397
405static inline bool ble_npl_callout_is_active(struct ble_npl_callout *c)
406{
407 return ztimer_is_set(ZTIMER_MSEC, &c->co.timer);
408}
409
416{
417 return co->ticks;
418}
419
429 ble_npl_time_t time)
430{
431 (void)time;
433 return (ble_npl_time_t)(co->ticks - now);
434}
435
442static inline void ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
443{
444 co->co.c_ev.arg = arg;
445}
446
453{
454 return os_time_get();
455}
456
465static inline ble_npl_error_t ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
466{
467 return (ble_npl_error_t)os_time_ms_to_ticks(ms, out_ticks);
468}
469
478static inline ble_npl_error_t ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
479{
480 return (ble_npl_error_t)os_time_ticks_to_ms(ticks, out_ms);
481}
482
491{
492 return os_time_ms_to_ticks32(ms);
493}
494
503{
504 return os_time_ticks_to_ms32(ticks);
505}
506
511static inline void ble_npl_time_delay(ble_npl_time_t ticks)
512{
513 return os_time_delay(ticks);
514}
515
521static inline uint32_t ble_npl_hw_enter_critical(void)
522{
523 return os_hw_enter_critical();
524}
525
531static inline void ble_npl_hw_exit_critical(uint32_t ctx)
532{
534}
535
541static inline bool ble_npl_hw_is_in_critical(void)
542{
543 return os_hw_is_in_critical();
544}
545
551static inline void *ble_npl_get_current_task_id(void)
552{
553 return (void *)(uint32_t)thread_getpid();
554}
555
562static inline void ble_npl_hw_set_isr(int irqn, void (*addr)(void))
563{
564 nrf5x_hw_set_isr(irqn, addr);
565}
566
567/* XXX: these functions are required to build hal_timer.c, however with the
568* default configuration they are never used... */
569#if defined(CPU_FAM_NRF51) || defined(CPU_FAM_NRF52)
570static inline void
571nrf52_clock_hfxo_request(void)
572{
574}
575
576static inline void
577nrf52_clock_hfxo_release(void)
578{
580}
581#endif
582
583#ifdef __cplusplus
584}
585#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:436
uint32_t ztimer_now_t
type for ztimer_now() result
Definition ztimer.h:308
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition ztimer.h:680
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.
static bool ble_npl_os_started(void)
Not used in RIOT.
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 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.
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.
int32_t ble_npl_stime_t
time type
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_get_current_task_id(void)
Return current thread PID.
static void ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn, void *arg)
Init a 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
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 struct ble_npl_event * ble_npl_eventq_get_no_wait(struct ble_npl_eventq *evq)
Get next event from event queue, non-blocking.
static void ble_npl_eventq_run(struct ble_npl_eventq *evq)
Gets and runs an event from the queue callback.
static void * ble_npl_event_get_arg(struct ble_npl_event *ev)
Runs an event.
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 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.
mynewt-core error types
static bool os_hw_is_in_critical(void)
Check if is in critical section.
Definition os.h:94
static void os_hw_exit_critical(uint32_t ctx)
Restores ISR context.
Definition os.h:84
static uint32_t os_hw_enter_critical(void)
Disable ISRs.
Definition os.h:73
static bool os_eventq_is_empty(struct os_eventq *evq)
Check if queue is empty.
Definition os_eventq.h:223
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:174
static void os_event_init(struct os_event *ev, os_event_fn *fn, void *arg)
Init a event.
Definition os_eventq.h:56
static void os_event_set_arg(struct os_event *ev, void *arg)
Set the event argument.
Definition os_eventq.h:96
static int os_eventq_inited(struct os_eventq *evq)
Check whether the event queue is initialized.
Definition os_eventq.h:126
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:152
static void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
Remove an event from the queue.
Definition os_eventq.h:200
static bool os_event_is_queued(struct os_event *ev)
Check if event is in queue.
Definition os_eventq.h:75
void os_event_fn(struct os_event *ev)
Event callback function.
Definition os_eventq.h:47
static void os_event_run(struct os_event *ev)
Runs an event.
Definition os_eventq.h:106
static void os_eventq_init(struct os_eventq *evq)
Initialize the event queue.
Definition os_eventq.h:116
static void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
Put an event on the event queue.
Definition os_eventq.h:189
static void * os_event_get_arg(struct os_event *ev)
Returns event argument.
Definition os_eventq.h:85
static void os_eventq_run(struct os_eventq *evq)
Gets and runs an event from the queue callback.
Definition os_eventq.h:210
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:76
static os_time_t os_time_get(void)
Returns the low 32 bits of cputime.
Definition os_time.h:36
static void os_time_delay(os_time_t ticks)
Wait until the number of ticks has elapsed, BLOICKING.
Definition os_time.h:98
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:88
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:63
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:49
ble_npl callout wrapper
uint32_t ticks
the callout set timeout
struct os_callout co
the callout
ble_npl event wrapper
struct os_event ev
the event
ble_npl event queue wrapper
struct os_eventq evq
the event queue
ble_npl mutex wrapper
struct os_mutex mu
mutex
ble_npl semaphore wrapper
struct os_sem sem
semaphore
Event wrapper.
Definition os_eventq.h:31
Event queue wrapper.
Definition os_eventq.h:40