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