Skip to content

Commit d48b519

Browse files
committed
feature: add Wi-Fi and BLE SW coexistence manager
Add all necessary sources to allow using software based controller to handle Wi-Fi and BLE coexistence. Signed-off-by: Sylvio Alves <[email protected]>
1 parent 48ef433 commit d48b519

21 files changed

+1400
-792
lines changed

zephyr/esp32/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,23 @@ if(CONFIG_SOC_SERIES_ESP32)
374374
../../components/esp_phy/src/phy_common.c
375375
)
376376

377+
if (CONFIG_ESP32_SW_COEXIST_ENABLE)
378+
zephyr_link_libraries_ifndef(
379+
CONFIG_BUILD_ONLY_NO_BLOBS
380+
## ble
381+
btdm_app
382+
-L${CMAKE_CURRENT_SOURCE_DIR}/../blobs/lib/esp32
383+
)
384+
endif()
385+
377386
zephyr_link_libraries_ifndef(
378387
CONFIG_BUILD_ONLY_NO_BLOBS
379388
net80211
380389
core
381390
pp
382391
phy
383392
rtc
393+
coexist
384394
## esp-idf wifi libs refer gcc libs symbols, so linked in libgcc
385395
gcc
386396
-L${CMAKE_CURRENT_SOURCE_DIR}/../blobs/lib/esp32
@@ -576,6 +586,7 @@ if(CONFIG_SOC_SERIES_ESP32)
576586

577587
zephyr_sources(
578588
src/wifi/esp_wifi_adapter.c
589+
src/coex/esp_coex_adapter.c
579590
../port/wifi/wifi_init.c
580591
${WPA_SUPPLICANT_SRCS}
581592
${ESP_SUPPLICANT_SRCS}

zephyr/esp32/src/bt/esp_bt_adapter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ static void IRAM_ATTR coex_bb_reset_unlock_wrapper(uint32_t restore)
885885
static int coex_schm_register_btdm_callback_wrapper(void *callback)
886886
{
887887
#if CONFIG_SW_COEXIST_ENABLE
888-
return coex_schm_register_callback(OEX_SCHM_CALLBACK_TYPE_BT, callback);
888+
return coex_schm_register_callback(COEX_SCHM_CALLBACK_TYPE_BT, callback);
889889
#else
890890
return 0;
891891
#endif
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "esp_private/wifi.h"
8+
#include "soc/soc_caps.h"
9+
#include "esp_attr.h"
10+
#include "esp32/rom/ets_sys.h"
11+
#include "esp_heap_adapter.h"
12+
#include "esp_timer.h"
13+
#include "soc/rtc.h"
14+
#include "esp_private/esp_clk.h"
15+
#include "private/esp_coexist_adapter.h"
16+
17+
#include <zephyr/logging/log.h>
18+
LOG_MODULE_REGISTER(esp32_coex_adapter, CONFIG_WIFI_LOG_LEVEL);
19+
20+
#define OSI_FUNCS_TIME_BLOCKING 0xffffffff
21+
22+
void esp_wifi_free(void *mem)
23+
{
24+
esp_wifi_free_func(mem);
25+
}
26+
27+
bool IRAM_ATTR esp_coex_common_env_is_chip_wrapper(void)
28+
{
29+
#ifdef CONFIG_IDF_ENV_FPGA
30+
return false;
31+
#else
32+
return true;
33+
#endif
34+
}
35+
36+
void *esp_coex_common_spin_lock_create_wrapper(void)
37+
{
38+
unsigned int *wifi_spin_lock = (unsigned int *)wifi_malloc(sizeof(unsigned int));
39+
if (wifi_spin_lock == NULL) {
40+
LOG_ERR("spin_lock_create_wrapper allocation failed");
41+
}
42+
43+
return (void *)wifi_spin_lock;
44+
}
45+
46+
uint32_t IRAM_ATTR esp_coex_common_int_disable_wrapper(void *wifi_int_mux)
47+
{
48+
unsigned int *int_mux = (unsigned int *)wifi_int_mux;
49+
50+
*int_mux = irq_lock();
51+
return 0;
52+
}
53+
54+
void IRAM_ATTR esp_coex_common_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp)
55+
{
56+
unsigned int *key = (unsigned int *)wifi_int_mux;
57+
58+
irq_unlock(*key);
59+
}
60+
61+
void IRAM_ATTR esp_coex_common_task_yield_from_isr_wrapper(void)
62+
{
63+
k_yield();
64+
}
65+
66+
void *esp_coex_common_semphr_create_wrapper(uint32_t max, uint32_t init)
67+
{
68+
struct k_sem *sem = (struct k_sem *)wifi_malloc(sizeof(struct k_sem));
69+
70+
if (sem == NULL) {
71+
LOG_ERR("semphr_create_wrapper allocation failed");
72+
}
73+
74+
k_sem_init(sem, init, max);
75+
76+
return (void *)sem;
77+
}
78+
79+
void esp_coex_common_semphr_delete_wrapper(void *semphr)
80+
{
81+
esp_wifi_free(semphr);
82+
}
83+
84+
int32_t esp_coex_common_semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
85+
{
86+
if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
87+
int ret = k_sem_take((struct k_sem *)semphr, K_FOREVER);
88+
if (ret == 0) {
89+
return 1;
90+
}
91+
} else {
92+
int ret = k_sem_take((struct k_sem *)semphr, K_TICKS(block_time_tick));
93+
94+
if (ret == 0) {
95+
return 1;
96+
}
97+
}
98+
return 0;
99+
}
100+
101+
int32_t esp_coex_common_semphr_give_wrapper(void *semphr)
102+
{
103+
k_sem_give((struct k_sem *)semphr);
104+
return 1;
105+
}
106+
107+
void IRAM_ATTR esp_coex_common_timer_disarm_wrapper(void *timer)
108+
{
109+
ets_timer_disarm(timer);
110+
}
111+
112+
void esp_coex_common_timer_done_wrapper(void *ptimer)
113+
{
114+
ets_timer_done(ptimer);
115+
}
116+
117+
void esp_coex_common_timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
118+
{
119+
ets_timer_setfn(ptimer, pfunction, parg);
120+
}
121+
122+
void IRAM_ATTR esp_coex_common_timer_arm_us_wrapper(void *ptimer, uint32_t us, bool repeat)
123+
{
124+
ets_timer_arm_us(ptimer, us, repeat);
125+
}
126+
127+
void *IRAM_ATTR esp_coex_common_malloc_internal_wrapper(size_t size)
128+
{
129+
return wifi_malloc(size);
130+
}
131+
132+
static int32_t IRAM_ATTR esp_coex_internal_semphr_take_from_isr_wrapper(void *semphr, void *hptw)
133+
{
134+
int *hpt = (int *)hptw;
135+
136+
int ret = k_sem_take((struct k_sem *)semphr, K_NO_WAIT);
137+
138+
if (ret == 0) {
139+
return 1;
140+
}
141+
142+
*hpt = 0;
143+
return 0;
144+
}
145+
146+
static int32_t IRAM_ATTR esp_coex_internal_semphr_give_from_isr_wrapper(void *semphr, void *hptw)
147+
{
148+
int *hpt = (int *)hptw;
149+
150+
k_sem_give((struct k_sem *)semphr);
151+
152+
*hpt = 0;
153+
return 0;
154+
}
155+
156+
static int esp_coexist_debug_matrix_init_wrapper(int evt, int sig, bool rev)
157+
{
158+
#if CONFIG_ESP_COEX_GPIO_DEBUG
159+
return esp_coexist_debug_matrix_init(evt, sig, rev);
160+
#else
161+
return ESP_ERR_NOT_SUPPORTED;
162+
#endif
163+
}
164+
165+
int32_t IRAM_ATTR esp_coex_is_in_isr_wrapper(void)
166+
{
167+
return k_is_in_isr();
168+
}
169+
170+
coex_adapter_funcs_t g_coex_adapter_funcs = {
171+
._version = COEX_ADAPTER_VERSION,
172+
._spin_lock_create = esp_coex_common_spin_lock_create_wrapper,
173+
._spin_lock_delete = esp_wifi_free,
174+
._int_disable = esp_coex_common_int_disable_wrapper,
175+
._int_enable = esp_coex_common_int_restore_wrapper,
176+
._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
177+
._semphr_create = esp_coex_common_semphr_create_wrapper,
178+
._semphr_delete = esp_coex_common_semphr_delete_wrapper,
179+
._semphr_take_from_isr = esp_coex_internal_semphr_take_from_isr_wrapper,
180+
._semphr_give_from_isr = esp_coex_internal_semphr_give_from_isr_wrapper,
181+
._semphr_take = esp_coex_common_semphr_take_wrapper,
182+
._semphr_give = esp_coex_common_semphr_give_wrapper,
183+
._is_in_isr = esp_coex_is_in_isr_wrapper,
184+
._malloc_internal = esp_coex_common_malloc_internal_wrapper,
185+
._free = esp_wifi_free,
186+
._esp_timer_get_time = esp_timer_get_time,
187+
._timer_disarm = esp_coex_common_timer_disarm_wrapper,
188+
._timer_done = esp_coex_common_timer_done_wrapper,
189+
._timer_setfn = esp_coex_common_timer_setfn_wrapper,
190+
._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
191+
._debug_matrix_init = esp_coexist_debug_matrix_init_wrapper,
192+
._magic = COEX_ADAPTER_MAGIC,
193+
};

0 commit comments

Comments
 (0)