1 /*
2  * SPDX-FileCopyrightText: 2015-2022 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 "esp_coexist_adapter.h"
24 #include "esp32c3/rom/ets_sys.h"
25 #include "soc/system_reg.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_malloc_internal_wrapper(size_t size)121 void * IRAM_ATTR esp_coex_common_malloc_internal_wrapper(size_t size)
122 {
123     return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
124 }
125 
esp_coex_common_clk_slowclk_cal_get_wrapper(void)126 uint32_t esp_coex_common_clk_slowclk_cal_get_wrapper(void)
127 {
128     /* The bit width of WiFi light sleep clock calibration is 12 while the one of
129      * system is 19. It should shift 19 - 12 = 7.
130     */
131     if (GET_PERI_REG_MASK(SYSTEM_BT_LPCK_DIV_FRAC_REG, SYSTEM_LPCLK_SEL_XTAL)) {
132         uint64_t time_per_us = 1000000ULL;
133         return (((time_per_us << RTC_CLK_CAL_FRACT) / (MHZ)) >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
134     } else {
135         return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
136     }
137 }
138 
139 /* static wrapper */
140 
esp_coex_semphr_take_from_isr_wrapper(void * semphr,void * hptw)141 static int32_t IRAM_ATTR esp_coex_semphr_take_from_isr_wrapper(void *semphr, void *hptw)
142 {
143     return (int32_t)xSemaphoreTakeFromISR(semphr, hptw);
144 }
145 
esp_coex_semphr_give_from_isr_wrapper(void * semphr,void * hptw)146 static int32_t IRAM_ATTR esp_coex_semphr_give_from_isr_wrapper(void *semphr, void *hptw)
147 {
148     return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
149 }
150 
151 coex_adapter_funcs_t g_coex_adapter_funcs = {
152     ._version = COEX_ADAPTER_VERSION,
153     ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
154     ._semphr_create = esp_coex_common_semphr_create_wrapper,
155     ._semphr_delete = esp_coex_common_semphr_delete_wrapper,
156     ._semphr_take_from_isr = esp_coex_semphr_take_from_isr_wrapper,
157     ._semphr_give_from_isr = esp_coex_semphr_give_from_isr_wrapper,
158     ._semphr_take = esp_coex_common_semphr_take_wrapper,
159     ._semphr_give = esp_coex_common_semphr_give_wrapper,
160     ._is_in_isr = xPortInIsrContext,
161     ._malloc_internal =  esp_coex_common_malloc_internal_wrapper,
162     ._free = free,
163     ._esp_timer_get_time = esp_timer_get_time,
164     ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
165     ._timer_done = esp_coex_common_timer_done_wrapper,
166     ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
167     ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
168     ._magic = COEX_ADAPTER_MAGIC,
169 };
170