8000 Select ESP32 Wi-Fi protocol mode during WLAN init · pycom/pycom-micropython-sigfox@3b00898 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 3b00898

Browse files
committed
Select ESP32 Wi-Fi protocol mode during WLAN init
1 parent 5cf02dc commit 3b00898

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

docs/library/network.WLAN.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ Methods
7070
- ``channel`` a number in the range 1-11. Only needed when mode is ``WLAN.AP``.
7171
- ``antenna`` selects between the internal and the external antenna. Can be either
7272
``WLAN.INT_ANT`` or ``WLAN.EXT_ANT``.
73+
- ``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``.
7374
- ``power_save`` enables or disables power save functions in STA mode.
7475

76+
7577
For example, you can do::
7678

7779
# create and configure as an access point
@@ -194,3 +196,10 @@ Constants
194196
WLAN.EXT_ANT
195197

196198
selects the antenna type
199+
200+
.. data:: WLAN.PROTOCOL_11B
201+
WLAN.PROTOCOL_11G
202+
WLAN.PROTOCOL_11N
203+
WLAN.PROTOCOL_LR
204+
205+
selects the Wi-Fi protocol mode

esp32/mods/modwlan.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void wlan_pre_init (void) {
169169
wlan_obj.base.type = (mp_obj_t)&mod_network_nic_type_wlan;
170170
}
171171

172-
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) {
172+
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) {
173173

174174
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
175175
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,
198198

199199
// start the servers before returning
200200
wlan_servers_start();
201+
202+
// set wifi protocol mode
203+
int32_t wifi_interface = 0;
204+
switch( mode ) {
205+
case WIFI_MODE_STA:
206+
wifi_interface = ESP_IF_WIFI_STA;
207+
break;
208+
case WIFI_MODE_AP:
209+
wifi_interface = ESP_IF_WIFI_AP;
210+
break;
211+
default:
212+
wifi_interface = ESP_IF_WIFI_STA;
213+
}
214+
ESP_ERROR_CHECK(esp_wifi_set_protocol(wifi_interface, protocol_mode));
215+
201216
}
202217

203218
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) {
664679

665680
wlan_obj.pwrsave = args[5].u_bool;
666681
bool hidden = args[6].u_bool;
682+
int32_t protocol_mode = args[7].u_int;
667683

668684
if (mode != WIFI_MODE_STA) {
669685
if (ssid == NULL) {
@@ -675,7 +691,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) {
675691
}
676692

677693
// initialize the wlan subsystem
678-
wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false, hidden);
694+
wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false, hidden, protocol_mode);
679695
mod_network_register_nic(&wlan_obj);
680696

681697
return mp_const_none;
@@ -690,6 +706,7 @@ STATIC const mp_arg_t wlan_init_args[] = {
690706
{ MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
691707
{ MP_QSTR_power_save, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
692708
{ MP_QSTR_hidden, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
709+
{ MP_QSTR_protocol_mode,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N} },
693710
};
694711
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) {
695712
// parse args
@@ -1172,6 +1189,10 @@ STATIC const mp_map_elem_t wlan_locals_dict_table[] = {
11721189
{ MP_OBJ_NEW_QSTR(MP_QSTR_WPA2_ENT), MP_OBJ_NEW_SMALL_INT(WIFI_AUTH_WPA2_ENTERPRISE) },
11731190
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_INTERNAL) },
11741191
{ MP_OBJ_NEW_QSTR(MP_QSTR_EXT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_EXTERNAL) },
1192+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11B), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11B) },
1193+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11G), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11G) },
1194+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11N), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11N) },
1195+
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_LR), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_LR) },
11751196
// { MP_OBJ_NEW_QSTR(MP_QSTR_ANY_EVENT), MP_OBJ_NEW_SMALL_INT(MODWLAN_WIFI_EVENT_ANY) },
11761197
};
11771198
STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table);

esp32/mods/modwlan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extern wlan_obj_t wlan_obj;
7474
DECLARE PUBLIC FUNCTIONS
7575
******************************************************************************/
7676
extern void wlan_pre_init (void);
77-
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);
77+
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);
7878
extern void wlan_update(void);
7979
extern void wlan_get_mac (uint8_t *macAddress);
8080
extern void wlan_get_ip (uint32_t *ip);

esp32/mptask.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ STATIC void mptask_enable_wifi_ap (void) {
448448
uint8_t wifi_pwd[64];
449449
config_get_wifi_pwd(wifi_pwd);
450450
wlan_setup (WIFI_MODE_AP, DEFAULT_AP_SSID, WIFI_AUTH_WPA2_PSK, DEFAULT_AP_PASSWORD ,
451-
DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true, false);
451+
DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true, false, WIFI_PROTOCOL_11B |
452+
WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N );
452453
mod_network_register_nic(&wlan_obj);
453454
}
454455

0 commit comments

Comments
 (0)
0