Skip to content

esp32/network_wlan: Add channel bandwidth configuration parameter.#19260

Open
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:esp32-wifi-bandwidth
Open

esp32/network_wlan: Add channel bandwidth configuration parameter.#19260
agatti wants to merge 1 commit into
micropython:masterfrom
agatti:esp32-wifi-bandwidth

Conversation

@agatti
Copy link
Copy Markdown
Contributor

@agatti agatti commented May 22, 2026

Summary

This commit adds a configuration parameter to set and retrieve the WiFi channel bandwidth.

The configuration only applies when the adapter is in single-band mode (ie. either 2.4 GHz or 5 GHz, not both at the same time), which is not an issue at the moment since there's no provision to put the adapter in multi-band mode.

Rather than accepting the bandwidth as a raw MHz value, five new constants are introduced, following the same nomenclature as the ESP-IDF SDK. This is required since there's one constant that indicates a multi-antenna bandwidth setup and therefore not representable as a single integer value.

All constants involved are present in ESP-IDF 5.3 and later.

This addresses #6036.

Testing

Network connectivity tests were performed on ESP32, ESP32C3, and ESP32S3, checking whether changing bandwidth between 20MHz and 40MHz would not impact connection in STA mode, and whether a bandwidth change in AP mode would be reported by a network analyser running on a phone.

On older boards, setting constants above 40MHz may not trigger an error but automatically cap bandwidth to either 20MHz or 40MHz depending on the board.

The firmware was also built for ESP32P4 to make sure this feature should not be gated behind a define in hosted mode.

Trade-offs and Alternatives

There's a small size increase, but that cannot really be avoided. The fact that there's one bandwidth setting for multi-antenna setups also forces usage of constants.

The constant names could probably be more descriptive, like WLAN.BANDWIDTH_20 rather than WLAN.BW20, but I followed the SDK here and shorter names mean taking up less space. If more descriptive constant names are preferred, changing them is not a problem.

Generative AI

I did not use generative AI tools when creating this PR.


I originally planned to also support multi-mode WiFi since a supported target has it (ESP32C5), but then I realised there's no way to set the adapter into multi-mode to begin with. That requires some minor changes to protocol, channel, and bandwidth retrieval/setting procedures.

I can add multi-mode support and apply the necessary changes in another PR but somebody else has to test it as I do not have the suitable hardware (although I can make sure it builds :)) and put that behind a define if multi-mode is not available on the target.

This commit adds a configuration parameter to set and retrieve the
WiFi channel bandwidth.

The configuration only applies when the adapter is in single-band mode
(ie. either 2.4 GHz or 5 GHz, not both at the same time), which is not an
issue at the moment since there's no provision to put the adapter in
multi-band mode.

Rather than accepting the bandwidth as a raw MHz value, five new
constants are introduced, following the same nomenclature as the ESP-IDF
SDK.  This is required since there's one constant that indicates a
multi-antenna bandwidth setup and therefore not representable as a
single integer value.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@github-actions
Copy link
Copy Markdown

Code size report:

Reference:  docs/library/rp2.DMA: Correct and add pack_ctrl defaults. [8f3a416]
Comparison: esp32/network_wlan: Add channel bandwidth configuration parameter. [merge of bb5acc7]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32: +1612 +0.092% ESP32_GENERIC[incl +396(data)]
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@agatti
Copy link
Copy Markdown
Contributor Author

agatti commented May 22, 2026

A size increase of 1.5KiB is actually a lot for such a simple change :(

Should this be put behind a define, or just scrapped? I wouldn't say it's a critical feature after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant