From 3b00898f5ff3fdd18857eef03440c17933249d98 Mon Sep 17 00:00:00 2001 From: Rocco Lucia Date: Fri, 8 Mar 2019 20:31:04 +0100 Subject: [PATCH] Select ESP32 Wi-Fi protocol mode during WLAN init --- docs/library/network.WLAN.rst | 9 +++++++++ esp32/mods/modwlan.c | 25 +++++++++++++++++++++++-- esp32/mods/modwlan.h | 2 +- esp32/mptask.c | 3 ++- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/library/network.WLAN.rst b/docs/library/network.WLAN.rst index 6dfd1a0d43..789d8cd2e1 100644 --- a/docs/library/network.WLAN.rst +++ b/docs/library/network.WLAN.rst @@ -70,8 +70,10 @@ Methods - ``channel`` a number in the range 1-11. Only needed when mode is ``WLAN.AP``. - ``antenna`` selects between the internal and the external antenna. Can be either ``WLAN.INT_ANT`` or ``WLAN.EXT_ANT``. + - ``protocol_mode`` selects Wi-Fi protocol mode. Defaults to 802.11BGN (``WLAN.PROTOCOL_11B|WLAN.PROTOCOL_11G|WLAN.PROTOCOL_11N``). Here you can activate Espressif-patented long range mode with ``WLAN.PROTOCOL_LR``. - ``power_save`` enables or disables power save functions in STA mode. + For example, you can do:: # create and configure as an access point @@ -194,3 +196,10 @@ Constants WLAN.EXT_ANT selects the antenna type + +.. data:: WLAN.PROTOCOL_11B + WLAN.PROTOCOL_11G + WLAN.PROTOCOL_11N + WLAN.PROTOCOL_LR + + selects the Wi-Fi protocol mode diff --git a/esp32/mods/modwlan.c b/esp32/mods/modwlan.c index 11e4a5e8d6..21c0fb0f00 100644 --- a/esp32/mods/modwlan.c +++ b/esp32/mods/modwlan.c @@ -169,7 +169,7 @@ void wlan_pre_init (void) { wlan_obj.base.type = (mp_obj_t)&mod_network_nic_type_wlan; } -void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool hidden) { +void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool hidden, uint32_t protocol_mode) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); @@ -198,6 +198,21 @@ void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, // start the servers before returning wlan_servers_start(); + + // set wifi protocol mode + int32_t wifi_interface = 0; + switch( mode ) { + case WIFI_MODE_STA: + wifi_interface = ESP_IF_WIFI_STA; + break; + case WIFI_MODE_AP: + wifi_interface = ESP_IF_WIFI_AP; + break; + default: + wifi_interface = ESP_IF_WIFI_STA; + } + ESP_ERROR_CHECK(esp_wifi_set_protocol(wifi_interface, protocol_mode)); + } void wlan_get_mac (uint8_t *macAddress) { @@ -664,6 +679,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) { wlan_obj.pwrsave = args[5].u_bool; bool hidden = args[6].u_bool; + int32_t protocol_mode = args[7].u_int; if (mode != WIFI_MODE_STA) { if (ssid == NULL) { @@ -675,7 +691,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) { } // initialize the wlan subsystem - wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false, hidden); + wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false, hidden, protocol_mode); mod_network_register_nic(&wlan_obj); return mp_const_none; @@ -690,6 +706,7 @@ STATIC const mp_arg_t wlan_init_args[] = { { MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { MP_QSTR_power_save, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, { MP_QSTR_hidden, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, + { MP_QSTR_protocol_mode,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N} }, }; STATIC mp_obj_t wlan_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) { // parse args @@ -1172,6 +1189,10 @@ STATIC const mp_map_elem_t wlan_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_WPA2_ENT), MP_OBJ_NEW_SMALL_INT(WIFI_AUTH_WPA2_ENTERPRISE) }, { MP_OBJ_NEW_QSTR(MP_QSTR_INT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_INTERNAL) }, { MP_OBJ_NEW_QSTR(MP_QSTR_EXT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_EXTERNAL) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11B), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11B) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11G), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11G) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11N), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11N) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_LR), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_LR) }, // { MP_OBJ_NEW_QSTR(MP_QSTR_ANY_EVENT), MP_OBJ_NEW_SMALL_INT(MODWLAN_WIFI_EVENT_ANY) }, }; STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table); diff --git a/esp32/mods/modwlan.h b/esp32/mods/modwlan.h index d6a87afdaf..0850ee9973 100644 --- a/esp32/mods/modwlan.h +++ b/esp32/mods/modwlan.h @@ -74,7 +74,7 @@ extern wlan_obj_t wlan_obj; DECLARE PUBLIC FUNCTIONS ******************************************************************************/ extern void wlan_pre_init (void); -extern void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool ssid_hidden); +extern void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool ssid_hidden, uint32_t protocol_mode); extern void wlan_update(void); extern void wlan_get_mac (uint8_t *macAddress); extern void wlan_get_ip (uint32_t *ip); diff --git a/esp32/mptask.c b/esp32/mptask.c index 528c8d45f0..67c3d1c1fb 100644 --- a/esp32/mptask.c +++ b/esp32/mptask.c @@ -448,7 +448,8 @@ STATIC void mptask_enable_wifi_ap (void) { uint8_t wifi_pwd[64]; config_get_wifi_pwd(wifi_pwd); wlan_setup (WIFI_MODE_AP, DEFAULT_AP_SSID, WIFI_AUTH_WPA2_PSK, DEFAULT_AP_PASSWORD , - DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true, false); + DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true, false, WIFI_PROTOCOL_11B | + WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N ); mod_network_register_nic(&wlan_obj); }