summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/esp32/network_wlan.c43
1 files changed, 30 insertions, 13 deletions
diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c
index 7e802166c..66b72e495 100644
--- a/ports/esp32/network_wlan.c
+++ b/ports/esp32/network_wlan.c
@@ -549,21 +549,38 @@ static mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
break;
}
case MP_QSTR_channel: {
- uint8_t primary;
- wifi_second_chan_t secondary;
- // Get the current value of secondary
- esp_exceptions(esp_wifi_get_channel(&primary, &secondary));
- primary = mp_obj_get_int(kwargs->table[i].value);
- esp_err_t err = esp_wifi_set_channel(primary, secondary);
- if (err == ESP_ERR_INVALID_ARG) {
- // May need to swap secondary channel above to below or below to above
- secondary = (
- (secondary == WIFI_SECOND_CHAN_ABOVE)
- ? WIFI_SECOND_CHAN_BELOW
- : (secondary == WIFI_SECOND_CHAN_BELOW)
+ uint8_t channel = mp_obj_get_int(kwargs->table[i].value);
+ if (self->if_id == ESP_IF_WIFI_AP) {
+ cfg.ap.channel = channel;
+ } else {
+ // This setting is only used to determine the
+ // starting channel for a scan, so it can result in
+ // slightly faster connection times.
+ cfg.sta.channel = channel;
+
+ // This additional code to directly set the channel
+ // on the STA interface is only relevant for ESP-NOW
+ // (when there is no STA connection attempt.)
+ uint8_t old_primary;
+ wifi_second_chan_t secondary;
+ // Get the current value of secondary
+ esp_exceptions(esp_wifi_get_channel(&old_primary, &secondary));
+ esp_err_t err = esp_wifi_set_channel(channel, secondary);
+ if (err == ESP_ERR_INVALID_ARG) {
+ // May need to swap secondary channel above to below or below to above
+ secondary = (
+ (secondary == WIFI_SECOND_CHAN_ABOVE)
+ ? WIFI_SECOND_CHAN_BELOW
+ : (secondary == WIFI_SECOND_CHAN_BELOW)
? WIFI_SECOND_CHAN_ABOVE
: WIFI_SECOND_CHAN_NONE);
- esp_exceptions(esp_wifi_set_channel(primary, secondary));
+ err = esp_wifi_set_channel(channel, secondary);
+ }
+ esp_exceptions(err);
+ if (channel != old_primary) {
+ // Workaround the ESP-IDF Wi-Fi stack sometimes taking a moment to change channels
+ mp_hal_delay_ms(1);
+ }
}
break;
}