1 /*
2  * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __ESP_COEXIST_INTERNAL_H__
8 #define __ESP_COEXIST_INTERNAL_H__
9 
10 #include <stdbool.h>
11 #include "esp_coexist.h"
12 #include "private/esp_coexist_adapter.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 typedef enum {
19     COEX_PREFER_WIFI = 0,
20     COEX_PREFER_BT,
21     COEX_PREFER_BALANCE,
22     COEX_PREFER_NUM,
23 } coex_prefer_t;
24 
25 typedef enum {
26     COEX_SCHM_CALLBACK_TYPE_WIFI = 0,
27     COEX_SCHM_CALLBACK_TYPE_BT,
28     COEX_SCHM_CALLBACK_TYPE_I154,
29 } coex_schm_callback_type_t;
30 
31 typedef enum {
32     COEX_SCHM_ST_TYPE_WIFI = 0,
33     COEX_SCHM_ST_TYPE_BLE,
34     COEX_SCHM_ST_TYPE_BT,
35 } coex_schm_st_type_t;
36 
37 #define COEX_STATUS_GET_WIFI_BITMAP     (1 << COEX_SCHM_ST_TYPE_WIFI)
38 #define COEX_STATUS_GET_BLE_BITMAP      (1 << COEX_SCHM_ST_TYPE_BLE)
39 #define COEX_STATUS_GET_BT_BITMAP       (1 << COEX_SCHM_ST_TYPE_BT)
40 
41 typedef void (* coex_func_cb_t)(uint32_t event, int sched_cnt);
42 typedef esp_err_t (* coex_set_lpclk_source_callback_t)(void);
43 typedef void (* coex_wifi_channel_change_cb_t)(uint8_t primary, uint8_t secondary);
44 
45 /**
46  * @brief Pre-Init software coexist
47  *        extern function for internal use.
48  *
49  * @return Init ok or failed.
50  */
51 esp_err_t coex_pre_init(void);
52 
53 /**
54  * @brief Init software coexist
55  *        extern function for internal use.
56  *
57  * @return Init ok or failed.
58  */
59 esp_err_t coex_init(void);
60 
61 /**
62  * @brief De-init software coexist
63  *        extern function for internal use.
64  */
65 void coex_deinit(void);
66 
67 /**
68  * @brief Enable software coexist
69  *        extern function for internal use.
70  *
71  * @return Enable ok or failed.
72  */
73 esp_err_t coex_enable(void);
74 
75 /**
76  * @brief Disable software coexist
77  *        extern function for internal use.
78  */
79 void coex_disable(void);
80 
81 /**
82  * @brief Get software coexist version string
83  *        extern function for internal use.
84  * @return : version string
85  */
86 const char *coex_version_get(void);
87 
88 /**
89  * @brief Get software coexist version value
90  *        extern function for internal use.
91  * @param ptr_version : points to version structure
92  * @return : ESP_OK - success, other - failed
93  */
94 esp_err_t coex_version_get_value(coex_version_t* ptr_version);
95 
96 /**
97  * @brief Coexist performance preference set from libbt.a
98  *        extern function for internal use.
99  *
100  *  @param prefer : the prefer enumeration value
101  *  @return : ESP_OK - success, other - failed
102  */
103 esp_err_t coex_preference_set(coex_prefer_t prefer);
104 
105 /**
106  * @brief Get software coexist status.
107  *
108  * @param bitmap : bitmap of the module getting status.
109  * @return : software coexist status
110  */
111 uint32_t coex_status_get(uint8_t bitmap);
112 
113 /**
114  * @brief WiFi requests coexistence.
115  *
116  *  @param event : WiFi event
117  *  @param latency : WiFi will request coexistence after latency
118  *  @param duration : duration for WiFi to request coexistence
119  *  @return : 0 - success, other - failed
120  */
121 int coex_wifi_request(uint32_t event, uint32_t latency, uint32_t duration);
122 
123 /**
124  * @brief WiFi release coexistence.
125  *
126  *  @param event : WiFi event
127  *  @return : 0 - success, other - failed
128  */
129 int coex_wifi_release(uint32_t event);
130 
131 /**
132  * @brief Set WiFi channel to coexistence module.
133  *
134  *  @param primary : WiFi primary channel
135  *  @param secondary : WiFi secondary channel
136  *  @return : 0 - success, other - failed
137  */
138 int coex_wifi_channel_set(uint8_t primary, uint8_t secondary);
139 
140 /**
141  * @brief Get WiFi channel from coexistence module.
142  *
143  *  @param primary : pointer to value of WiFi primary channel
144  *  @param secondary : pointer to value of WiFi secondary channel
145  *  @return : 0 - success, other - failed
146  */
147 int coex_wifi_channel_get(uint8_t *primary, uint8_t *secondary);
148 
149 /**
150  * @brief Register application callback function to Wi-Fi update low power clock module.
151  *
152  * @param callback : Wi-Fi update low power clock callback function
153  */
154 void coex_wifi_register_update_lpclk_callback(coex_set_lpclk_source_callback_t callback);
155 
156 /**
157  * @brief Bluetooth requests coexistence
158  *
159  *  @param event : Bluetooth event
160  *  @param latency : Bluetooth will request coexistence after latency
161  *  @param duration : duration for Bluetooth to request coexistence
162  *  @return : 0 - success, other - failed
163  */
164 int coex_bt_request(uint32_t event, uint32_t latency, uint32_t duration);
165 
166 /**
167  * @brief Bluetooth release coexistence.
168  *
169  *  @param event : Bluetooth event
170  *  @return : 0 - success, other - failed
171  */
172 int coex_bt_release(uint32_t event);
173 
174 #if CONFIG_IDF_TARGET_ESP32
175 /**
176  * @brief Bluetooth registers callback function to coexistence module
177  *        This function is only used on ESP32.
178  *
179  *  @param callback: callback function registered to coexistence module
180  *  @return : 0 - success, other - failed
181  */
182 int coex_register_bt_cb(coex_func_cb_t callback);
183 
184 /**
185  * @brief To acquire the spin-lock used in resetting Bluetooth baseband.
186  *        This function is only used to workaround ESP32 hardware issue.
187  *
188  *  @return : value of the spinlock to be restored
189  */
190 uint32_t coex_bb_reset_lock(void);
191 
192 /**
193  * @brief To release the spin-lock used in resetting Bluetooth baseband.
194  *        This function is only used to workaround ESP32 hardware issue.
195  *
196  *  @param restore: value of the spinlock returned from previous call of coex_bb_rest_lock
197  */
198 void coex_bb_reset_unlock(uint32_t restore);
199 #endif /* CONFIG_IDF_TARGET_ESP32 */
200 
201 /**
202  * @brief Bluetooth registers callback function to receive notification when Wi-Fi channel changes
203  *
204  *  @param callback: callback function registered to coexistence module
205  *  @return : 0 - success, other - failed
206  */
207 int coex_register_wifi_channel_change_callback(coex_wifi_channel_change_cb_t callback);
208 
209 /**
210  * @brief Update low power clock interval
211  */
212 void coex_update_lpclk_interval(void);
213 
214 /**
215  * @brief Get coexistence event duration.
216  *
217  *  @param event : Coexistence event
218  *  @param duration: Coexistence event duration
219  *  @return : 0 - success, other - failed
220  */
221 int coex_event_duration_get(uint32_t event, uint32_t *duration);
222 
223 #if SOC_COEX_HW_PTI
224 /**
225  * @brief Get coexistence event priority.
226  *
227  *  @param event : Coexistence event
228  *  @param pti: Coexistence event priority
229  *  @return : 0 - success, other - failed
230  */
231 int coex_pti_get(uint32_t event, uint8_t *pti);
232 #endif
233 
234 /**
235  * @brief Clear coexistence status.
236  *
237  *  @param type : Coexistence status type
238  *  @param status: Coexistence status
239  */
240 void coex_schm_status_bit_clear(uint32_t type, uint32_t status);
241 
242 /**
243  * @brief Set coexistence status.
244  *
245  *  @param type : Coexistence status type
246  *  @param status: Coexistence status
247  */
248 void coex_schm_status_bit_set(uint32_t type, uint32_t status);
249 
250 /**
251  * @brief Set coexistence scheme interval.
252  *
253  *  @param interval : Coexistence scheme interval
254  *  @return : 0 - success, other - failed
255  */
256 int coex_schm_interval_set(uint32_t interval);
257 
258 /**
259  * @brief Get coexistence scheme interval.
260  *
261  *  @return : Coexistence scheme interval
262  */
263 uint32_t coex_schm_interval_get(void);
264 
265 /**
266  * @brief Get current coexistence scheme period.
267  *
268  *  @return : Coexistence scheme period
269  */
270 uint8_t coex_schm_curr_period_get(void);
271 
272 /**
273  * @brief Get current coexistence scheme phase.
274  *
275  *  @return : Coexistence scheme phase
276  */
277 void * coex_schm_curr_phase_get(void);
278 
279 /**
280  * @brief Set current coexistence scheme phase index.
281  *
282  *  @param idx : Coexistence scheme phase index
283  *  @return : 0 - success, other - failed
284  */
285 int coex_schm_curr_phase_idx_set(int idx);
286 
287 /**
288  * @brief Get current coexistence scheme phase index.
289  *
290  *  @return : Coexistence scheme phase index
291  */
292 int coex_schm_curr_phase_idx_get(void);
293 
294 /**
295  * @brief Register WiFi callback for coexistence starts.
296  *
297  *  @param cb : WiFi callback
298  *  @return : 0 - success, other - failed
299  */
300 int coex_register_start_cb(int (* cb)(void));
301 
302 /**
303  * @brief Restart current coexistence scheme.
304  *
305  *  @return : 0 - success, other - failed
306  */
307 int coex_schm_process_restart(void);
308 
309 /**
310  * @brief Register callback for coexistence scheme.
311  *
312  *  @param type : callback type
313  *  @param callback : callback
314  *  @return : 0 - success, other - failed
315  */
316 int coex_schm_register_callback(coex_schm_callback_type_t type, void *callback);
317 
318 /**
319  * @brief Register coexistence adapter functions.
320  *
321  *  @param funcs : coexistence adapter functions
322  *  @return : ESP_OK - success, other - failed
323  */
324 esp_err_t esp_coex_adapter_register(coex_adapter_funcs_t *funcs);
325 
326 #if CONFIG_EXTERNAL_COEX_ENABLE
327 /**
328   * @brief     Set external coexistence advanced information, like working mode.
329   *
330   * @param     out_pti1    This parameter no longer works, will be deprecated and later removed in future releases.
331   * @param     out_pti2    This parameter no longer works, will be deprecated and later removed in future releases.
332   *
333   * @return
334   *    - ESP_OK: succeed
335   */
336 esp_err_t esp_coex_external_params(esp_external_coex_advance_t coex_info, uint32_t out_pti1, uint32_t out_pti2);
337 
338 /**
339   * @brief     Set external coexistence pti level and enable it.
340   *
341   * @param     level1    external coex low pti
342   * @param     level2    external coex mid pti
343   * @param     level3    external coex high pti
344   *
345   * @return
346   *    - ESP_OK: succeed
347   */
348 esp_err_t esp_coex_external_set(esp_coex_pti_level_t level1,
349          esp_coex_pti_level_t level2, esp_coex_pti_level_t level3);
350 
351 /**
352   * @brief     Disable external coexist
353   *
354   * @return
355   *    - ESP_OK: succeed
356   */
357 void esp_coex_external_stop(void);
358 
359 /**
360  * @brief Set external coexistence wire type.
361  *
362  * @param wire_type Set external coexistence wire type.
363  *
364  */
365 void esp_coex_external_set_wire_type(external_coex_wire_t wire_type);
366 
367 #if SOC_EXTERNAL_COEX_LEADER_TX_LINE
368 /**
369   * @brief     Enable external coexist tx line
370   *
371   * @param     en    Enable external coex tx line
372   *
373   * @return
374   *    - ESP_OK: succeed
375   */
376 void esp_coex_external_set_txline(bool en);
377 #endif    /*SOC_EXTERNAL_COEX_LEADER_TX_LINE*/
378 #endif    /*External Coex*/
379 
380 #if CONFIG_ESP_COEX_POWER_MANAGEMENT
381 /**
382   * @brief     Set coexist scheme flexible period
383   *
384   * @param     period    flexible period
385   *
386   * @return
387   *    - ESP_OK: succeed
388   */
389 int coex_schm_flexible_period_set(uint8_t period);
390 
391 /**
392   * @brief     Get coexist scheme flexible period
393   *
394   * @return    Coexist scheme flexible period
395   */
396 uint8_t coex_schm_flexible_period_get(void);
397 #endif
398 
399 /**
400   * @brief     Check the MD5 values of the coexistence adapter header files in IDF and WiFi library
401   *
402   * @attention 1. It is used for internal CI version check
403   *
404   * @return
405   *     - ESP_OK : succeed
406   *     - ESP_WIFI_INVALID_ARG : MD5 check fail
407   */
408 esp_err_t esp_coex_adapter_funcs_md5_check(const char *md5);
409 
410 #ifdef __cplusplus
411 }
412 #endif
413 
414 #endif /* __ESP_COEXIST_INTERNAL_H__ */
415