Skip to content

Commit 03bc561

Browse files
projectgusdpgeorge
authored andcommitted
esp32: Fix setting WLAN channel in AP mode.
- Previously the call to esp_wifi_set_channel() would be immediately overridden by calling esp_wifi_config(...) with the previous channel set. - AP interface doesn't seem to need more than esp_wifi_config(...) to work. It will automatically configure 40MHz bandwidth and place the secondary channel using similar logic to what was being explicitly calculated here. - However, calling esp_wifi_set_channel() on the STA interface is necessary if using this interface with ESP-NOW (without connecting to an AP). So the esp_wifi_set_channel() call is kept in for this purpose. Without this, tests/multi_espnow/70_channel.py fails. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 49b83ed commit 03bc561

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

ports/esp32/network_wlan.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -549,21 +549,38 @@ static mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
549549
break;
550550
}
551551
case MP_QSTR_channel: {
552-
uint8_t primary;
553-
wifi_second_chan_t secondary;
554-
// Get the current value of secondary
555-
esp_exceptions(esp_wifi_get_channel(&primary, &secondary));
556-
primary = mp_obj_get_int(kwargs->table[i].value);
557-
esp_err_t err = esp_wifi_set_channel(primary, secondary);
558-
if (err == ESP_ERR_INVALID_ARG) {
559-
// May need to swap secondary channel above to below or below to above
560-
secondary = (
561-
(secondary == WIFI_SECOND_CHAN_ABOVE)
562-
? WIFI_SECOND_CHAN_BELOW
563-
: (secondary == WIFI_SECOND_CHAN_BELOW)
552+
uint8_t channel = mp_obj_get_int(kwargs->table[i].value);
553+
if (self->if_id == ESP_IF_WIFI_AP) {
554+
cfg.ap.channel = channel;
555+
} else {
556+
// This setting is only used to determine the
557+
// starting channel for a scan, so it can result in
558+
// slightly faster connection times.
559+
cfg.sta.channel = channel;
560+
561+
// This additional code to directly set the channel
562+
// on the STA interface is only relevant for ESP-NOW
563+
// (when there is no STA connection attempt.)
564+
uint8_t old_primary;
565+
wifi_second_chan_t secondary;
566+
// Get the current value of secondary
567+
esp_exceptions(esp_wifi_get_channel(&old_primary, &secondary));
568+
esp_err_t err = esp_wifi_set_channel(channel, secondary);
569+
if (err == ESP_ERR_INVALID_ARG) {
570+
// May need to swap secondary channel above to below or below to above
571+
secondary = (
572+
(secondary == WIFI_SECOND_CHAN_ABOVE)
573+
? WIFI_SECOND_CHAN_BELOW
574+
: (secondary == WIFI_SECOND_CHAN_BELOW)
564575
? WIFI_SECOND_CHAN_ABOVE
565576
: WIFI_SECOND_CHAN_NONE);
566-
esp_exceptions(esp_wifi_set_channel(primary, secondary));
577+
err = esp_wifi_set_channel(channel, secondary);
578+
}
579+
esp_exceptions(err);
580+
if (channel != old_primary) {
581+
// Workaround the ESP-IDF Wi-Fi stack sometimes taking a moment to change channels
582+
mp_hal_delay_ms(1);
583+
}
567584
}
568585
break;
569586
}

0 commit comments

Comments
 (0)