1 /*
2 * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8 #include "private/esp_coexist_debug.h"
9
10 #include <stdbool.h>
11 #include "esp_err.h"
12 #include "string.h"
13 #include "esp_attr.h"
14 #include "esp_log.h"
15
16 #include "rom/ets_sys.h"
17 #include "driver/gpio.h"
18 #include "soc/gpio_sig_map.h"
19 #include "esp_rom_gpio.h"
20 #include "soc/soc.h"
21 #if SOC_MODEM_CLOCK_IS_INDEPENDENT
22 #include "esp_private/esp_modem_clock.h"
23 #endif
24
25 #if CONFIG_ESP_COEX_GPIO_DEBUG
26 static char* TAG = "coexist debug";
27
wifi_set_gpio_debug_cb(void (* cb)(int,coex_gpio_debug_sig_t))28 __attribute__((weak)) void wifi_set_gpio_debug_cb(void (* cb)(int, coex_gpio_debug_sig_t))
29 {
30 ESP_LOGW(TAG, "Not support: %s", __FUNCTION__);
31 }
wifi_gpio_debug_max_event_get(void)32 __attribute__((weak)) int wifi_gpio_debug_max_event_get(void)
33 {
34 ESP_LOGW(TAG, "Not support: %s", __FUNCTION__);
35 return 0;
36 }
37
coex_set_gpio_debug_cb(void (* cb)(int,coex_gpio_debug_sig_t))38 __attribute__((weak)) void coex_set_gpio_debug_cb(void (*cb)(int, coex_gpio_debug_sig_t))
39 {
40 ESP_LOGW(TAG, "Not support: %s", __FUNCTION__);
41 }
42
coex_gpio_debug_max_event_get(void)43 __attribute__((weak)) int coex_gpio_debug_max_event_get(void)
44 {
45 ESP_LOGW(TAG, "Not support: %s", __FUNCTION__);
46 return 0;
47 }
48
coex_gpio_debug_matrix_init(void)49 __attribute__((weak)) esp_err_t coex_gpio_debug_matrix_init(void)
50 {
51 ESP_LOGW(TAG, "Not support: %s", __FUNCTION__);
52 return ESP_OK;
53 }
54
55 /* Check if functions in ROM */
56 static const void* rom_funcs[] = {
57 #if CONFIG_ESP_WIFI_ENABLED
58 lmacProcessTxComplete,
59 lmacTxFrame,
60 pm_update_by_connectionless_status,
61 pm_sleep,
62 pm_dream,
63 pm_beacon_monitor_timeout_process,
64 pm_connectionless_wake_window_timeout_process,
65 pm_coex_schm_process,
66 pm_tbtt_process,
67 pm_rx_beacon_process,
68 ppTask,
69 wDev_IndicateFrame,
70 pm_check_state,
71 pm_tx_null_data_done_process,
72 pm_start,
73 pm_stop,
74 pm_disconnected_wake,
75 #endif
76 };
77 static const char* rom_funcs_name[] = {
78 #if CONFIG_ESP_WIFI_ENABLED
79 "lmacProcessTxComplete",
80 "lmacTxframe",
81 "pm_update_by_connectionless_status",
82 "pm_sleep",
83 "pm_dream",
84 "pm_beacon_monitor_timeout_process",
85 "pm_connectionless_wake_window_timeout_process",
86 "pm_coex_schm_process",
87 "pm_tbtt_process",
88 "pm_rx_beacon_process",
89 "ppTask",
90 "wDev_IndicateFrame",
91 "pm_check_state",
92 "pm_tx_null_data_done_process",
93 "pm_start",
94 "pm_stop",
95 "pm_disconnected_wake",
96 #endif
97 };
98
check_funcs_in_rom(void)99 static bool check_funcs_in_rom(void)
100 {
101 bool in_rom = false;
102 for (uint8_t i = 0; i < sizeof(rom_funcs) / sizeof(void*); i++) {
103 if ((uint32_t)rom_funcs[i] >= SOC_IROM_MASK_LOW && (uint32_t)rom_funcs[i] <= SOC_IROM_MASK_HIGH) {
104 ESP_LOGE(TAG, "remove function from ROM: %s", rom_funcs_name[i]);
105 in_rom = true;
106 }
107 }
108 return in_rom;
109 }
110
111 /* Define used IO nums */
112 static const DRAM_ATTR gpio_num_t s_io_nums[COEX_GPIO_DEBUG_IO_COUNT_MAX] = {
113 COEX_GPIO_DEBUG_IO_IDX0,
114 COEX_GPIO_DEBUG_IO_IDX1,
115 COEX_GPIO_DEBUG_IO_IDX2,
116 COEX_GPIO_DEBUG_IO_IDX3,
117 COEX_GPIO_DEBUG_IO_IDX4,
118 COEX_GPIO_DEBUG_IO_IDX5,
119 COEX_GPIO_DEBUG_IO_IDX6,
120 COEX_GPIO_DEBUG_IO_IDX7,
121 COEX_GPIO_DEBUG_IO_IDX8,
122 COEX_GPIO_DEBUG_IO_IDX9,
123 COEX_GPIO_DEBUG_IO_IDX10,
124 COEX_GPIO_DEBUG_IO_IDX11,
125 };
126
127 /* Mapping from evt to IO */
128 static DRAM_ATTR gpio_num_t *s_evt_io_map, *s_wifi_evt_io_map, *s_coex_evt_io_map;
129 static DRAM_ATTR uint8_t s_wifi_evt_max, s_coex_evt_max;
130
bind_io_to_evt(gpio_num_t * ptrmap,uint8_t io,uint8_t evt)131 inline static void bind_io_to_evt(gpio_num_t *ptrmap, uint8_t io, uint8_t evt)
132 {
133 ptrmap[evt] = io;
134 }
135
evt_set_signal(gpio_num_t io,coex_gpio_debug_sig_t sig)136 inline static void evt_set_signal(gpio_num_t io, coex_gpio_debug_sig_t sig)
137 {
138 if (sig == COEX_GPIO_DEBUG_SIG_POSE) {
139 gpio_set_level(io, true);
140 } else if (sig == COEX_GPIO_DEBUG_SIG_NEGA) {
141 gpio_set_level(io, false);
142 } else {
143 gpio_set_level(io, true);
144 esp_rom_delay_us(COEX_GPIO_DEBUG_SIG_TO_DURATION(sig));
145 gpio_set_level(io, false);
146 }
147 }
148
wifi_bind_io_to_evt(uint8_t io_idx,uint8_t evt)149 void wifi_bind_io_to_evt(uint8_t io_idx, uint8_t evt)
150 {
151 if (!s_wifi_evt_io_map || evt >= s_wifi_evt_max || io_idx >= COEX_GPIO_DEBUG_IO_COUNT) {
152 return;
153 }
154 ESP_LOGI(TAG, "Bind IO %u to Wi-Fi evt %u", s_io_nums[io_idx], evt);
155 bind_io_to_evt(s_wifi_evt_io_map, s_io_nums[io_idx], evt);
156 }
157
coex_bind_io_to_evt(uint8_t io_idx,uint8_t evt)158 void coex_bind_io_to_evt(uint8_t io_idx, uint8_t evt)
159 {
160 if (!s_coex_evt_io_map || evt >= s_coex_evt_max || io_idx >= COEX_GPIO_DEBUG_IO_COUNT) {
161 return;
162 }
163 ESP_LOGI(TAG, "Bind IO %u to coexist evt %u", s_io_nums[io_idx], evt);
164 bind_io_to_evt(s_coex_evt_io_map, s_io_nums[io_idx], evt);
165 }
166
wifi_set_gpio_debug(int evt,coex_gpio_debug_sig_t sig)167 IRAM_ATTR void wifi_set_gpio_debug(int evt, coex_gpio_debug_sig_t sig)
168 {
169 if (evt >= s_wifi_evt_max || s_wifi_evt_io_map[evt] == COEX_GPIO_DEBUG_IO_INVALID) {
170 return;
171 }
172 evt_set_signal(s_wifi_evt_io_map[evt], sig);
173 }
174
coex_set_gpio_debug(int evt,coex_gpio_debug_sig_t sig)175 IRAM_ATTR void coex_set_gpio_debug(int evt, coex_gpio_debug_sig_t sig)
176 {
177 if (evt >= s_coex_evt_max || s_coex_evt_io_map[evt] == COEX_GPIO_DEBUG_IO_INVALID) {
178 return;
179 }
180 evt_set_signal(s_coex_evt_io_map[evt], sig);
181 }
182
esp_coexist_debug_matrix_init(int evt,int sig,bool rev)183 esp_err_t esp_coexist_debug_matrix_init(int evt, int sig, bool rev)
184 {
185 if (evt >= s_coex_evt_max || s_coex_evt_io_map[evt] == COEX_GPIO_DEBUG_IO_INVALID) {
186 return ESP_ERR_INVALID_ARG;
187 }
188 esp_rom_gpio_connect_out_signal(s_coex_evt_io_map[evt], sig, rev, false);
189 return ESP_OK;
190 }
191
esp_coexist_gpio_debug_matrix_config(int event)192 esp_err_t esp_coexist_gpio_debug_matrix_config(int event)
193 {
194 #if SOC_MODEM_CLOCK_IS_INDEPENDENT
195 modem_clock_module_enable(PERIPH_COEX_MODULE);
196 #endif
197 esp_err_t ret = coex_gpio_debug_matrix_config(event);
198 #if SOC_MODEM_CLOCK_IS_INDEPENDENT
199 modem_clock_module_disable(PERIPH_COEX_MODULE);
200 #endif
201 return ret;
202 }
203
esp_coexist_debug_init(void)204 esp_err_t esp_coexist_debug_init(void)
205 {
206 if (check_funcs_in_rom()) {
207 return ESP_ERR_INVALID_STATE;
208 }
209
210 s_wifi_evt_max = wifi_gpio_debug_max_event_get();
211 s_coex_evt_max = coex_gpio_debug_max_event_get();
212 uint8_t evt_max = s_wifi_evt_max + s_coex_evt_max;
213 if (evt_max == 0) {
214 return ESP_ERR_NOT_SUPPORTED;
215 }
216
217 /* Allocate binding map */
218 s_evt_io_map = malloc(sizeof(gpio_num_t) * evt_max);
219 if (!s_evt_io_map) {
220 return ESP_ERR_NO_MEM;
221 }
222 /* Init to invalid IO num */
223 for (uint8_t i = 0; i < evt_max; i++) {
224 s_evt_io_map[i] = COEX_GPIO_DEBUG_IO_INVALID;
225 }
226 s_wifi_evt_io_map = s_evt_io_map;
227 s_coex_evt_io_map = s_evt_io_map + s_wifi_evt_max;
228
229 /* binding map configuration */
230 diagram_bind_io_to_evt();
231
232 /* Register callback for Wi-Fi evt */
233 wifi_set_gpio_debug_cb(wifi_set_gpio_debug);
234
235 /* Register callback for coexist evt */
236 coex_set_gpio_debug_cb(coex_set_gpio_debug);
237
238 /* IO init and validity check */
239 gpio_config_t io_conf = {
240 //disable interrupt
241 .intr_type = GPIO_INTR_DISABLE,
242 //set as output mode
243 .mode = GPIO_MODE_OUTPUT,
244 //bit mask of the pins that you want to set,e.g.GPIO18/19
245 .pin_bit_mask = 0,
246 //disable pull-down mode
247 .pull_down_en = GPIO_PULLDOWN_DISABLE,
248 //enable pull-up mode
249 .pull_up_en = GPIO_PULLUP_ENABLE,
250 };
251
252 for (uint8_t i = 0; i < COEX_GPIO_DEBUG_IO_COUNT; i++) {
253 gpio_num_t io = s_io_nums[i];
254 io_conf.pin_bit_mask = (1ULL << io);
255 gpio_config(&io_conf);
256 gpio_set_level(io, 0);
257 }
258 esp_rom_delay_us(COEX_GPIO_DEBUG_SIG_CHECK_US);
259 for (uint8_t i = 0; i < COEX_GPIO_DEBUG_IO_COUNT; i++) {
260 gpio_set_level(s_io_nums[i], true);
261 }
262 esp_rom_delay_us(COEX_GPIO_DEBUG_SIG_CHECK_US);
263 for (uint8_t i = 0; i < COEX_GPIO_DEBUG_IO_COUNT; i++) {
264 gpio_set_level(s_io_nums[i], false);
265 }
266
267 #if SOC_MODEM_CLOCK_IS_INDEPENDENT
268 modem_clock_module_enable(PERIPH_COEX_MODULE);
269 #endif
270 /* Init coexist hardware signal */
271 ESP_ERROR_CHECK(coex_gpio_debug_matrix_init());
272 #if SOC_MODEM_CLOCK_IS_INDEPENDENT
273 modem_clock_module_disable(PERIPH_COEX_MODULE);
274 #endif
275
276 return ESP_OK;
277 }
278
279 #endif
280