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/event_groups.h"
19 #include "freertos/portmacro.h"
20 #include "freertos/xtensa_api.h"
21 #include "freertos/idf_additions.h"
22 #include "esp_types.h"
23 #include "esp_random.h"
24 #include "esp_mac.h"
25 #include "esp_task.h"
26 #include "esp_intr_alloc.h"
27 #include "esp_attr.h"
28 #include "esp_log.h"
29 #include "esp_event.h"
30 #include "esp_heap_caps.h"
31 #include "esp_timer.h"
32 #include "esp_cpu.h"
33 #include "esp_private/wifi_os_adapter.h"
34 #include "esp_private/wifi.h"
35 #include "esp_phy_init.h"
36 #include "soc/dport_reg.h"
37 #include "soc/syscon_reg.h"
38 #include "phy_init_data.h"
39 #include "esp_private/periph_ctrl.h"
40 #include "nvs.h"
41 #include "os.h"
42 #include "esp_smartconfig.h"
43 #include "private/esp_coexist_internal.h"
44 #include "dport_access.h"
45 #include "esp_rom_sys.h"
46 #include "esp32/rom/ets_sys.h"
47 #include "private/esp_modem_wrapper.h"
48 #if __has_include("esp_psram.h")
49 #include "esp_psram.h"
50 #endif
51 
52 #define TAG "esp_adapter"
53 
54 #ifdef CONFIG_PM_ENABLE
55 extern void wifi_apb80m_request(void);
56 extern void wifi_apb80m_release(void);
57 #endif
58 
s_esp_dport_access_stall_other_cpu_start(void)59 static void IRAM_ATTR s_esp_dport_access_stall_other_cpu_start(void)
60 {
61     DPORT_STALL_OTHER_CPU_START();
62 }
63 
s_esp_dport_access_stall_other_cpu_end(void)64 static void IRAM_ATTR s_esp_dport_access_stall_other_cpu_end(void)
65 {
66     DPORT_STALL_OTHER_CPU_END();
67 }
68 
69 /*
70  If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly.
71  If failed, try to allocate it in internal memory then.
72  */
wifi_malloc(size_t size)73 IRAM_ATTR void *wifi_malloc( size_t size )
74 {
75 #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
76     return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
77 #else
78     return malloc(size);
79 #endif
80 }
81 
82 /*
83  If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly.
84  If failed, try to allocate it in internal memory then.
85  */
wifi_realloc(void * ptr,size_t size)86 IRAM_ATTR void *wifi_realloc( void *ptr, size_t size )
87 {
88 #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
89     return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
90 #else
91     return realloc(ptr, size);
92 #endif
93 }
94 
95 /*
96  If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly.
97  If failed, try to allocate it in internal memory then.
98  */
wifi_calloc(size_t n,size_t size)99 IRAM_ATTR void *wifi_calloc( size_t n, size_t size )
100 {
101 #if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
102     return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
103 #else
104     return calloc(n, size);
105 #endif
106 }
107 
wifi_zalloc_wrapper(size_t size)108 static void * IRAM_ATTR wifi_zalloc_wrapper(size_t size)
109 {
110     void *ptr = wifi_calloc(1, size);
111     return ptr;
112 }
113 
wifi_create_queue(int queue_len,int item_size)114 wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size)
115 {
116     wifi_static_queue_t *queue = NULL;
117 
118     queue = (wifi_static_queue_t*)heap_caps_malloc(sizeof(wifi_static_queue_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
119     if (!queue) {
120         return NULL;
121     }
122 
123 #if CONFIG_SPIRAM_USE_MALLOC
124     /* Wi-Fi still use internal RAM */
125 
126     queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
127     if (!queue->storage) {
128         goto _error;
129     }
130 
131     queue->handle = xQueueCreateStatic( queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage));
132 
133     if (!queue->handle) {
134         goto _error;
135     }
136 
137     return queue;
138 
139 _error:
140     if (queue) {
141         if (queue->storage) {
142             free(queue->storage);
143         }
144 
145         free(queue);
146     }
147 
148     return NULL;
149 #else
150     queue->handle = xQueueCreate( queue_len, item_size);
151     return queue;
152 #endif
153 }
154 
wifi_delete_queue(wifi_static_queue_t * queue)155 void wifi_delete_queue(wifi_static_queue_t *queue)
156 {
157     if (queue) {
158         vQueueDelete(queue->handle);
159 
160 #if CONFIG_SPIRAM_USE_MALLOC
161         if (queue->storage) {
162             free(queue->storage);
163         }
164 #endif
165 
166         free(queue);
167     }
168 }
169 
wifi_create_queue_wrapper(int queue_len,int item_size)170 static void * wifi_create_queue_wrapper(int queue_len, int item_size)
171 {
172     return wifi_create_queue(queue_len, item_size);
173 }
174 
wifi_delete_queue_wrapper(void * queue)175 static void wifi_delete_queue_wrapper(void *queue)
176 {
177     wifi_delete_queue(queue);
178 }
179 
set_intr_wrapper(int32_t cpu_no,uint32_t intr_source,uint32_t intr_num,int32_t intr_prio)180 static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source, uint32_t intr_num, int32_t intr_prio)
181 {
182     esp_rom_route_intr_matrix(cpu_no, intr_source, intr_num);
183 }
184 
clear_intr_wrapper(uint32_t intr_source,uint32_t intr_num)185 static void clear_intr_wrapper(uint32_t intr_source, uint32_t intr_num)
186 {
187 
188 }
189 
set_isr_wrapper(int32_t n,void * f,void * arg)190 static void set_isr_wrapper(int32_t n, void *f, void *arg)
191 {
192     xt_set_interrupt_handler(n, (xt_handler)f, arg);
193 }
194 
is_from_isr_wrapper(void)195 static bool IRAM_ATTR is_from_isr_wrapper(void)
196 {
197     return !xPortCanYield();
198 }
199 
wifi_thread_semphr_free(void * data)200 static void wifi_thread_semphr_free(void* data)
201 {
202     SemaphoreHandle_t *sem = (SemaphoreHandle_t*)(data);
203 
204     if (sem) {
205         vSemaphoreDelete(sem);
206     }
207 }
208 
wifi_thread_semphr_get_wrapper(void)209 static void * wifi_thread_semphr_get_wrapper(void)
210 {
211     static bool s_wifi_thread_sem_key_init = false;
212     static pthread_key_t s_wifi_thread_sem_key;
213     SemaphoreHandle_t sem = NULL;
214 
215     if (s_wifi_thread_sem_key_init == false) {
216         if (0 != pthread_key_create(&s_wifi_thread_sem_key, wifi_thread_semphr_free)) {
217             return NULL;
218         }
219         s_wifi_thread_sem_key_init = true;
220     }
221 
222     sem = pthread_getspecific(s_wifi_thread_sem_key);
223     if (!sem) {
224         sem = xSemaphoreCreateCounting(1, 0);
225         if (sem) {
226             pthread_setspecific(s_wifi_thread_sem_key, sem);
227             ESP_LOGV(TAG, "thread sem create: sem=%p", sem);
228         }
229     }
230 
231     ESP_LOGV(TAG, "thread sem get: sem=%p", sem);
232     return (void*)sem;
233 }
234 
recursive_mutex_create_wrapper(void)235 static void * recursive_mutex_create_wrapper(void)
236 {
237     return (void *)xSemaphoreCreateRecursiveMutex();
238 }
239 
mutex_create_wrapper(void)240 static void * mutex_create_wrapper(void)
241 {
242     return (void *)xSemaphoreCreateMutex();
243 }
244 
mutex_delete_wrapper(void * mutex)245 static void mutex_delete_wrapper(void *mutex)
246 {
247     vSemaphoreDelete(mutex);
248 }
249 
mutex_lock_wrapper(void * mutex)250 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
251 {
252     return (int32_t)xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
253 }
254 
mutex_unlock_wrapper(void * mutex)255 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
256 {
257     return (int32_t)xSemaphoreGiveRecursive(mutex);
258 }
259 
queue_create_wrapper(uint32_t queue_len,uint32_t item_size)260 static void * queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
261 {
262     StaticQueue_t *queue_buffer = heap_caps_malloc_prefer(sizeof(StaticQueue_t) + (queue_len * item_size), 2,
263                                                           MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM,
264                                                           MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL);
265     if (!queue_buffer) {
266         return NULL;
267     }
268     QueueHandle_t queue_handle = xQueueCreateStatic(queue_len, item_size, (uint8_t *)queue_buffer + sizeof(StaticQueue_t),
269                                                     queue_buffer);
270     if (!queue_handle) {
271         free(queue_buffer);
272         return NULL;
273     }
274 
275     return (void *)queue_handle;
276 }
277 
queue_delete_wrapper(void * queue)278 static void queue_delete_wrapper(void *queue)
279 {
280     if (queue) {
281         StaticQueue_t *queue_buffer = NULL;
282         xQueueGetStaticBuffers(queue, NULL, &queue_buffer);
283         vQueueDelete(queue);
284         if (queue_buffer) {
285             free(queue_buffer);
286         }
287     }
288 }
289 
queue_send_wrapper(void * queue,void * item,uint32_t block_time_tick)290 static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_tick)
291 {
292     if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
293         return (int32_t)xQueueSend(queue, item, portMAX_DELAY);
294     } else {
295         return (int32_t)xQueueSend(queue, item, block_time_tick);
296     }
297 }
298 
queue_send_from_isr_wrapper(void * queue,void * item,void * hptw)299 static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
300 {
301     return (int32_t)xQueueSendFromISR(queue, item, hptw);
302 }
303 
queue_send_to_back_wrapper(void * queue,void * item,uint32_t block_time_tick)304 static int32_t queue_send_to_back_wrapper(void *queue, void *item, uint32_t block_time_tick)
305 {
306     return (int32_t)xQueueGenericSend(queue, item, block_time_tick, queueSEND_TO_BACK);
307 }
308 
queue_send_to_front_wrapper(void * queue,void * item,uint32_t block_time_tick)309 static int32_t queue_send_to_front_wrapper(void *queue, void *item, uint32_t block_time_tick)
310 {
311     return (int32_t)xQueueGenericSend(queue, item, block_time_tick, queueSEND_TO_FRONT);
312 }
313 
queue_recv_wrapper(void * queue,void * item,uint32_t block_time_tick)314 static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_tick)
315 {
316     if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
317         return (int32_t)xQueueReceive(queue, item, portMAX_DELAY);
318     } else {
319         return (int32_t)xQueueReceive(queue, item, block_time_tick);
320     }
321 }
322 
event_group_wait_bits_wrapper(void * event,uint32_t bits_to_wait_for,int clear_on_exit,int wait_for_all_bits,uint32_t block_time_tick)323 static uint32_t event_group_wait_bits_wrapper(void *event, uint32_t bits_to_wait_for, int clear_on_exit, int wait_for_all_bits, uint32_t block_time_tick)
324 {
325     if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
326         return (uint32_t)xEventGroupWaitBits(event, bits_to_wait_for, clear_on_exit, wait_for_all_bits, portMAX_DELAY);
327     } else {
328         return (uint32_t)xEventGroupWaitBits(event, bits_to_wait_for, clear_on_exit, wait_for_all_bits, block_time_tick);
329     }
330 }
331 
task_create_pinned_to_core_wrapper(void * task_func,const char * name,uint32_t stack_depth,void * param,uint32_t prio,void * task_handle,uint32_t core_id)332 static int32_t task_create_pinned_to_core_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id)
333 {
334     return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY));
335 }
336 
task_create_wrapper(void * task_func,const char * name,uint32_t stack_depth,void * param,uint32_t prio,void * task_handle)337 static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle)
338 {
339     return (uint32_t)xTaskCreate(task_func, name, stack_depth, param, prio, task_handle);
340 }
341 
task_ms_to_tick_wrapper(uint32_t ms)342 static int32_t IRAM_ATTR task_ms_to_tick_wrapper(uint32_t ms)
343 {
344     return (int32_t)(ms / portTICK_PERIOD_MS);
345 }
346 
task_get_max_priority_wrapper(void)347 static int32_t task_get_max_priority_wrapper(void)
348 {
349     return (int32_t)(configMAX_PRIORITIES);
350 }
351 
esp_event_post_wrapper(const char * event_base,int32_t event_id,void * event_data,size_t event_data_size,uint32_t ticks_to_wait)352 static int32_t esp_event_post_wrapper(const char* event_base, int32_t event_id, void* event_data, size_t event_data_size, uint32_t ticks_to_wait)
353 {
354     if (ticks_to_wait == OSI_FUNCS_TIME_BLOCKING) {
355         return (int32_t)esp_event_post(event_base, event_id, event_data, event_data_size, portMAX_DELAY);
356     } else {
357         return (int32_t)esp_event_post(event_base, event_id, event_data, event_data_size, ticks_to_wait);
358     }
359 }
360 
wifi_apb80m_request_wrapper(void)361 static void IRAM_ATTR wifi_apb80m_request_wrapper(void)
362 {
363 #ifdef CONFIG_PM_ENABLE
364     wifi_apb80m_request();
365 #endif
366 }
367 
wifi_apb80m_release_wrapper(void)368 static void IRAM_ATTR wifi_apb80m_release_wrapper(void)
369 {
370 #ifdef CONFIG_PM_ENABLE
371     wifi_apb80m_release();
372 #endif
373 }
374 
timer_arm_wrapper(void * timer,uint32_t tmout,bool repeat)375 static void IRAM_ATTR timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat)
376 {
377     ets_timer_arm(timer, tmout, repeat);
378 }
379 
wifi_reset_mac_wrapper(void)380 static void wifi_reset_mac_wrapper(void)
381 {
382     periph_module_reset(PERIPH_WIFI_MODULE);
383 }
384 
wifi_clock_enable_wrapper(void)385 static void wifi_clock_enable_wrapper(void)
386 {
387     wifi_module_enable();
388 }
389 
wifi_clock_disable_wrapper(void)390 static void wifi_clock_disable_wrapper(void)
391 {
392     wifi_module_disable();
393 }
394 
get_time_wrapper(void * t)395 static int get_time_wrapper(void *t)
396 {
397     return os_get_time(t);
398 }
399 
realloc_internal_wrapper(void * ptr,size_t size)400 static void * IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size)
401 {
402     return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
403 }
404 
calloc_internal_wrapper(size_t n,size_t size)405 static void * IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size)
406 {
407     return heap_caps_calloc(n, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
408 }
409 
zalloc_internal_wrapper(size_t size)410 static void * IRAM_ATTR zalloc_internal_wrapper(size_t size)
411 {
412     void *ptr = heap_caps_calloc(1, size, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
413     return ptr;
414 }
415 
coex_init_wrapper(void)416 static int coex_init_wrapper(void)
417 {
418 #if CONFIG_SW_COEXIST_ENABLE
419     return coex_init();
420 #else
421     return 0;
422 #endif
423 }
424 
coex_deinit_wrapper(void)425 static void coex_deinit_wrapper(void)
426 {
427 #if CONFIG_SW_COEXIST_ENABLE
428     coex_deinit();
429 #endif
430 }
431 
coex_enable_wrapper(void)432 static int coex_enable_wrapper(void)
433 {
434 #if CONFIG_SW_COEXIST_ENABLE
435     return coex_enable();
436 #else
437     return 0;
438 #endif
439 }
440 
coex_disable_wrapper(void)441 static void coex_disable_wrapper(void)
442 {
443 #if CONFIG_SW_COEXIST_ENABLE
444     coex_disable();
445 #endif
446 }
447 
coex_status_get_wrapper(void)448 static IRAM_ATTR uint32_t coex_status_get_wrapper(void)
449 {
450 #if CONFIG_SW_COEXIST_ENABLE
451     return coex_status_get(COEX_STATUS_GET_WIFI_BITMAP);
452 #else
453     return 0;
454 #endif
455 }
456 
coex_wifi_request_wrapper(uint32_t event,uint32_t latency,uint32_t duration)457 static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration)
458 {
459 #if CONFIG_SW_COEXIST_ENABLE
460     return coex_wifi_request(event, latency, duration);
461 #else
462     return 0;
463 #endif
464 }
465 
coex_wifi_release_wrapper(uint32_t event)466 static IRAM_ATTR int coex_wifi_release_wrapper(uint32_t event)
467 {
468 #if CONFIG_SW_COEXIST_ENABLE
469     return coex_wifi_release(event);
470 #else
471     return 0;
472 #endif
473 }
474 
coex_wifi_channel_set_wrapper(uint8_t primary,uint8_t secondary)475 static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary)
476 {
477 #if CONFIG_SW_COEXIST_ENABLE
478     return coex_wifi_channel_set(primary, secondary);
479 #else
480     return 0;
481 #endif
482 }
483 
coex_event_duration_get_wrapper(uint32_t event,uint32_t * duration)484 static IRAM_ATTR int coex_event_duration_get_wrapper(uint32_t event, uint32_t *duration)
485 {
486 #if CONFIG_SW_COEXIST_ENABLE
487     return coex_event_duration_get(event, duration);
488 #else
489     return 0;
490 #endif
491 }
492 
coex_pti_get_wrapper(uint32_t event,uint8_t * pti)493 static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti)
494 {
495     return 0;
496 }
497 
coex_schm_status_bit_clear_wrapper(uint32_t type,uint32_t status)498 static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
499 {
500 #if CONFIG_SW_COEXIST_ENABLE
501     coex_schm_status_bit_clear(type, status);
502 #endif
503 }
504 
coex_schm_status_bit_set_wrapper(uint32_t type,uint32_t status)505 static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
506 {
507 #if CONFIG_SW_COEXIST_ENABLE
508     coex_schm_status_bit_set(type, status);
509 #endif
510 }
511 
coex_schm_interval_set_wrapper(uint32_t interval)512 static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval)
513 {
514 #if CONFIG_SW_COEXIST_ENABLE
515     return coex_schm_interval_set(interval);
516 #else
517     return 0;
518 #endif
519 }
520 
coex_schm_interval_get_wrapper(void)521 static uint32_t coex_schm_interval_get_wrapper(void)
522 {
523 #if CONFIG_SW_COEXIST_ENABLE
524     return coex_schm_interval_get();
525 #else
526     return 0;
527 #endif
528 }
529 
coex_schm_curr_period_get_wrapper(void)530 static uint8_t coex_schm_curr_period_get_wrapper(void)
531 {
532 #if CONFIG_SW_COEXIST_ENABLE
533     return coex_schm_curr_period_get();
534 #else
535     return 0;
536 #endif
537 }
538 
coex_schm_curr_phase_get_wrapper(void)539 static void * coex_schm_curr_phase_get_wrapper(void)
540 {
541 #if CONFIG_SW_COEXIST_ENABLE
542     return coex_schm_curr_phase_get();
543 #else
544     return NULL;
545 #endif
546 }
547 
coex_register_start_cb_wrapper(int (* cb)(void))548 static int coex_register_start_cb_wrapper(int (* cb)(void))
549 {
550 #if CONFIG_SW_COEXIST_ENABLE
551     return coex_register_start_cb(cb);
552 #else
553     return 0;
554 #endif
555 }
556 
coex_schm_process_restart_wrapper(void)557 static int coex_schm_process_restart_wrapper(void)
558 {
559 #if CONFIG_SW_COEXIST_ENABLE
560     return coex_schm_process_restart();
561 #else
562     return 0;
563 #endif
564 }
565 
coex_schm_register_cb_wrapper(int type,int (* cb)(int))566 static int coex_schm_register_cb_wrapper(int type, int(*cb)(int))
567 {
568 #if CONFIG_SW_COEXIST_ENABLE
569     return coex_schm_register_callback(type, cb);
570 #else
571     return 0;
572 #endif
573 }
574 
coex_schm_flexible_period_set_wrapper(uint8_t period)575 static int coex_schm_flexible_period_set_wrapper(uint8_t period)
576 {
577 #if CONFIG_ESP_COEX_POWER_MANAGEMENT
578     return coex_schm_flexible_period_set(period);
579 #else
580     return 0;
581 #endif
582 }
583 
coex_schm_flexible_period_get_wrapper(void)584 static uint8_t coex_schm_flexible_period_get_wrapper(void)
585 {
586 #if CONFIG_ESP_COEX_POWER_MANAGEMENT
587     return coex_schm_flexible_period_get();
588 #else
589     return 1;
590 #endif
591 }
592 
esp_empty_wrapper(void)593 static void IRAM_ATTR esp_empty_wrapper(void)
594 {
595 
596 }
597 
esp_phy_enable_wrapper(void)598 static void esp_phy_enable_wrapper(void)
599 {
600     esp_phy_enable(PHY_MODEM_WIFI);
601     phy_wifi_enable_set(1);
602 }
603 
esp_phy_disable_wrapper(void)604 static void esp_phy_disable_wrapper(void)
605 {
606     phy_wifi_enable_set(0);
607     esp_phy_disable(PHY_MODEM_WIFI);
608 }
609 
610 wifi_osi_funcs_t g_wifi_osi_funcs = {
611     ._version = ESP_WIFI_OS_ADAPTER_VERSION,
612     ._env_is_chip = esp_coex_common_env_is_chip_wrapper,
613     ._set_intr = set_intr_wrapper,
614     ._clear_intr = clear_intr_wrapper,
615     ._set_isr = set_isr_wrapper,
616     ._ints_on = esp_cpu_intr_enable,
617     ._ints_off = esp_cpu_intr_disable,
618     ._is_from_isr = is_from_isr_wrapper,
619     ._spin_lock_create = esp_coex_common_spin_lock_create_wrapper,
620     ._spin_lock_delete = free,
621     ._wifi_int_disable = esp_coex_common_int_disable_wrapper,
622     ._wifi_int_restore = esp_coex_common_int_restore_wrapper,
623     ._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
624     ._semphr_create = esp_coex_common_semphr_create_wrapper,
625     ._semphr_delete = esp_coex_common_semphr_delete_wrapper,
626     ._semphr_take = esp_coex_common_semphr_take_wrapper,
627     ._semphr_give = esp_coex_common_semphr_give_wrapper,
628     ._wifi_thread_semphr_get = wifi_thread_semphr_get_wrapper,
629     ._mutex_create = mutex_create_wrapper,
630     ._recursive_mutex_create = recursive_mutex_create_wrapper,
631     ._mutex_delete = mutex_delete_wrapper,
632     ._mutex_lock = mutex_lock_wrapper,
633     ._mutex_unlock = mutex_unlock_wrapper,
634     ._queue_create = queue_create_wrapper,
635     ._queue_delete = queue_delete_wrapper,
636     ._queue_send = queue_send_wrapper,
637     ._queue_send_from_isr = queue_send_from_isr_wrapper,
638     ._queue_send_to_back = queue_send_to_back_wrapper,
639     ._queue_send_to_front = queue_send_to_front_wrapper,
640     ._queue_recv = queue_recv_wrapper,
641     ._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting,
642     ._event_group_create = (void *(*)(void))xEventGroupCreate,
643     ._event_group_delete = (void(*)(void *))vEventGroupDelete,
644     ._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits,
645     ._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits,
646     ._event_group_wait_bits = event_group_wait_bits_wrapper,
647     ._task_create_pinned_to_core = task_create_pinned_to_core_wrapper,
648     ._task_create = task_create_wrapper,
649     ._task_delete = (void(*)(void *))vTaskDelete,
650     ._task_delay = vTaskDelay,
651     ._task_ms_to_tick = task_ms_to_tick_wrapper,
652     ._task_get_current_task = (void *(*)(void))xTaskGetCurrentTaskHandle,
653     ._task_get_max_priority = task_get_max_priority_wrapper,
654     ._malloc = malloc,
655     ._free = free,
656     ._event_post = esp_event_post_wrapper,
657     ._get_free_heap_size = esp_get_free_internal_heap_size,
658     ._rand = esp_random,
659     ._dport_access_stall_other_cpu_start_wrap = s_esp_dport_access_stall_other_cpu_start,
660     ._dport_access_stall_other_cpu_end_wrap = s_esp_dport_access_stall_other_cpu_end,
661     ._wifi_apb80m_request = wifi_apb80m_request_wrapper,
662     ._wifi_apb80m_release = wifi_apb80m_release_wrapper,
663     ._phy_disable = esp_phy_disable_wrapper,
664     ._phy_enable = esp_phy_enable_wrapper,
665     ._phy_common_clock_enable = esp_phy_common_clock_enable,
666     ._phy_common_clock_disable = esp_phy_common_clock_disable,
667     ._phy_update_country_info = esp_phy_update_country_info,
668     ._read_mac = esp_read_mac,
669     ._timer_arm = timer_arm_wrapper,
670     ._timer_disarm = esp_coex_common_timer_disarm_wrapper,
671     ._timer_done = esp_coex_common_timer_done_wrapper,
672     ._timer_setfn = esp_coex_common_timer_setfn_wrapper,
673     ._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
674     ._wifi_reset_mac = wifi_reset_mac_wrapper,
675     ._wifi_clock_enable = wifi_clock_enable_wrapper,
676     ._wifi_clock_disable = wifi_clock_disable_wrapper,
677     ._wifi_rtc_enable_iso = esp_empty_wrapper,
678     ._wifi_rtc_disable_iso = esp_empty_wrapper,
679     ._esp_timer_get_time = esp_timer_get_time,
680     ._nvs_set_i8 = nvs_set_i8,
681     ._nvs_get_i8 = nvs_get_i8,
682     ._nvs_set_u8 = nvs_set_u8,
683     ._nvs_get_u8 = nvs_get_u8,
684     ._nvs_set_u16 = nvs_set_u16,
685     ._nvs_get_u16 = nvs_get_u16,
686     ._nvs_open = nvs_open,
687     ._nvs_close = nvs_close,
688     ._nvs_commit = nvs_commit,
689     ._nvs_set_blob = nvs_set_blob,
690     ._nvs_get_blob = nvs_get_blob,
691     ._nvs_erase_key = nvs_erase_key,
692     ._get_random = os_get_random,
693     ._get_time = get_time_wrapper,
694     ._random = os_random,
695     ._log_write = esp_log_write,
696     ._log_writev = esp_log_writev,
697     ._log_timestamp = esp_log_timestamp,
698     ._malloc_internal =  esp_coex_common_malloc_internal_wrapper,
699     ._realloc_internal = realloc_internal_wrapper,
700     ._calloc_internal = calloc_internal_wrapper,
701     ._zalloc_internal = zalloc_internal_wrapper,
702     ._wifi_malloc = wifi_malloc,
703     ._wifi_realloc = wifi_realloc,
704     ._wifi_calloc = wifi_calloc,
705     ._wifi_zalloc = wifi_zalloc_wrapper,
706     ._wifi_create_queue = wifi_create_queue_wrapper,
707     ._wifi_delete_queue = wifi_delete_queue_wrapper,
708     ._coex_init = coex_init_wrapper,
709     ._coex_deinit = coex_deinit_wrapper,
710     ._coex_enable = coex_enable_wrapper,
711     ._coex_disable = coex_disable_wrapper,
712     ._coex_status_get = coex_status_get_wrapper,
713     ._coex_wifi_request = coex_wifi_request_wrapper,
714     ._coex_wifi_release = coex_wifi_release_wrapper,
715     ._coex_wifi_channel_set = coex_wifi_channel_set_wrapper,
716     ._coex_event_duration_get = coex_event_duration_get_wrapper,
717     ._coex_pti_get = coex_pti_get_wrapper,
718     ._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
719     ._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
720     ._coex_schm_interval_set = coex_schm_interval_set_wrapper,
721     ._coex_schm_interval_get = coex_schm_interval_get_wrapper,
722     ._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper,
723     ._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper,
724     ._coex_register_start_cb = coex_register_start_cb_wrapper,
725     ._coex_schm_process_restart = coex_schm_process_restart_wrapper,
726     ._coex_schm_register_cb = coex_schm_register_cb_wrapper,
727     ._coex_schm_flexible_period_set = coex_schm_flexible_period_set_wrapper,
728     ._coex_schm_flexible_period_get = coex_schm_flexible_period_get_wrapper,
729     ._magic = ESP_WIFI_OS_ADAPTER_MAGIC,
730 };
731