1 /*
2 * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <pthread.h>
13
14 #include "freertos/FreeRTOS.h"
15 #include "freertos/task.h"
16 #include "freertos/queue.h"
17 #include "freertos/semphr.h"
18 #include "freertos/portmacro.h"
19 #include "esp_heap_caps.h"
20 #include "esp_timer.h"
21 #include "soc/rtc.h"
22 #include "esp_private/esp_clk.h"
23 #include "private/esp_coexist_adapter.h"
24 #include "esp32s3/rom/ets_sys.h"
25 #include "soc/system_reg.h"
26 #include "private/esp_coexist_debug.h"
27
28 #define TAG "esp_coex_adapter"
29
30 #define OSI_FUNCS_TIME_BLOCKING 0xffffffff
31
32 typedef struct {
33 QueueHandle_t handle; /**< FreeRTOS queue handler */
34 void *storage; /**< storage for FreeRTOS queue */
35 } modem_static_queue_t;
36
esp_coex_common_env_is_chip_wrapper(void)37 bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void)
38 {
39 #ifdef CONFIG_IDF_ENV_FPGA
40 return false;
41 #else
42 return true;
43 #endif
44 }
45
esp_coex_common_spin_lock_create_wrapper(void)46 void * esp_coex_common_spin_lock_create_wrapper(void)
47 {
48 portMUX_TYPE tmp = portMUX_INITIALIZER_UNLOCKED;
49 void *mux = heap_caps_malloc(sizeof(portMUX_TYPE), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
50
51 if (mux) {
52 memcpy(mux,&tmp,sizeof(portMUX_TYPE));
53 return mux;
54 }
55 return NULL;
56 }
57
esp_coex_common_int_disable_wrapper(void * wifi_int_mux)58 uint32_t IRAM_ATTR esp_coex_common_int_disable_wrapper(void *wifi_int_mux)
59 {
60 if (xPortInIsrContext()) {
61 portENTER_CRITICAL_ISR(wifi_int_mux);
62 } else {
63 portENTER_CRITICAL(wifi_int_mux);
64 }
65
66 return 0;
67 }
68
esp_coex_common_int_restore_wrapper(void * wifi_int_mux,uint32_t tmp)69 void IRAM_ATTR esp_coex_common_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp)
70 {
71 if (xPortInIsrContext()) {
72 portEXIT_CRITICAL_ISR(wifi_int_mux);
73 } else {
74 portEXIT_CRITICAL(wifi_int_mux);
75 }
76 }
77
esp_coex_common_task_yield_from_isr_wrapper(void)78 void IRAM_ATTR esp_coex_common_task_yield_from_isr_wrapper(void)
79 {
80 portYIELD_FROM_ISR();
81 }
82
esp_coex_common_semphr_create_wrapper(uint32_t max,uint32_t init)83 void * esp_coex_common_semphr_create_wrapper(uint32_t max, uint32_t init)
84 {
85 return (void *)xSemaphoreCreateCounting(max, init);
86 }
87
esp_coex_common_semphr_delete_wrapper(void * semphr)88 void esp_coex_common_semphr_delete_wrapper(void *semphr)
89 {
90 vSemaphoreDelete(semphr);
91 }
92
esp_coex_common_semphr_take_wrapper(void * semphr,uint32_t block_time_tick)93 int32_t esp_coex_common_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
94 {
95 if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
96 return (int32_t)xSemaphoreTake(semphr, portMAX_DELAY);
97 } else {
98 return (int32_t)xSemaphoreTake(semphr, block_time_tick);
99 }
100 }
101
esp_coex_common_semphr_give_wrapper(void * semphr)102 int32_t esp_coex_common_semphr_give_wrapper(void *semphr)
103 {
104 return (int32_t)xSemaphoreGive(semphr);
105 }
106
esp_coex_common_timer_disarm_wrapper(void * timer)107 void IRAM_ATTR esp_coex_common_timer_disarm_wrapper(void *timer)
108 {
109 ets_timer_disarm(timer);
110 }
111
esp_coex_common_timer_done_wrapper(void * ptimer)112 void esp_coex_common_timer_done_wrapper(void *ptimer)
113 {
114 ets_timer_done(ptimer);
115 }
116
esp_coex_common_timer_setfn_wrapper(void * ptimer,void * pfunction,void * parg)117 void esp_coex_common_timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
118 {
119 ets_timer_setfn(ptimer, pfunction, parg);
120 }
121
esp_coex_common_timer_arm_us_wrapper(void * ptimer,uint32_t us,bool repeat)122 void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer, uint32_t us, bool repeat)
123 {
124 ets_timer_arm_us(ptimer, us, repeat);
125 }
126
esp_coex_common_malloc_internal_wrapper(size_t size)127 void * IRAM_ATTR esp_coex_common_malloc_internal_wrapper(size_t size)
128 {
129 return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
130 }
131
esp_coex_common_clk_slowclk_cal_get_wrapper(void)132 uint32_t esp_coex_common_clk_slowclk_cal_get_wrapper(void)
133 {
134 /* The bit width of WiFi light sleep clock calibration is 12 while the one of
135 * system is 19. It should shift 19 - 12 = 7.
136 */
137 if (GET_PERI_REG_MASK(SYSTEM_BT_LPCK_DIV_FRAC_REG, SYSTEM_LPCLK_SEL_XTAL)) {
138 uint64_t time_per_us = 1000000ULL;
139 return (((time_per_us << RTC_CLK_CAL_FRACT) / (MHZ)) >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
140 } else {
141 return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
142 }
143 }
144
145 /* static wrapper */
146
esp_coex_is_in_isr_wrapper(void)147 static int IRAM_ATTR esp_coex_is_in_isr_wrapper(void)
148 {
149 return !xPortCanYield();
150 }
151
esp_coex_internal_semphr_create_wrapper(uint32_t max,uint32_t init)152 static void *esp_coex_internal_semphr_create_wrapper(uint32_t max, uint32_t init)
153 {
154 modem_static_queue_t *semphr = heap_caps_calloc(1, sizeof(modem_static_queue_t), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
155 if (!semphr) {
156 return NULL;
157 }
158
159 #ifdef CONFIG_SPIRAM_USE_MALLOC
160 semphr->storage = heap_caps_calloc(1, sizeof(StaticSemaphore_t), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
161 if (!semphr->storage) {
162 goto _error;
163 }
164
165 semphr->handle = xSemaphoreCreateCountingStatic(max, init, semphr->storage);
166 if (!semphr->handle) {
167 goto _error;
168 }
169 return (void *)semphr;
170
171 _error:
172 if (semphr) {
173 if (semphr->storage) {
174 free(semphr->storage);
175 }
176
177 free(semphr);
178 }
179 return NULL;
180 #else
181 semphr->handle = xSemaphoreCreateCounting(max, init);
182 return (void *)semphr;
183 #endif
184 }
185
esp_coex_internal_semphr_delete_wrapper(void * semphr)186 static void esp_coex_internal_semphr_delete_wrapper(void *semphr)
187 {
188 modem_static_queue_t *semphr_item = (modem_static_queue_t *)semphr;
189 if (semphr_item) {
190 if (semphr_item->handle) {
191 vSemaphoreDelete(semphr_item->handle);
192 }
193 #ifdef CONFIG_SPIRAM_USE_MALLOC
194 if (semphr_item->storage) {
195 free(semphr_item->storage);
196 }
197 #endif
198 free(semphr_item);
199 }
200 }
201
esp_coex_internal_semphr_take_from_isr_wrapper(void * semphr,void * hptw)202 static int32_t IRAM_ATTR esp_coex_internal_semphr_take_from_isr_wrapper(void *semphr, void *hptw)
203 {
204 return (int32_t)xSemaphoreTakeFromISR(((modem_static_queue_t *)semphr)->handle, hptw);
205 }
206
esp_coex_internal_semphr_give_from_isr_wrapper(void * semphr,void * hptw)207 static int32_t IRAM_ATTR esp_coex_internal_semphr_give_from_isr_wrapper(void *semphr, void *hptw)
208 {
209 return (int32_t)xSemaphoreGiveFromISR(((modem_static_queue_t *)semphr)->handle, hptw);
210 }
211
esp_coex_internal_semphr_take_wrapper(void * semphr,uint32_t block_time_tick)212 static int32_t esp_coex_internal_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
213 {
214 if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
215 return (int32_t)xSemaphoreTake(((modem_static_queue_t *)semphr)->handle, portMAX_DELAY);
216 } else {
217 return (int32_t)xSemaphoreTake(((modem_static_queue_t *)semphr)->handle, block_time_tick);
218 }
219 }
220
esp_coex_internal_semphr_give_wrapper(void * semphr)221 static int32_t esp_coex_internal_semphr_give_wrapper(void *semphr)
222 {
223 return (int32_t)xSemaphoreGive(((modem_static_queue_t *)semphr)->handle);
224 }
225
esp_coexist_debug_matrix_init_wrapper(int evt,int sig,bool rev)226 static int esp_coexist_debug_matrix_init_wrapper(int evt, int sig, bool rev)
227 {
228 #if CONFIG_ESP_COEX_GPIO_DEBUG
229 return esp_coexist_debug_matrix_init(evt, sig, rev);
230 #else
231 return ESP_ERR_NOT_SUPPORTED;
232 #endif
233 }
234
235 coex_adapter_funcs_t g_coex_adapter_funcs = {
236 ._version = COEX_ADAPTER_VERSION,
237 ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
238 ._semphr_create = esp_coex_internal_semphr_create_wrapper,
239 ._semphr_delete = esp_coex_internal_semphr_delete_wrapper,
240 ._semphr_take_from_isr = esp_coex_internal_semphr_take_from_isr_wrapper,
241 ._semphr_give_from_isr = esp_coex_internal_semphr_give_from_isr_wrapper,
242 ._semphr_take = esp_coex_internal_semphr_take_wrapper,
243 ._semphr_give = esp_coex_internal_semphr_give_wrapper,
244 ._is_in_isr = esp_coex_is_in_isr_wrapper,
245 ._malloc_internal = esp_coex_common_malloc_internal_wrapper,
246 ._free = free,
247 ._esp_timer_get_time = esp_timer_get_time,
248 ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
249 ._timer_done = esp_coex_common_timer_done_wrapper,
250 ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
251 ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
252 ._debug_matrix_init = esp_coexist_debug_matrix_init_wrapper,
253 ._magic = COEX_ADAPTER_MAGIC,
254 };
255