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 "esp_coexist_adapter.h"
22 #include "esp32/rom/ets_sys.h"
23 
24 #define TAG "esp_coex_adapter"
25 
26 #define OSI_FUNCS_TIME_BLOCKING  0xffffffff
27 
28 typedef struct {
29     QueueHandle_t handle; /**< FreeRTOS queue handler */
30     void *storage;        /**< storage for FreeRTOS queue */
31 } modem_static_queue_t;
32 
esp_coex_common_env_is_chip_wrapper(void)33 bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void)
34 {
35 #ifdef CONFIG_IDF_ENV_FPGA
36     return false;
37 #else
38     return true;
39 #endif
40 }
41 
esp_coex_common_spin_lock_create_wrapper(void)42 void * esp_coex_common_spin_lock_create_wrapper(void)
43 {
44     portMUX_TYPE tmp = portMUX_INITIALIZER_UNLOCKED;
45     void *mux = heap_caps_malloc(sizeof(portMUX_TYPE), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
46 
47     if (mux) {
48         memcpy(mux,&tmp,sizeof(portMUX_TYPE));
49         return mux;
50     }
51     return NULL;
52 }
53 
esp_coex_common_int_disable_wrapper(void * wifi_int_mux)54 uint32_t IRAM_ATTR esp_coex_common_int_disable_wrapper(void *wifi_int_mux)
55 {
56     if (xPortInIsrContext()) {
57         portENTER_CRITICAL_ISR(wifi_int_mux);
58     } else {
59         portENTER_CRITICAL(wifi_int_mux);
60     }
61 
62     return 0;
63 }
64 
esp_coex_common_int_restore_wrapper(void * wifi_int_mux,uint32_t tmp)65 void IRAM_ATTR esp_coex_common_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp)
66 {
67     if (xPortInIsrContext()) {
68         portEXIT_CRITICAL_ISR(wifi_int_mux);
69     } else {
70         portEXIT_CRITICAL(wifi_int_mux);
71     }
72 }
73 
esp_coex_common_task_yield_from_isr_wrapper(void)74 void IRAM_ATTR esp_coex_common_task_yield_from_isr_wrapper(void)
75 {
76     portYIELD_FROM_ISR();
77 }
78 
esp_coex_common_semphr_create_wrapper(uint32_t max,uint32_t init)79 void * esp_coex_common_semphr_create_wrapper(uint32_t max, uint32_t init)
80 {
81     return (void *)xSemaphoreCreateCounting(max, init);
82 }
83 
esp_coex_common_semphr_delete_wrapper(void * semphr)84 void esp_coex_common_semphr_delete_wrapper(void *semphr)
85 {
86     vSemaphoreDelete(semphr);
87 }
88 
esp_coex_common_semphr_take_wrapper(void * semphr,uint32_t block_time_tick)89 int32_t esp_coex_common_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
90 {
91     if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
92         return (int32_t)xSemaphoreTake(semphr, portMAX_DELAY);
93     } else {
94         return (int32_t)xSemaphoreTake(semphr, block_time_tick);
95     }
96 }
97 
esp_coex_common_semphr_give_wrapper(void * semphr)98 int32_t esp_coex_common_semphr_give_wrapper(void *semphr)
99 {
100     return (int32_t)xSemaphoreGive(semphr);
101 }
102 
esp_coex_common_timer_disarm_wrapper(void * timer)103 void IRAM_ATTR esp_coex_common_timer_disarm_wrapper(void *timer)
104 {
105     ets_timer_disarm(timer);
106 }
107 
esp_coex_common_timer_done_wrapper(void * ptimer)108 void esp_coex_common_timer_done_wrapper(void *ptimer)
109 {
110     ets_timer_done(ptimer);
111 }
112 
esp_coex_common_timer_setfn_wrapper(void * ptimer,void * pfunction,void * parg)113 void esp_coex_common_timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
114 {
115     ets_timer_setfn(ptimer, pfunction, parg);
116 }
117 
esp_coex_common_timer_arm_us_wrapper(void * ptimer,uint32_t us,bool repeat)118 void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer, uint32_t us, bool repeat)
119 {
120     ets_timer_arm_us(ptimer, us, repeat);
121 }
122 
esp_coex_common_malloc_internal_wrapper(size_t size)123 void * IRAM_ATTR esp_coex_common_malloc_internal_wrapper(size_t size)
124 {
125     return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
126 }
127 
128 /* static wrapper */
129 
esp_coex_is_in_isr_wrapper(void)130 static int IRAM_ATTR esp_coex_is_in_isr_wrapper(void)
131 {
132     return !xPortCanYield();
133 }
134 
esp_coex_internal_semphr_create_wrapper(uint32_t max,uint32_t init)135 static void *esp_coex_internal_semphr_create_wrapper(uint32_t max, uint32_t init)
136 {
137     modem_static_queue_t *semphr = heap_caps_calloc(1, sizeof(modem_static_queue_t), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
138     if (!semphr) {
139         return NULL;
140     }
141 
142 #ifdef CONFIG_SPIRAM_USE_MALLOC
143     semphr->storage = heap_caps_calloc(1, sizeof(StaticSemaphore_t), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
144     if (!semphr->storage) {
145         goto _error;
146     }
147 
148     semphr->handle = xSemaphoreCreateCountingStatic(max, init, semphr->storage);
149     if (!semphr->handle) {
150         goto _error;
151     }
152     return (void *)semphr;
153 
154 _error:
155     if (semphr) {
156         if (semphr->storage) {
157             free(semphr->storage);
158         }
159 
160         free(semphr);
161     }
162     return NULL;
163 #else
164     semphr->handle = xSemaphoreCreateCounting(max, init);
165     return (void *)semphr;
166 #endif
167 }
168 
esp_coex_internal_semphr_delete_wrapper(void * semphr)169 static void esp_coex_internal_semphr_delete_wrapper(void *semphr)
170 {
171     modem_static_queue_t *semphr_item = (modem_static_queue_t *)semphr;
172     if (semphr_item) {
173         if (semphr_item->handle) {
174             vSemaphoreDelete(semphr_item->handle);
175         }
176 #ifdef CONFIG_SPIRAM_USE_MALLOC
177         if (semphr_item->storage) {
178             free(semphr_item->storage);
179         }
180 #endif
181         free(semphr_item);
182     }
183 }
184 
esp_coex_internal_semphr_take_from_isr_wrapper(void * semphr,void * hptw)185 static int32_t IRAM_ATTR esp_coex_internal_semphr_take_from_isr_wrapper(void *semphr, void *hptw)
186 {
187     return (int32_t)xSemaphoreTakeFromISR(((modem_static_queue_t *)semphr)->handle, hptw);
188 }
189 
esp_coex_internal_semphr_give_from_isr_wrapper(void * semphr,void * hptw)190 static int32_t IRAM_ATTR esp_coex_internal_semphr_give_from_isr_wrapper(void *semphr, void *hptw)
191 {
192     return (int32_t)xSemaphoreGiveFromISR(((modem_static_queue_t *)semphr)->handle, hptw);
193 }
194 
esp_coex_internal_semphr_take_wrapper(void * semphr,uint32_t block_time_tick)195 static int32_t esp_coex_internal_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
196 {
197     if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
198         return (int32_t)xSemaphoreTake(((modem_static_queue_t *)semphr)->handle, portMAX_DELAY);
199     } else {
200         return (int32_t)xSemaphoreTake(((modem_static_queue_t *)semphr)->handle, block_time_tick);
201     }
202 }
203 
esp_coex_internal_semphr_give_wrapper(void * semphr)204 static int32_t esp_coex_internal_semphr_give_wrapper(void *semphr)
205 {
206     return (int32_t)xSemaphoreGive(((modem_static_queue_t *)semphr)->handle);
207 }
208 
209 coex_adapter_funcs_t g_coex_adapter_funcs = {
210     ._version = COEX_ADAPTER_VERSION,
211     ._spin_lock_create = esp_coex_common_spin_lock_create_wrapper,
212     ._spin_lock_delete = free,
213     ._int_disable = esp_coex_common_int_disable_wrapper,
214     ._int_enable = esp_coex_common_int_restore_wrapper,
215     ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
216     ._semphr_create = esp_coex_internal_semphr_create_wrapper,
217     ._semphr_delete = esp_coex_internal_semphr_delete_wrapper,
218     ._semphr_take_from_isr = esp_coex_internal_semphr_take_from_isr_wrapper,
219     ._semphr_give_from_isr = esp_coex_internal_semphr_give_from_isr_wrapper,
220     ._semphr_take = esp_coex_internal_semphr_take_wrapper,
221     ._semphr_give = esp_coex_internal_semphr_give_wrapper,
222     ._is_in_isr = esp_coex_is_in_isr_wrapper,
223     ._malloc_internal =  esp_coex_common_malloc_internal_wrapper,
224     ._free = free,
225     ._esp_timer_get_time = esp_timer_get_time,
226     ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
227     ._timer_done = esp_coex_common_timer_done_wrapper,
228     ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
229     ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
230     ._magic = COEX_ADAPTER_MAGIC,
231 };
232