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 "esp32c3/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
esp_coex_common_env_is_chip_wrapper(void)32 bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void)
33 {
34 #ifdef CONFIG_IDF_ENV_FPGA
35 return false;
36 #else
37 return true;
38 #endif
39 }
40
esp_coex_common_spin_lock_create_wrapper(void)41 void * esp_coex_common_spin_lock_create_wrapper(void)
42 {
43 portMUX_TYPE tmp = portMUX_INITIALIZER_UNLOCKED;
44 void *mux = malloc(sizeof(portMUX_TYPE));
45
46 if (mux) {
47 memcpy(mux,&tmp,sizeof(portMUX_TYPE));
48 return mux;
49 }
50 return NULL;
51 }
52
esp_coex_common_int_disable_wrapper(void * wifi_int_mux)53 uint32_t IRAM_ATTR esp_coex_common_int_disable_wrapper(void *wifi_int_mux)
54 {
55 if (xPortInIsrContext()) {
56 portENTER_CRITICAL_ISR(wifi_int_mux);
57 } else {
58 portENTER_CRITICAL(wifi_int_mux);
59 }
60
61 return 0;
62 }
63
esp_coex_common_int_restore_wrapper(void * wifi_int_mux,uint32_t tmp)64 void IRAM_ATTR esp_coex_common_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp)
65 {
66 if (xPortInIsrContext()) {
67 portEXIT_CRITICAL_ISR(wifi_int_mux);
68 } else {
69 portEXIT_CRITICAL(wifi_int_mux);
70 }
71 }
72
esp_coex_common_task_yield_from_isr_wrapper(void)73 void IRAM_ATTR esp_coex_common_task_yield_from_isr_wrapper(void)
74 {
75 portYIELD_FROM_ISR();
76 }
77
esp_coex_common_semphr_create_wrapper(uint32_t max,uint32_t init)78 void * esp_coex_common_semphr_create_wrapper(uint32_t max, uint32_t init)
79 {
80 return (void *)xSemaphoreCreateCounting(max, init);
81 }
82
esp_coex_common_semphr_delete_wrapper(void * semphr)83 void esp_coex_common_semphr_delete_wrapper(void *semphr)
84 {
85 vSemaphoreDelete(semphr);
86 }
87
esp_coex_common_semphr_take_wrapper(void * semphr,uint32_t block_time_tick)88 int32_t esp_coex_common_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
89 {
90 if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
91 return (int32_t)xSemaphoreTake(semphr, portMAX_DELAY);
92 } else {
93 return (int32_t)xSemaphoreTake(semphr, block_time_tick);
94 }
95 }
96
esp_coex_common_semphr_give_wrapper(void * semphr)97 int32_t esp_coex_common_semphr_give_wrapper(void *semphr)
98 {
99 return (int32_t)xSemaphoreGive(semphr);
100 }
101
esp_coex_common_timer_disarm_wrapper(void * timer)102 void IRAM_ATTR esp_coex_common_timer_disarm_wrapper(void *timer)
103 {
104 ets_timer_disarm(timer);
105 }
106
esp_coex_common_timer_done_wrapper(void * ptimer)107 void esp_coex_common_timer_done_wrapper(void *ptimer)
108 {
109 ets_timer_done(ptimer);
110 }
111
esp_coex_common_timer_setfn_wrapper(void * ptimer,void * pfunction,void * parg)112 void esp_coex_common_timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
113 {
114 ets_timer_setfn(ptimer, pfunction, parg);
115 }
116
esp_coex_common_timer_arm_us_wrapper(void * ptimer,uint32_t us,bool repeat)117 void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer, uint32_t us, bool repeat)
118 {
119 ets_timer_arm_us(ptimer, us, repeat);
120 }
121
esp_coex_common_malloc_internal_wrapper(size_t size)122 void * IRAM_ATTR esp_coex_common_malloc_internal_wrapper(size_t size)
123 {
124 return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
125 }
126
esp_coex_common_clk_slowclk_cal_get_wrapper(void)127 uint32_t esp_coex_common_clk_slowclk_cal_get_wrapper(void)
128 {
129 /* The bit width of WiFi light sleep clock calibration is 12 while the one of
130 * system is 19. It should shift 19 - 12 = 7.
131 */
132 if (GET_PERI_REG_MASK(SYSTEM_BT_LPCK_DIV_FRAC_REG, SYSTEM_LPCLK_SEL_XTAL)) {
133 uint64_t time_per_us = 1000000ULL;
134 return (((time_per_us << RTC_CLK_CAL_FRACT) / (MHZ)) >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
135 } else {
136 return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
137 }
138 }
139
140 /* static wrapper */
141
esp_coex_semphr_take_from_isr_wrapper(void * semphr,void * hptw)142 static int32_t IRAM_ATTR esp_coex_semphr_take_from_isr_wrapper(void *semphr, void *hptw)
143 {
144 return (int32_t)xSemaphoreTakeFromISR(semphr, hptw);
145 }
146
esp_coex_semphr_give_from_isr_wrapper(void * semphr,void * hptw)147 static int32_t IRAM_ATTR esp_coex_semphr_give_from_isr_wrapper(void *semphr, void *hptw)
148 {
149 return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
150 }
151
esp_coexist_debug_matrix_init_wrapper(int evt,int sig,bool rev)152 static int esp_coexist_debug_matrix_init_wrapper(int evt, int sig, bool rev)
153 {
154 #if CONFIG_ESP_COEX_GPIO_DEBUG
155 return esp_coexist_debug_matrix_init(evt, sig, rev);
156 #else
157 return ESP_ERR_NOT_SUPPORTED;
158 #endif
159 }
160
161 coex_adapter_funcs_t g_coex_adapter_funcs = {
162 ._version = COEX_ADAPTER_VERSION,
163 ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
164 ._semphr_create = esp_coex_common_semphr_create_wrapper,
165 ._semphr_delete = esp_coex_common_semphr_delete_wrapper,
166 ._semphr_take_from_isr = esp_coex_semphr_take_from_isr_wrapper,
167 ._semphr_give_from_isr = esp_coex_semphr_give_from_isr_wrapper,
168 ._semphr_take = esp_coex_common_semphr_take_wrapper,
169 ._semphr_give = esp_coex_common_semphr_give_wrapper,
170 ._is_in_isr = xPortInIsrContext,
171 ._malloc_internal = esp_coex_common_malloc_internal_wrapper,
172 ._free = free,
173 ._esp_timer_get_time = esp_timer_get_time,
174 ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
175 ._timer_done = esp_coex_common_timer_done_wrapper,
176 ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
177 ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
178 ._debug_matrix_init = esp_coexist_debug_matrix_init_wrapper,
179 ._magic = COEX_ADAPTER_MAGIC,
180 };
181