1 /*
2  * SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF)
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * SPDX-FileContributor: 2019-2022 Espressif Systems (Shanghai) CO LTD
7  */
8 
9 #ifndef _NPL_FREERTOS_H_
10 #define _NPL_FREERTOS_H_
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 #include "sdkconfig.h"
16 
17 #if ((defined(CONFIG_BT_NIMBLE_USE_ESP_TIMER) && CONFIG_BT_NIMBLE_USE_ESP_TIMER) || \
18      (defined(CONFIG_BT_LE_USE_ESP_TIMER) && CONFIG_BT_LE_USE_ESP_TIMER))
19 /* Use esp timer instead of FreeRTOS timer to implement the callout. */
20 #define BLE_NPL_USE_ESP_TIMER       (1)
21 #else
22 #define BLE_NPL_USE_ESP_TIMER       (0)
23 #endif
24 
25 typedef struct {
26     uint16_t evt_count;
27     uint16_t evtq_count;
28     uint16_t co_count;
29     uint16_t sem_count;
30     uint16_t mutex_count;
31 } ble_npl_count_info_t;
32 
33 typedef void ble_npl_event_fn(struct ble_npl_event *ev);
34 
35 struct ble_npl_event_freertos {
36     bool queued;
37     ble_npl_event_fn *fn;
38     void *arg;
39 };
40 
41 struct ble_npl_eventq_freertos {
42     QueueHandle_t q;
43 };
44 
45 struct ble_npl_callout_freertos {
46 #if BLE_NPL_USE_ESP_TIMER
47    esp_timer_handle_t handle;
48 #else
49     TimerHandle_t handle;
50 #endif
51     struct ble_npl_eventq *evq;
52     struct ble_npl_event ev;
53 };
54 
55 struct ble_npl_mutex_freertos {
56     SemaphoreHandle_t handle;
57 };
58 
59 struct ble_npl_sem_freertos {
60     SemaphoreHandle_t handle;
61 };
62 
63 typedef void ble_npl_event_fn_freertos(struct ble_npl_event_freertos *ev);
64 
65 struct ble_npl_eventq *npl_freertos_eventq_dflt_get(void);
66 
67 struct ble_npl_event *npl_freertos_eventq_get(struct ble_npl_eventq *evq,
68                                               ble_npl_time_t tmo);
69 
70 void npl_freertos_eventq_put(struct ble_npl_eventq *evq,
71                              struct ble_npl_event *ev);
72 
73 void npl_freertos_eventq_remove(struct ble_npl_eventq *evq,
74                                 struct ble_npl_event *ev);
75 
76 ble_npl_error_t npl_freertos_mutex_init(struct ble_npl_mutex *mu);
77 ble_npl_error_t npl_freertos_mutex_deinit(struct ble_npl_mutex *mu);
78 
79 ble_npl_error_t npl_freertos_mutex_pend(struct ble_npl_mutex *mu,
80                                         ble_npl_time_t timeout);
81 
82 ble_npl_error_t npl_freertos_mutex_release(struct ble_npl_mutex *mu);
83 
84 ble_npl_error_t npl_freertos_sem_init(struct ble_npl_sem *sem, uint16_t tokens);
85 ble_npl_error_t npl_freertos_sem_deinit(struct ble_npl_sem *sem);
86 
87 ble_npl_error_t npl_freertos_sem_pend(struct ble_npl_sem *sem,
88                                       ble_npl_time_t timeout);
89 
90 ble_npl_error_t npl_freertos_sem_release(struct ble_npl_sem *sem);
91 
92 int npl_freertos_callout_init(struct ble_npl_callout *co,
93                                struct ble_npl_eventq *evq,
94                                ble_npl_event_fn *ev_cb, void *ev_arg);
95 
96 void npl_freertos_callout_deinit(struct ble_npl_callout *co);
97 
98 void npl_freertos_callout_stop(struct ble_npl_callout *co);
99 
100 bool npl_freertos_callout_is_active(struct ble_npl_callout *co);
101 
102 ble_npl_time_t npl_freertos_callout_get_ticks(struct ble_npl_callout *co);
103 
104 ble_npl_error_t npl_freertos_callout_reset(struct ble_npl_callout *co,
105                                            ble_npl_time_t ticks);
106 
107 ble_npl_time_t npl_freertos_callout_remaining_ticks(struct ble_npl_callout *co,
108 						    ble_npl_time_t now);
109 
110 ble_npl_error_t npl_freertos_time_ms_to_ticks(uint32_t ms,
111                                               ble_npl_time_t *out_ticks);
112 
113 ble_npl_error_t npl_freertos_time_ticks_to_ms(ble_npl_time_t ticks,
114                                               uint32_t *out_ms);
115 
116 
117 uint32_t npl_freertos_hw_enter_critical(void);
118 
119 void npl_freertos_hw_exit_critical(uint32_t ctx);
120 
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 #endif  /* _NPL_FREERTOS_H_ */
126