20#ifndef _NIMBLE_NPL_OS_H_
21#define _NIMBLE_NPL_OS_H_
22#include "nimble/porting/nimble/include/syscfg/syscfg.h"
24#if CONFIG_NIMBLE_STACK_USE_MEM_POOLS
29#include "freertos/FreeRTOS.h"
30#include "freertos/queue.h"
31#include "freertos/semphr.h"
32#include "freertos/task.h"
33#include "freertos/timers.h"
41#define ARRAY_SIZE(array) \
42 (sizeof(array) / sizeof((array)[0]))
46extern int ets_printf(
const char *fmt, ...);
48#define ets_printf printf
50#define NIMBLE_CFG_CONTROLLER 1
53#define PLATFORM_BLE_LL_ASSERT(con) \
56 ets_printf("assertion:%s\n",#con); \
57 ets_printf("line:%d,function:%s\n", __LINE__, __func__);\
62#define BLE_NPL_OS_ALIGNMENT (4)
64#define BLE_NPL_TIME_FOREVER ble_npl_get_time_forever()
67typedef uint32_t ble_npl_time_t;
68typedef int32_t ble_npl_stime_t;
71typedef void ble_npl_event_fn(
struct ble_npl_event *ev);
77struct ble_npl_eventq {
81struct ble_npl_callout {
99#include "npl_freertos.h"
102 bool (*p_ble_npl_os_started)(void);
103 void *(*p_ble_npl_get_current_task_id)(void);
104 void (*p_ble_npl_eventq_init)(
struct ble_npl_eventq *);
105 void (*p_ble_npl_eventq_deinit)(
struct ble_npl_eventq *);
106 struct ble_npl_event * (*p_ble_npl_eventq_get)(
struct ble_npl_eventq *, ble_npl_time_t);
107 void (*p_ble_npl_eventq_put)(
struct ble_npl_eventq *,
struct ble_npl_event *);
108 void (*p_ble_npl_eventq_remove)(
struct ble_npl_eventq *,
struct ble_npl_event *);
109 void (*p_ble_npl_event_run)(
struct ble_npl_event *);
110 bool (*p_ble_npl_eventq_is_empty)(
struct ble_npl_eventq *);
111 void (*p_ble_npl_event_init)(
struct ble_npl_event *, ble_npl_event_fn *,
void *);
112 void (*p_ble_npl_event_deinit)(
struct ble_npl_event *);
113 void (*p_ble_npl_event_reset)(
struct ble_npl_event *);
114 bool (*p_ble_npl_event_is_queued)(
struct ble_npl_event *);
115 void * (*p_ble_npl_event_get_arg)(
struct ble_npl_event *);
116 void (*p_ble_npl_event_set_arg)(
struct ble_npl_event *,
void *);
117 ble_npl_error_t (*p_ble_npl_mutex_init)(
struct ble_npl_mutex *);
118 ble_npl_error_t (*p_ble_npl_mutex_deinit)(
struct ble_npl_mutex *);
119 ble_npl_error_t (*p_ble_npl_mutex_pend)(
struct ble_npl_mutex *, ble_npl_time_t);
120 ble_npl_error_t (*p_ble_npl_mutex_release)(
struct ble_npl_mutex *);
121 ble_npl_error_t (*p_ble_npl_sem_init)(
struct ble_npl_sem *, uint16_t);
122 ble_npl_error_t (*p_ble_npl_sem_deinit)(
struct ble_npl_sem *);
123 ble_npl_error_t (*p_ble_npl_sem_pend)(
struct ble_npl_sem *, ble_npl_time_t);
124 ble_npl_error_t (*p_ble_npl_sem_release)(
struct ble_npl_sem *);
125 uint16_t (*p_ble_npl_sem_get_count)(
struct ble_npl_sem *);
126 int (*p_ble_npl_callout_init)(
struct ble_npl_callout *,
struct ble_npl_eventq *, ble_npl_event_fn *,
void *);
127 ble_npl_error_t (*p_ble_npl_callout_reset)(
struct ble_npl_callout *, ble_npl_time_t);
128 void (*p_ble_npl_callout_stop)(
struct ble_npl_callout *);
129 void (*p_ble_npl_callout_deinit)(
struct ble_npl_callout *);
130 void (*p_ble_npl_callout_mem_reset)(
struct ble_npl_callout *);
131 bool (*p_ble_npl_callout_is_active)(
struct ble_npl_callout *);
132 ble_npl_time_t (*p_ble_npl_callout_get_ticks)(
struct ble_npl_callout *);
133 uint32_t (*p_ble_npl_callout_remaining_ticks)(
struct ble_npl_callout *, ble_npl_time_t);
134 void (*p_ble_npl_callout_set_arg)(
struct ble_npl_callout *,
void *);
135 uint32_t (*p_ble_npl_time_get)(void);
136 ble_npl_error_t (*p_ble_npl_time_ms_to_ticks)(uint32_t ms, ble_npl_time_t *);
137 ble_npl_error_t (*p_ble_npl_time_ticks_to_ms)(ble_npl_time_t, uint32_t *);
138 ble_npl_time_t (*p_ble_npl_time_ms_to_ticks32)(uint32_t);
139 uint32_t (*p_ble_npl_time_ticks_to_ms32)(ble_npl_time_t);
140 void (*p_ble_npl_time_delay)(ble_npl_time_t);
142 void (*p_ble_npl_hw_set_isr)(int, uint32_t);
144 void (*p_ble_npl_hw_set_isr)(int, void (*addr)(void));
146 uint32_t (*p_ble_npl_hw_enter_critical)(void);
147 void (*p_ble_npl_hw_exit_critical)(uint32_t);
148 uint32_t (*p_ble_npl_get_time_forever)(void);
149 uint8_t (*p_ble_npl_hw_is_in_critical)(void);
152extern struct npl_funcs_t *npl_funcs;
155ble_npl_os_started(
void)
157 return npl_funcs->p_ble_npl_os_started();
161ble_npl_get_current_task_id(
void)
163 return npl_funcs->p_ble_npl_get_current_task_id();
167ble_npl_eventq_init(
struct ble_npl_eventq *evq)
169 return npl_funcs->p_ble_npl_eventq_init(evq);
173ble_npl_eventq_deinit(
struct ble_npl_eventq *evq)
175 return npl_funcs->p_ble_npl_eventq_deinit(evq);
178static inline struct ble_npl_event *
179ble_npl_eventq_get(
struct ble_npl_eventq *evq, ble_npl_time_t tmo)
181 return npl_funcs->p_ble_npl_eventq_get(evq, tmo);
185ble_npl_eventq_put(
struct ble_npl_eventq *evq,
struct ble_npl_event *ev)
187 return npl_funcs->p_ble_npl_eventq_put(evq, ev);
191ble_npl_eventq_remove(
struct ble_npl_eventq *evq,
struct ble_npl_event *ev)
193 return npl_funcs->p_ble_npl_eventq_remove(evq, ev);
197ble_npl_event_run(
struct ble_npl_event *ev)
199 return npl_funcs->p_ble_npl_event_run(ev);
203ble_npl_eventq_is_empty(
struct ble_npl_eventq *evq)
205 return npl_funcs->p_ble_npl_eventq_is_empty(evq);
209ble_npl_event_init(
struct ble_npl_event *ev, ble_npl_event_fn *fn,
212 return npl_funcs->p_ble_npl_event_init(ev, fn, arg);
216ble_npl_event_is_queued(
struct ble_npl_event *ev)
218 return npl_funcs->p_ble_npl_event_is_queued(ev);
222ble_npl_event_get_arg(
struct ble_npl_event *ev)
224 return npl_funcs->p_ble_npl_event_get_arg(ev);
228ble_npl_event_set_arg(
struct ble_npl_event *ev,
void *arg)
230 return npl_funcs->p_ble_npl_event_set_arg(ev, arg);
233static inline ble_npl_error_t
234ble_npl_mutex_init(
struct ble_npl_mutex *mu)
236 return npl_funcs->p_ble_npl_mutex_init(mu);
239static inline ble_npl_error_t
240ble_npl_mutex_deinit(
struct ble_npl_mutex *mu)
242 return npl_funcs->p_ble_npl_mutex_deinit(mu);
245static inline ble_npl_error_t
246ble_npl_mutex_pend(
struct ble_npl_mutex *mu, ble_npl_time_t timeout)
248 return npl_funcs->p_ble_npl_mutex_pend(mu, timeout);
251static inline ble_npl_error_t
252ble_npl_mutex_release(
struct ble_npl_mutex *mu)
254 return npl_funcs->p_ble_npl_mutex_release(mu);
257static inline ble_npl_error_t
258ble_npl_sem_init(
struct ble_npl_sem *sem, uint16_t tokens)
260 return npl_funcs->p_ble_npl_sem_init(sem, tokens);
263static inline ble_npl_error_t
264ble_npl_sem_deinit(
struct ble_npl_sem *sem)
266 return npl_funcs->p_ble_npl_sem_deinit(sem);
269static inline ble_npl_error_t
270ble_npl_sem_pend(
struct ble_npl_sem *sem, ble_npl_time_t timeout)
272 return npl_funcs->p_ble_npl_sem_pend(sem, timeout);
275static inline ble_npl_error_t
276ble_npl_sem_release(
struct ble_npl_sem *sem)
278 return npl_funcs->p_ble_npl_sem_release(sem);
281static inline uint16_t
282ble_npl_sem_get_count(
struct ble_npl_sem *sem)
284 return npl_funcs->p_ble_npl_sem_get_count(sem);
288ble_npl_callout_init(
struct ble_npl_callout *co,
struct ble_npl_eventq *evq,
289 ble_npl_event_fn *ev_cb,
void *ev_arg)
291 return npl_funcs->p_ble_npl_callout_init(co, evq, ev_cb, ev_arg);
294ble_npl_callout_deinit(
struct ble_npl_callout *co)
296 return npl_funcs->p_ble_npl_callout_deinit(co);
299static inline ble_npl_error_t
300ble_npl_callout_reset(
struct ble_npl_callout *co, ble_npl_time_t ticks)
302 return npl_funcs->p_ble_npl_callout_reset(co, ticks);
306ble_npl_callout_stop(
struct ble_npl_callout *co)
308 return npl_funcs->p_ble_npl_callout_stop(co);
312ble_npl_callout_is_active(
struct ble_npl_callout *co)
314 return npl_funcs->p_ble_npl_callout_is_active(co);
317static inline ble_npl_time_t
318ble_npl_callout_get_ticks(
struct ble_npl_callout *co)
320 return npl_funcs->p_ble_npl_callout_get_ticks(co);
323static inline ble_npl_time_t
324ble_npl_callout_remaining_ticks(
struct ble_npl_callout *co,
327 return npl_funcs->p_ble_npl_callout_remaining_ticks(co, time);
331ble_npl_callout_set_arg(
struct ble_npl_callout *co,
void *arg)
333 return npl_funcs->p_ble_npl_callout_set_arg(co, arg);
336static inline ble_npl_time_t
337ble_npl_time_get(
void)
339 return npl_funcs->p_ble_npl_time_get();
342static inline ble_npl_error_t
343ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
345 return npl_funcs->p_ble_npl_time_ms_to_ticks(ms, out_ticks);
348static inline ble_npl_error_t
349ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
351 return npl_funcs->p_ble_npl_time_ticks_to_ms(ticks, out_ms);
354static inline ble_npl_time_t
355ble_npl_time_ms_to_ticks32(uint32_t ms)
357 return npl_funcs->p_ble_npl_time_ms_to_ticks32(ms);
360static inline uint32_t
361ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
363 return npl_funcs->p_ble_npl_time_ticks_to_ms32(ticks);
367ble_npl_time_delay(ble_npl_time_t ticks)
369 return npl_funcs->p_ble_npl_time_delay(ticks);
372#if NIMBLE_CFG_CONTROLLER
375ble_npl_hw_set_isr(
int irqn, uint32_t addr)
377 return npl_funcs->p_ble_npl_hw_set_isr(irqn, addr);
381IRAM_ATTR ble_npl_hw_set_isr(
int irqn,
void (*addr)(
void))
383 return npl_funcs->p_ble_npl_hw_set_isr(irqn, addr);
389static inline uint32_t
390ble_npl_hw_enter_critical(
void)
392 return npl_funcs->p_ble_npl_hw_enter_critical();
396ble_npl_hw_exit_critical(uint32_t ctx)
398 return npl_funcs->p_ble_npl_hw_exit_critical(ctx);
401static inline bool ble_npl_hw_is_in_critical(
void)
403 return npl_funcs->p_ble_npl_hw_is_in_critical();
406#define ble_npl_get_time_forever (*npl_funcs->p_ble_npl_get_time_forever)
407#define ble_npl_callout_mem_reset (*npl_funcs->p_ble_npl_callout_mem_reset)
408#define ble_npl_event_deinit (*npl_funcs->p_ble_npl_event_deinit)
409#define ble_npl_event_reset (*npl_funcs->p_ble_npl_event_reset)
420#include "freertos/FreeRTOS.h"
421#include "freertos/queue.h"
422#include "freertos/semphr.h"
423#include "freertos/task.h"
424#include "freertos/timers.h"
430#define BLE_NPL_OS_ALIGNMENT 4
432#define BLE_NPL_TIME_FOREVER portMAX_DELAY
436#define NIMBLE_CFG_CONTROLLER 1
437#define NIMBLE_EVT_QUEUE_SIZE 4
439#define NIMBLE_EVT_QUEUE_SIZE 32
443typedef uint32_t ble_npl_time_t;
444typedef int32_t ble_npl_stime_t;
446struct ble_npl_event {
448 ble_npl_event_fn *fn;
452struct ble_npl_eventq {
456struct ble_npl_callout {
457#if CONFIG_BT_NIMBLE_USE_ESP_TIMER
458 esp_timer_handle_t handle;
460 TimerHandle_t handle;
462 struct ble_npl_eventq *evq;
463 struct ble_npl_event ev;
466struct ble_npl_mutex {
467 SemaphoreHandle_t handle;
471 SemaphoreHandle_t handle;
480#include "npl_freertos.h"
483ble_npl_os_started(
void)
485 return xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED;
489ble_npl_get_current_task_id(
void)
491 return xTaskGetCurrentTaskHandle();
495ble_npl_eventq_init(
struct ble_npl_eventq *evq)
497 evq->q = xQueueCreate(NIMBLE_EVT_QUEUE_SIZE,
sizeof(
struct ble_npl_eventq *));
501ble_npl_eventq_deinit(
struct ble_npl_eventq *evq)
503 vQueueDelete(evq->q);
506static inline struct ble_npl_event *
507ble_npl_eventq_get(
struct ble_npl_eventq *evq, ble_npl_time_t tmo)
509 return npl_freertos_eventq_get(evq, tmo);
513ble_npl_eventq_put(
struct ble_npl_eventq *evq,
struct ble_npl_event *ev)
515 npl_freertos_eventq_put(evq, ev);
519ble_npl_eventq_remove(
struct ble_npl_eventq *evq,
struct ble_npl_event *ev)
521 npl_freertos_eventq_remove(evq, ev);
525ble_npl_event_run(
struct ble_npl_event *ev)
531ble_npl_eventq_is_empty(
struct ble_npl_eventq *evq)
533 return xQueueIsQueueEmptyFromISR(evq->q);
537ble_npl_event_init(
struct ble_npl_event *ev, ble_npl_event_fn *fn,
540 memset(ev, 0,
sizeof(*ev));
546ble_npl_event_deinit(
struct ble_npl_event *ev)
552ble_npl_event_is_queued(
struct ble_npl_event *ev)
558ble_npl_event_get_arg(
struct ble_npl_event *ev)
564ble_npl_event_set_arg(
struct ble_npl_event *ev,
void *arg)
569static inline ble_npl_error_t
570ble_npl_mutex_init(
struct ble_npl_mutex *mu)
572 return npl_freertos_mutex_init(mu);
575static inline ble_npl_error_t
576ble_npl_mutex_deinit(
struct ble_npl_mutex *mu)
578 return npl_freertos_mutex_deinit(mu);
581static inline ble_npl_error_t
582ble_npl_mutex_pend(
struct ble_npl_mutex *mu, ble_npl_time_t timeout)
584 return npl_freertos_mutex_pend(mu, timeout);
587static inline ble_npl_error_t
588ble_npl_mutex_release(
struct ble_npl_mutex *mu)
590 return npl_freertos_mutex_release(mu);
593static inline ble_npl_error_t
594ble_npl_sem_init(
struct ble_npl_sem *sem, uint16_t tokens)
596 return npl_freertos_sem_init(sem, tokens);
599static inline ble_npl_error_t
600ble_npl_sem_deinit(
struct ble_npl_sem *sem)
602 return npl_freertos_sem_deinit(sem);
605static inline ble_npl_error_t
606ble_npl_sem_pend(
struct ble_npl_sem *sem, ble_npl_time_t timeout)
608 return npl_freertos_sem_pend(sem, timeout);
611static inline ble_npl_error_t
612ble_npl_sem_release(
struct ble_npl_sem *sem)
614 return npl_freertos_sem_release(sem);
617static inline uint16_t
618ble_npl_sem_get_count(
struct ble_npl_sem *sem)
620 return uxSemaphoreGetCount(sem->handle);
624ble_npl_callout_init(
struct ble_npl_callout *co,
struct ble_npl_eventq *evq,
625 ble_npl_event_fn *ev_cb,
void *ev_arg)
627 return npl_freertos_callout_init(co, evq, ev_cb, ev_arg);
631ble_npl_callout_deinit(
struct ble_npl_callout *co)
633 npl_freertos_callout_deinit(co);
636static inline ble_npl_error_t
637ble_npl_callout_reset(
struct ble_npl_callout *co, ble_npl_time_t ticks)
639 return npl_freertos_callout_reset(co, ticks);
643ble_npl_callout_stop(
struct ble_npl_callout *co)
645 npl_freertos_callout_stop(co);
649ble_npl_callout_is_active(
struct ble_npl_callout *co)
651 return npl_freertos_callout_is_active(co);
654static inline ble_npl_time_t
655ble_npl_callout_get_ticks(
struct ble_npl_callout *co)
657 return npl_freertos_callout_get_ticks(co);
660static inline uint32_t
661ble_npl_callout_remaining_ticks(
struct ble_npl_callout *co,
664 return npl_freertos_callout_remaining_ticks(co, time);
668ble_npl_callout_set_arg(
struct ble_npl_callout *co,
void *arg)
673static inline uint32_t
674ble_npl_time_get(
void)
676 return xTaskGetTickCountFromISR();
679static inline ble_npl_error_t
680ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
682 return npl_freertos_time_ms_to_ticks(ms, out_ticks);
685static inline ble_npl_error_t
686ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
688 return ble_npl_time_ticks_to_ms(ticks, out_ms);
691static inline ble_npl_time_t
692ble_npl_time_ms_to_ticks32(uint32_t ms)
694 return ms * configTICK_RATE_HZ / 1000;
697static inline uint32_t
698ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
700 return ticks * 1000 / configTICK_RATE_HZ;
704ble_npl_time_delay(ble_npl_time_t ticks)
709#if NIMBLE_CFG_CONTROLLER
711ble_npl_hw_set_isr(
int irqn,
void (*addr)(
void))
713 npl_freertos_hw_set_isr(irqn, addr);
717ble_npl_hw_is_in_critical(
void)
719 return (uxGetCriticalNestingDepth() > 0);
724extern portMUX_TYPE ble_port_mutex;
726static inline uint32_t
727ble_npl_hw_enter_critical(
void)
729 portENTER_CRITICAL(&ble_port_mutex);
734ble_npl_hw_exit_critical(uint32_t ctx)
736 portEXIT_CRITICAL(&ble_port_mutex);
740static inline uint32_t
741ble_npl_hw_enter_critical(
void)
743 vPortEnterCritical();
748ble_npl_hw_exit_critical(uint32_t ctx)