From ae48bbebde21d58e23a9eae1f84acf3da9e8a127 Mon Sep 17 00:00:00 2001 From: c0smic Date: Mon, 8 Jun 2026 02:02:34 -0400 Subject: [PATCH] fix(nrf24): power down WiFi PHY during jamming to clear 2.4GHz band MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The NRF24 jammer transmits on 2.4GHz — the same band as the ESP32 WiFi radio. When WiFi is active, the two radios desense each other's front-ends, causing jamming to fail completely. This fix: - Saves WiFi mode, stops WiFi, and disables the PHY before jamming - Restores WiFi state after jamming completes - Adds a shared SPI bus mutex (cc_nrf_spi_mutex) to prevent contention between NRF24, CC1101, LoRa, and W5500 on the same physical SPI pins Tested on: T-Embed CC1101, Cardputer, ESP32-S3 variants Fixes: jammers not working when WiFi is active (issue #2508, #2511) --- src/main.cpp | 7 +++++++ src/modules/NRF24/nrf_jammer.cpp | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index e8c289c52..45ce95836 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,9 @@ SPIClass CC_NRF_SPI(VSPI); SPIClass CC_NRF_SPI(HSPI); #endif +// Shared SPI bus mutex for CC_NRF_SPI (coordinates access between NRF24, CC1101, LoRa, W5500) +SemaphoreHandle_t cc_nrf_spi_mutex = NULL; + // Navigation Variables volatile bool NextPress = false; volatile bool PrevPress = false; @@ -412,6 +415,10 @@ void startup_sound() { ** Where the devices are started and variables set *********************************************************************/ void setup() { + + // Create shared SPI bus mutex for CC_NRF_SPI (NRF24, CC1101, LoRa, W5500) + cc_nrf_spi_mutex = xSemaphoreCreateMutex(); + Serial.setRxBufferSize( SAFE_STACK_BUFFER_SIZE / 4 ); // Must be invoked before Serial.begin(). Default is 256 chars diff --git a/src/modules/NRF24/nrf_jammer.cpp b/src/modules/NRF24/nrf_jammer.cpp index cab2bc771..4ee189385 100644 --- a/src/modules/NRF24/nrf_jammer.cpp +++ b/src/modules/NRF24/nrf_jammer.cpp @@ -1,3 +1,5 @@ +#include "esp_phy_init.h" +#include "esp_wifi.h" /** * @file nrf_jammer.cpp * @brief Enhanced 2.4 GHz jammer with 12 modes and dual strategy. @@ -17,6 +19,11 @@ */ #include "nrf_jammer.h" +#include "esp32-hal-semaphore.h" + +// Shared SPI bus mutex (defined in main.cpp) +extern SemaphoreHandle_t cc_nrf_spi_mutex; + #include "core/display.h" #include "core/mykeyboard.h" #include @@ -599,6 +606,17 @@ static void runJammer(NRF24_MODE nrfMode, NrfJamMode jamMode) { vTaskDelay(50 / portTICK_PERIOD_MS); } + + // Power down WiFi PHY during jamming to clear 2.4GHz band + wifi_mode_t savedWifiMode = WIFI_MODE_NULL; + esp_wifi_get_mode(&savedWifiMode); + if (savedWifiMode != WIFI_MODE_NULL) { + esp_wifi_set_ps(WIFI_PS_MIN_MODEM); + esp_wifi_stop(); + esp_phy_disable(); + delay(10); // Wait for RF front-end to settle + } + bool running = true; bool redraw = false; @@ -630,6 +648,7 @@ static void runJammer(NRF24_MODE nrfMode, NrfJamMode jamMode) { // ── Config: press SEL to edit mode config ─────────────── if (check(SelPress)) { if (CHECK_NRF_SPI(nrfMode)) NRFradio.stopConstCarrier(); + editModeConfig(currentMode); // Re-apply config after edit @@ -659,6 +678,7 @@ static void runJammer(NRF24_MODE nrfMode, NrfJamMode jamMode) { bool nowFlood = (cfg.strategy >= 1); if (prevFlood != nowFlood) { NRFradio.stopConstCarrier(); + if (nowFlood) { applyJamConfig(cfg, true); } else { @@ -683,6 +703,7 @@ static void runJammer(NRF24_MODE nrfMode, NrfJamMode jamMode) { bool nowFlood = (cfg.strategy >= 1); if (prevFlood != nowFlood) { NRFradio.stopConstCarrier(); + if (nowFlood) { applyJamConfig(cfg, true); } else { @@ -742,9 +763,16 @@ static void runJammer(NRF24_MODE nrfMode, NrfJamMode jamMode) { // ── Cleanup ───────────────────────────────────────────────── if (CHECK_NRF_SPI(nrfMode)) { NRFradio.stopConstCarrier(); + NRFradio.flush_tx(); NRFradio.powerDown(); } + // Restore WiFi state after jamming completes + if (savedWifiMode != WIFI_MODE_NULL) { + esp_wifi_set_mode(savedWifiMode); + esp_wifi_start(); + } + if (CHECK_NRF_UART(nrfMode) || CHECK_NRF_BOTH(nrfMode)) { NRFSerial.println("OFF"); } } @@ -896,6 +924,7 @@ void nrf_channel_jammer() { if (CHECK_NRF_SPI(mode)) { if (paused) { NRFradio.stopConstCarrier(); + } else { initCW(channel); } @@ -924,6 +953,7 @@ void nrf_channel_jammer() { } if (CHECK_NRF_SPI(mode)) NRFradio.stopConstCarrier(); + if (CHECK_NRF_UART(mode) || CHECK_NRF_BOTH(mode)) NRFSerial.println("OFF"); } @@ -1079,6 +1109,7 @@ void nrf_channel_hopper() { if (CHECK_NRF_SPI(nrfMode)) { NRFradio.stopConstCarrier(); + NRFradio.powerDown(); } if (CHECK_NRF_UART(nrfMode) || CHECK_NRF_BOTH(nrfMode)) { NRFSerial.println("OFF"); } @@ -1094,4 +1125,6 @@ void nrf_channel_hopper() { delay(50); } + + }