1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __ESP_COEXIST_H__
8 #define __ESP_COEXIST_H__
9 
10 #include <stdbool.h>
11 #include "esp_err.h"
12 #include "hal/gpio_types.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #define EXTERNAL_COEXIST_WIRE_1 0
19 #define EXTERNAL_COEXIST_WIRE_2 1
20 #define EXTERNAL_COEXIST_WIRE_3 2
21 #define EXTERNAL_COEXIST_WIRE_4 3
22 /**
23  * @brief coex prefer value
24  */
25 typedef enum {
26     ESP_COEX_PREFER_WIFI = 0,       /*!< Prefer to WiFi, WiFi will have more opportunity to use RF */
27     ESP_COEX_PREFER_BT,             /*!< Prefer to bluetooth, bluetooth will have more opportunity to use RF */
28     ESP_COEX_PREFER_BALANCE,        /*!< Do balance of WiFi and bluetooth */
29     ESP_COEX_PREFER_NUM,            /*!< Prefer value numbers */
30 } esp_coex_prefer_t;
31 
32 typedef enum {
33     EXTERN_COEX_WIRE_1 = EXTERNAL_COEXIST_WIRE_1,
34     EXTERN_COEX_WIRE_2 = EXTERNAL_COEXIST_WIRE_2,
35     EXTERN_COEX_WIRE_3 = EXTERNAL_COEXIST_WIRE_3,
36     EXTERN_COEX_WIRE_4 = EXTERNAL_COEXIST_WIRE_4,
37     EXTERN_COEX_WIRE_NUM,
38 } external_coex_wire_t;
39 
40 /**
41  * @brief coex status type
42  */
43 typedef enum {
44     ESP_COEX_ST_TYPE_WIFI = 0,
45     ESP_COEX_ST_TYPE_BLE,
46     ESP_COEX_ST_TYPE_BT,
47 } esp_coex_status_type_t;
48 
49 #if CONFIG_EXTERNAL_COEX_ENABLE
50 /**
51  * @brief external coex gpio pti
52  */
53 typedef struct {
54     union {
55         uint32_t in_pin0 __attribute__((deprecated("Use 'request' instead")));
56         gpio_num_t request; /**< request gpio signal from follower to leader */
57     };
58     union {
59         uint32_t in_pin1 __attribute__((deprecated("Use 'priority' instead")));
60         gpio_num_t priority; /**< request gpio signal priority from follower to leader */
61     };
62     union {
63         uint32_t out_pin0 __attribute__((deprecated("Use 'grant' instead")));
64         gpio_num_t grant; /**< grant gpio signal from leader to follower */
65     };
66     union {
67         uint32_t out_pin1 __attribute__((deprecated("Use 'tx_line' instead")));
68         gpio_num_t tx_line; /**< tx_line gpio signal from leader to follower, indicates whether the leader's WiFi is transmitting or not*/
69     };
70 } esp_external_coex_gpio_set_t;
71 
72 
73 /**
74  * @brief external coex pti level
75  */
76 typedef enum {
77     EXTERN_COEX_PTI_MID = 0,
78     EXTERN_COEX_PTI_HIGH,
79     EXTERN_COEX_PTI_NUM,
80 } esp_coex_pti_level_t;
81 
82 /**
83  * @brief external coex role
84  */
85 typedef enum {
86     EXTERNAL_COEX_LEADER_ROLE = 0,
87     EXTERNAL_COEX_FOLLOWER_ROLE = 2,
88     EXTERNAL_COEX_UNKNOWN_ROLE,
89 } esp_extern_coex_work_mode_t;
90 
91 /**
92  * @brief external coex advance setup
93  */
94 typedef struct {
95     esp_extern_coex_work_mode_t work_mode;
96     uint8_t delay_us;
97     bool is_high_valid;
98 } esp_external_coex_advance_t;
99 #endif
100 
101 #define ESP_COEX_BLE_ST_MESH_CONFIG        0x08
102 #define ESP_COEX_BLE_ST_MESH_TRAFFIC       0x10
103 #define ESP_COEX_BLE_ST_MESH_STANDBY       0x20
104 
105 #define ESP_COEX_BT_ST_A2DP_STREAMING      0x10
106 #define ESP_COEX_BT_ST_A2DP_PAUSED         0x20
107 
108 /**
109  * @brief Get software coexist version string
110  *
111  * @return : version string
112  */
113 const char *esp_coex_version_get(void);
114 
115 /**
116  * @deprecated Use esp_coex_status_bit_set() and esp_coex_status_bit_clear() instead.
117  *  Set coexist preference of performance
118  *  For example, if prefer to bluetooth, then it will make A2DP(play audio via classic bt)
119  *  more smooth while wifi is running something.
120  *  If prefer to wifi, it will do similar things as prefer to bluetooth.
121  *  Default, it prefer to balance.
122  *
123  *  @param prefer : the prefer enumeration value
124  *  @return : ESP_OK - success, other - failed
125  */
126 esp_err_t esp_coex_preference_set(esp_coex_prefer_t prefer);
127 
128 /**
129  * @brief Set coex schm status
130  * @param type : WIFI/BLE/BT
131  * @param status : WIFI/BLE/BT STATUS
132  * @return : ESP_OK - success, other - failed
133  */
134 esp_err_t esp_coex_status_bit_set(esp_coex_status_type_t type, uint32_t status);
135 
136 /**
137  * @brief Clear coex schm status
138  * @param type : WIFI/BLE/BT
139  * @param status : WIFI/BLE/BT STATUS
140  * @return : ESP_OK - success, other - failed
141  */
142 esp_err_t esp_coex_status_bit_clear(esp_coex_status_type_t type, uint32_t status);
143 
144 #if CONFIG_EXTERNAL_COEX_ENABLE
145 /**
146  * @brief Configure work mode, the default work mode is leader role.
147  * @param work_mode : work mode.
148  * @return : ESP_OK - success, other - failed
149  */
150 esp_err_t esp_external_coex_set_work_mode(esp_extern_coex_work_mode_t work_mode);
151 
152 /**
153  * @brief Setup gpio pin and corresponding pti level, start external coex,
154  *        the default work mode is leader role, the default output grant validate pin is high,
155  *        and the default delay output grant value is zero.
156  * @param wire_type : to select the whole external coex gpio number.
157  * @param gpio_pin : gpio pin number to choose.
158  * @return : ESP_OK - success, other - failed
159  */
160 esp_err_t esp_enable_extern_coex_gpio_pin(external_coex_wire_t wire_type,
161                      esp_external_coex_gpio_set_t gpio_pin);
162 
163 /**
164  * @brief Disable external coex.
165  * @return : ESP_OK - success, other - failed
166  */
167 esp_err_t esp_disable_extern_coex_gpio_pin(void);
168 
169 #if SOC_EXTERNAL_COEX_ADVANCE
170 /**
171  * @brief Configure leader work mode, gpio pin correspondly and finally enable external coex,
172  *        demand not to call the legacy function of `esp_enable_extern_coex_gpio_pin` any more.
173  * @param wire_type : to select the whole external coex gpio number.
174  * @param request : request gpio pin number to select.
175  * @param priority : priority gpio pin number to select.
176  * @param grant : grant gpio pin number to select.
177  * @return : ESP_OK - success, other - failed
178  */
179 esp_err_t esp_external_coex_leader_role_set_gpio_pin(external_coex_wire_t wire_type, uint32_t request, uint32_t priority,
180                 uint32_t grant) __attribute__((deprecated("Please use esp_external_coex_set_work_mode and esp_enable_extern_coex_gpio_pin instead")));
181 
182 /**
183  * @brief Configure follower work mode, gpio pin correspondly and finally enable external coex,
184  *        demand not to call the legacy function of `esp_enable_extern_coex_gpio_pin` any more.
185  * @param wire_type : to select the whole external coex gpio number.
186  * @param request : request gpio pin number to select.
187  * @param priority : priority gpio pin number to select.
188  * @param grant : grant gpio pin number to select.
189  * @return : ESP_OK - success, other - failed
190  */
191 esp_err_t esp_external_coex_follower_role_set_gpio_pin(external_coex_wire_t wire_type, uint32_t request, uint32_t priority,
192                 uint32_t grant) __attribute__((deprecated("Please use esp_external_coex_set_work_mode and esp_enable_extern_coex_gpio_pin instead")));
193 
194 /**
195  * @brief Configure output grant signal latency in delay microseconds only for leader role of external coex,
196  *        demand to call this function before `esp_external_coex_leader_role_set_gpio_pin`,
197  *        if users want to setup output delay value.
198  * @param delay_us : to setup how many microseconds the output signal performs latency.
199  * @return : ESP_OK - success, other - failed
200  */
201 esp_err_t esp_external_coex_set_grant_delay(uint8_t delay_us);
202 
203 /**
204  * @brief Configure output grant signal is high validate or not only for leader role of external coex,
205  *        demand to call this function before `esp_external_coex_leader_role_set_gpio_pin`,
206  *        if users want to setup output grant validate pin value.
207  * @param is_high_valid : to select true means the output grant signal validate is high, other - validate is low.
208  * @return : ESP_OK - success, other - failed
209  */
210 esp_err_t esp_external_coex_set_validate_high(bool is_high_valid);
211 #endif /* SOC_EXTERNAL_COEX_ADVANCE */
212 #endif /* CONFIG_EXTERNAL_COEX_ENABLE */
213 
214 #if CONFIG_ESP_COEX_SW_COEXIST_ENABLE && CONFIG_SOC_IEEE802154_SUPPORTED
215 /**
216  * @brief Enable Wi-Fi and 802.15.4 coexistence.
217  * @return : ESP_OK - success, other - failed
218  */
219 esp_err_t esp_coex_wifi_i154_enable(void);
220 #endif
221 
222 #if CONFIG_ESP_COEX_GPIO_DEBUG
223 /**
224  * @brief Enable coexist GPIO debug.
225  *        To fully enable this feature, make sure functions in rom_funcs are out of ROM.
226  * @return : ESP_OK - success, other - failed
227  */
228 esp_err_t esp_coexist_debug_init(void);
229 #endif
230 
231 #ifdef __cplusplus
232 }
233 #endif
234 
235 
236 #endif /* __ESP_COEXIST_H__ */
237