more on 5G and 80MHz

This commit is contained in:
Bob 2025-12-03 16:09:12 -08:00
parent 7705499537
commit 64b3443085
3 changed files with 78 additions and 12 deletions

View File

@ -4,6 +4,8 @@
// - USB path uses driver API (usb_serial_jtag_read_bytes / write_bytes) — works with /dev/ttyACM* // - USB path uses driver API (usb_serial_jtag_read_bytes / write_bytes) — works with /dev/ttyACM*
// - Tolerates ESP_ERR_INVALID_STATE on repeated inits // - Tolerates ESP_ERR_INVALID_STATE on repeated inits
// - Supports DHCP or static IP, persists to NVS, applies immediately // - Supports DHCP or static IP, persists to NVS, applies immediately
// - Bandwidth options: HT20 (20MHz), HT40 (40MHz), VHT80 (80MHz, 5GHz only)
// - Power save modes: NONE (default, best for CSI), MIN/MIN_MODEM, MAX/MAX_MODEM
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -146,6 +148,8 @@ bool wifi_cfg_apply_from_nvs(void) {
char ssid[64]={0}, pass[64]={0}, ip[32]={0}, mask[32]={0}, gw[32]={0}; char ssid[64]={0}, pass[64]={0}, ip[32]={0}, mask[32]={0}, gw[32]={0};
char band[16]={0}, bw[16]={0}, powersave[16]={0}; char band[16]={0}, bw[16]={0}, powersave[16]={0};
bool dhcp = true; bool dhcp = true;
esp_err_t err2;
if (!load_cfg(ssid,sizeof(ssid), pass,sizeof(pass), ip,sizeof(ip), mask,sizeof(mask), gw,sizeof(gw), if (!load_cfg(ssid,sizeof(ssid), pass,sizeof(pass), ip,sizeof(ip), mask,sizeof(mask), gw,sizeof(gw),
band,sizeof(band), bw,sizeof(bw), powersave,sizeof(powersave), &dhcp)){ band,sizeof(band), bw,sizeof(bw), powersave,sizeof(powersave), &dhcp)){
ESP_LOGW(TAG, "No WiFi config in NVS"); ESP_LOGW(TAG, "No WiFi config in NVS");
@ -245,10 +249,7 @@ bool wifi_cfg_apply_from_nvs(void) {
if (sta_netif) esp_netif_dhcpc_start(sta_netif); if (sta_netif) esp_netif_dhcpc_start(sta_netif);
} }
esp_err_t err2 = esp_wifi_start(); // Set bandwidth
if (err2 != ESP_OK && err2 != ESP_ERR_INVALID_STATE) { ESP_ERROR_CHECK(err2); }
// Set bandwidth AFTER WiFi is started but BEFORE connect
#if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 #if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6
// Dual-band chips use struct-based API // Dual-band chips use struct-based API
wifi_bandwidths_t bandwidths = { wifi_bandwidths_t bandwidths = {
@ -256,7 +257,12 @@ bool wifi_cfg_apply_from_nvs(void) {
.ghz_5g = WIFI_BW_HT20 .ghz_5g = WIFI_BW_HT20
}; };
if (strcmp(bw, "HT40") == 0) { if (strcmp(bw, "VHT80") == 0) {
// 80MHz only supported on 5GHz
bandwidths.ghz_2g = WIFI_BW_HT40; // 2.4GHz fallback to 40MHz
bandwidths.ghz_5g = WIFI_BW80; // Use WIFI_BW80, not WIFI_BW_HT80
ESP_LOGI(TAG, "Setting bandwidth to VHT80 (80MHz) for 5GHz, HT40 (40MHz) for 2.4GHz");
} else if (strcmp(bw, "HT40") == 0) {
bandwidths.ghz_2g = WIFI_BW_HT40; bandwidths.ghz_2g = WIFI_BW_HT40;
bandwidths.ghz_5g = WIFI_BW_HT40; bandwidths.ghz_5g = WIFI_BW_HT40;
ESP_LOGI(TAG, "Setting bandwidth to HT40 (40MHz) for both bands"); ESP_LOGI(TAG, "Setting bandwidth to HT40 (40MHz) for both bands");
@ -272,7 +278,10 @@ bool wifi_cfg_apply_from_nvs(void) {
// Single-band chips (2.4GHz only) use legacy API // Single-band chips (2.4GHz only) use legacy API
wifi_bandwidth_t bandwidth = WIFI_BW_HT20; wifi_bandwidth_t bandwidth = WIFI_BW_HT20;
if (strcmp(bw, "HT40") == 0) { if (strcmp(bw, "VHT80") == 0) {
ESP_LOGW(TAG, "VHT80 (80MHz) not supported on 2.4GHz-only chips - using HT40 (40MHz)");
bandwidth = WIFI_BW_HT40;
} else if (strcmp(bw, "HT40") == 0) {
bandwidth = WIFI_BW_HT40; bandwidth = WIFI_BW_HT40;
ESP_LOGI(TAG, "Setting bandwidth to HT40 (40MHz) for 2.4GHz"); ESP_LOGI(TAG, "Setting bandwidth to HT40 (40MHz) for 2.4GHz");
} else { } else {
@ -285,6 +294,9 @@ bool wifi_cfg_apply_from_nvs(void) {
} }
#endif #endif
err2 = esp_wifi_start();
if (err2 != ESP_OK && err2 != ESP_ERR_INVALID_STATE) { ESP_ERROR_CHECK(err2); }
// Set power save mode based on configuration // Set power save mode based on configuration
wifi_ps_type_t ps_mode = WIFI_PS_NONE; // Default: no power save (best for CSI) wifi_ps_type_t ps_mode = WIFI_PS_NONE; // Default: no power save (best for CSI)

View File

@ -8,6 +8,12 @@
extern "C" { extern "C" {
#endif #endif
// Bandwidth constants - Define WIFI_BW_HT80 if not already defined by ESP-IDF
// ESP-IDF defines WIFI_BW_HT20 (0) and WIFI_BW_HT40 (1), but WIFI_BW_HT80 (2) may not exist yet
#ifndef WIFI_BW_HT80
#define WIFI_BW_HT80 WIFI_BW80 // 80MHz bandwidth (VHT80) for ESP32-C5/C6 on 5GHz
#endif
/** /**
* @brief Initialize the WiFi configuration system * @brief Initialize the WiFi configuration system
* *

View File

@ -265,12 +265,13 @@ static void event_handler(void* arg, esp_event_base_t event_base,
wifi_connected = true; wifi_connected = true;
current_led_state = LED_STATE_CONNECTED; current_led_state = LED_STATE_CONNECTED;
// Log connection details: SSID, band, channel, RSSI // Log connection details: SSID, band, channel, bandwidth, RSSI
wifi_config_t wifi_cfg; wifi_config_t wifi_cfg;
wifi_ap_record_t ap_info; wifi_ap_record_t ap_info;
if (esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg) == ESP_OK && if (esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg) == ESP_OK &&
esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) { esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) {
// Determine band from channel
const char *band_str = "Unknown"; const char *band_str = "Unknown";
if (ap_info.primary >= 1 && ap_info.primary <= 14) { if (ap_info.primary >= 1 && ap_info.primary <= 14) {
band_str = "2.4GHz"; band_str = "2.4GHz";
@ -278,10 +279,57 @@ static void event_handler(void* arg, esp_event_base_t event_base,
band_str = "5GHz"; band_str = "5GHz";
} }
// Get bandwidth - try dual-band API first, fallback to single-band
wifi_bandwidth_t bw = WIFI_BW_HT20; // Default to 20MHz
const char *bw_str = "Unknown";
bool bw_detected = false;
// Try esp_wifi_get_bandwidths() first (works on dual-band chips in auto mode)
wifi_bandwidths_t bandwidths = {0};
esp_err_t err = esp_wifi_get_bandwidths(WIFI_IF_STA, &bandwidths);
if (err == ESP_OK) {
// Dual-band API succeeded - select bandwidth based on current band
if (ap_info.primary >= 1 && ap_info.primary <= 14) {
// 2.4GHz band
bw = (wifi_bandwidth_t)bandwidths.ghz_2g;
bw_detected = true;
} else if (ap_info.primary >= 36) {
// 5GHz band
bw = (wifi_bandwidth_t)bandwidths.ghz_5g;
bw_detected = true;
}
} else {
// Dual-band API failed - try single-band API (ESP32-S3, older IDF)
err = esp_wifi_get_bandwidth(WIFI_IF_STA, &bw);
if (err == ESP_OK) {
bw_detected = true;
}
}
// Convert bandwidth enum to string
if (bw_detected) {
switch (bw) {
case WIFI_BW_HT20:
bw_str = "20MHz (HT20)";
break;
case WIFI_BW_HT40:
bw_str = "40MHz (HT40)";
break;
case WIFI_BW80:
bw_str = "80MHz (VHT80)";
break;
default:
bw_str = "Unknown";
break;
}
}
ESP_LOGI(TAG, "========================================"); ESP_LOGI(TAG, "========================================");
ESP_LOGI(TAG, "WiFi CONNECTED - BLUE LED solid"); ESP_LOGI(TAG, "WiFi CONNECTED - BLUE LED solid");
ESP_LOGI(TAG, " SSID: '%s'", wifi_cfg.sta.ssid); ESP_LOGI(TAG, " SSID: '%s'", wifi_cfg.sta.ssid);
ESP_LOGI(TAG, " Band: %s", band_str); ESP_LOGI(TAG, " Band: %s", band_str);
ESP_LOGI(TAG, " Bandwidth: %s", bw_str);
ESP_LOGI(TAG, " Channel: %d", ap_info.primary); ESP_LOGI(TAG, " Channel: %d", ap_info.primary);
ESP_LOGI(TAG, " RSSI: %d dBm", ap_info.rssi); ESP_LOGI(TAG, " RSSI: %d dBm", ap_info.rssi);
ESP_LOGI(TAG, " BSSID: %02x:%02x:%02x:%02x:%02x:%02x", ESP_LOGI(TAG, " BSSID: %02x:%02x:%02x:%02x:%02x:%02x",