|
4 | 4 | #include "espbridge/protocol.h" |
5 | 5 | #include "espbridge/modules.h" |
6 | 6 | #include <WiFi.h> |
7 | | -#include <esp_wifi.h> |
8 | 7 |
|
9 | 8 | static bool scanning = false; |
10 | 9 | static bool ap_active = false; |
11 | 10 | static bool sta_started = false; |
12 | | -static bool coex_pinned = false; // Wi-Fi pre-inited for BLE coex: never drop to MODE_NULL |
13 | | -static bool wifi_parked = false; // driver inited but radio stopped (coex idle) |
14 | 11 |
|
15 | | -bool wifi_is_active() { |
16 | | - // Parked = driver up, RF off: no ADC2 conflict, no coex airtime claimed. |
17 | | - return WiFi.getMode() != WIFI_MODE_NULL && !wifi_parked; |
18 | | -} |
19 | | - |
20 | | -// Classic-ESP32 coexistence: the Wi-Fi driver must come up BEFORE Bluedroid |
21 | | -// (heap ordering — Bluedroid's init crashes if the Wi-Fi driver grabs its |
22 | | -// buffers afterwards). Called from setup() ahead of link_ble_init() when |
23 | | -// BRIDGE_WIFI_COEX is set. Power save stays at the Arduino default |
24 | | -// WIFI_PS_MIN_MODEM — customizing it (especially WIFI_PS_NONE) destabilizes |
25 | | -// BLE coexistence per the IDF guide. |
26 | | -// |
27 | | -// Lesson from Esp-WiFi-BLE-Now: only bring the radio up when it is actually |
28 | | -// used. An idle-but-started STA makes the coex arbiter timeslice the |
29 | | -// 2.4 GHz radio against BLE forever (an unassociated STA never modem- |
30 | | -// sleeps) and holds ~25 KB of RX buffers — both destabilize the BLE link. |
31 | | -// So: init the driver in order, then PARK the radio until the host first |
32 | | -// asks for Wi-Fi/ESP-NOW (wifi_ensure_started below). |
33 | | -void wifi_coex_preinit() { |
34 | | - WiFi.mode(WIFI_STA); |
35 | | - esp_wifi_stop(); |
36 | | - wifi_parked = true; |
37 | | - coex_pinned = true; |
38 | | - proto_log_heap("coex: wifi inited, radio parked"); |
39 | | -} |
40 | | - |
41 | | -// Resume the parked radio before any Wi-Fi/ESP-NOW use. Arduino's layer |
42 | | -// still believes Wi-Fi is started (we stopped underneath it), so this is |
43 | | -// the matching low-level start; a no-op everywhere else. |
44 | | -void wifi_ensure_started() { |
45 | | - if (!wifi_parked) return; |
46 | | - wifi_parked = false; |
47 | | - esp_wifi_start(); |
48 | | - proto_log_heap("coex: wifi radio resumed"); |
49 | | -} |
| 12 | +// Wi-Fi is off until the first command here brings it up (WiFi.mode below); |
| 13 | +// a BLE-only board never pays the driver's heap. Leave coex/power-save at |
| 14 | +// IDF defaults (never WIFI_PS_NONE with BT) — the SW arbiter handles slotting. |
| 15 | +bool wifi_is_active() { return WiFi.getMode() != WIFI_MODE_NULL; } |
50 | 16 |
|
51 | 17 | static void on_wifi_event(WiFiEvent_t event, WiFiEventInfo_t info) { |
52 | 18 | uint8_t buf[5] = {0}; |
@@ -117,7 +83,6 @@ static bool take_str(const uint8_t*& p, uint16_t& left, char* out, uint8_t cap) |
117 | 83 |
|
118 | 84 | void wifi_handle(uint8_t op, uint8_t seq, const uint8_t* p, uint16_t len) { |
119 | 85 | uint16_t cmd = CMD(MOD_WIFI, op); |
120 | | - wifi_ensure_started(); // coex: the radio is parked until first Wi-Fi use |
121 | 86 | switch (op) { |
122 | 87 | case 0x01: { // SCAN (async) |
123 | 88 | // Quirk: a scan hops channels, so ESP-NOW packets are dropped while it |
@@ -150,9 +115,8 @@ void wifi_handle(uint8_t op, uint8_t seq, const uint8_t* p, uint16_t len) { |
150 | 115 | case 0x03: // DISCONNECT |
151 | 116 | WiFi.disconnect(true /*wifioff if no AP*/, false); |
152 | 117 | sta_started = false; |
153 | | - // Keep the radio in STA mode while ESP-NOW rides on it or the driver is |
154 | | - // coex-pinned (turning it off would break espnow / the BLE coex order). |
155 | | - if (!ap_active && !espnow_is_active() && !coex_pinned) WiFi.mode(WIFI_MODE_NULL); |
| 118 | + // Drop the radio (frees its heap) unless an AP or ESP-NOW still needs it. |
| 119 | + if (!ap_active && !espnow_is_active()) WiFi.mode(WIFI_MODE_NULL); |
156 | 120 | proto_reply_ok(seq, cmd); |
157 | 121 | break; |
158 | 122 |
|
@@ -196,7 +160,7 @@ void wifi_handle(uint8_t op, uint8_t seq, const uint8_t* p, uint16_t len) { |
196 | 160 | case 0x06: // AP_STOP |
197 | 161 | WiFi.softAPdisconnect(true); |
198 | 162 | ap_active = false; |
199 | | - if (!sta_started && !espnow_is_active() && !coex_pinned) WiFi.mode(WIFI_MODE_NULL); |
| 163 | + if (!sta_started && !espnow_is_active()) WiFi.mode(WIFI_MODE_NULL); |
200 | 164 | proto_reply_ok(seq, cmd); |
201 | 165 | break; |
202 | 166 |
|
|
0 commit comments