From 05ceca02ab498e8a157784b606b92d38b9fa5b67 Mon Sep 17 00:00:00 2001 From: Bob Date: Fri, 21 Nov 2025 10:53:44 -0800 Subject: [PATCH] more on CSI and S3 --- CMakeLists.txt | 2 +- components/wifi_cfg/wifi_cfg.c | 53 +++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9673e51..ec3d59a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.22) include($ENV{IDF_PATH}/tools/cmake/project.cmake) # "Trim" the build. Include the minimal set of components, main, and anything it depends on. idf_build_set_property(MINIMAL_BUILD ON) -project(hello_world) +project(CSI) diff --git a/components/wifi_cfg/wifi_cfg.c b/components/wifi_cfg/wifi_cfg.c index 009801e..bc0b0bb 100644 --- a/components/wifi_cfg/wifi_cfg.c +++ b/components/wifi_cfg/wifi_cfg.c @@ -206,24 +206,33 @@ bool wifi_cfg_apply_from_nvs(void) { } ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); - // Enable WiFi 6 (802.11ax) and all protocols for best compatibility + + // Configure WiFi protocols based on chip capabilities +#if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 + // Dual-band chips (2.4GHz + 5GHz support) wifi_protocols_t protocols = { - // 2.4 GHz: b/g/n/ax + // 2.4 GHz: b/g/n (no ax for CSI compatibility) .ghz_2g = WIFI_PROTOCOL_11B | - WIFI_PROTOCOL_11G | - WIFI_PROTOCOL_11N, - // Restrict protocols so CSI can work: - // - CSI on ESP32-C5 is only supported on 2.4 GHz HT (11n), not HE (11ax) - // WIFI_PROTOCOL_11AX, + WIFI_PROTOCOL_11G | + WIFI_PROTOCOL_11N, // 5 GHz: a/n/ac/ax .ghz_5g = WIFI_PROTOCOL_11A | - WIFI_PROTOCOL_11N | - WIFI_PROTOCOL_11AC | - WIFI_PROTOCOL_11AX, - // .ghz_6g will be zero-initialized (not used on C5) + WIFI_PROTOCOL_11N | + WIFI_PROTOCOL_11AC | + WIFI_PROTOCOL_11AX, }; - ESP_ERROR_CHECK( esp_wifi_set_protocols(WIFI_IF_STA, &protocols) ); +#else + // Single-band chips (2.4GHz only) - ESP32, ESP32-S2, ESP32-S3, ESP32-C3, etc. + // Use legacy API for 2.4GHz-only chips + uint8_t protocol_bitmap = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N; + ESP_ERROR_CHECK( esp_wifi_set_protocol(WIFI_IF_STA, protocol_bitmap) ); + + // Warn if user requested 5GHz on a 2.4GHz-only chip + if (strcmp(band, "5G") == 0) { + ESP_LOGW(TAG, "5GHz requested but this chip only supports 2.4GHz - using 2.4GHz"); + } +#endif ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wcfg) ); @@ -237,7 +246,8 @@ bool wifi_cfg_apply_from_nvs(void) { if (err2 != ESP_OK && err2 != ESP_ERR_INVALID_STATE) { ESP_ERROR_CHECK(err2); } // Set bandwidth AFTER WiFi is started but BEFORE connect - // For ESP32-C5 dual-band, use esp_wifi_set_bandwidths() with struct +#if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 + // Dual-band chips use struct-based API wifi_bandwidths_t bandwidths = { .ghz_2g = WIFI_BW_HT20, .ghz_5g = WIFI_BW_HT20 @@ -251,11 +261,26 @@ bool wifi_cfg_apply_from_nvs(void) { ESP_LOGI(TAG, "Setting bandwidth to HT20 (20MHz) for both bands"); } - // Use dual-band API with struct pointer err2 = esp_wifi_set_bandwidths(WIFI_IF_STA, &bandwidths); if (err2 != ESP_OK) { ESP_LOGW(TAG, "Failed to set bandwidths: %s", esp_err_to_name(err2)); } +#else + // Single-band chips (2.4GHz only) use legacy API + wifi_bandwidth_t bandwidth = WIFI_BW_HT20; + + if (strcmp(bw, "HT40") == 0) { + bandwidth = WIFI_BW_HT40; + ESP_LOGI(TAG, "Setting bandwidth to HT40 (40MHz) for 2.4GHz"); + } else { + ESP_LOGI(TAG, "Setting bandwidth to HT20 (20MHz) for 2.4GHz"); + } + + err2 = esp_wifi_set_bandwidth(WIFI_IF_STA, bandwidth); + if (err2 != ESP_OK) { + ESP_LOGW(TAG, "Failed to set bandwidth: %s", esp_err_to_name(err2)); + } +#endif err2 = esp_wifi_connect(); if (err2 != ESP_OK && err2 != ESP_ERR_WIFI_NOT_INIT && err2 != ESP_ERR_INVALID_STATE) { ESP_ERROR_CHECK(err2); }