8000 ports/esp32/modnetwork.c: Separated WLAN initialization from LAN · nickzoic/micropython-esp32@6e88117 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e88117

Browse files
committed
ports/esp32/modnetwork.c: Separated WLAN initialization from LAN
1 parent 5bd547f commit 6e88117

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

ports/esp32/modnetwork.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ STATIC void require_if(mp_obj_t wlan_if, int if_no) {
204204
}
205205

206206
STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
207+
static int initialized = 0;
208+
if (!initialized) {
209+
ESP_LOGD("modnetwork", "esp_event_loop_init done");
210+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
211+
ESP_LOGD("modnetwork", "Initializing WiFi");
212+
ESP_EXCEPTIONS( esp_wifi_init(&cfg) );
213+
ESP_EXCEPTIONS( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
214+
ESP_LOGD("modnetwork", "Initialized");
215+
ESP_EXCEPTIONS( esp_wifi_set_mode(0) );
216+
ESP_EXCEPTIONS( esp_wifi_start() );
217+
ESP_LOGD("modnetwork", "Started");
218+
initialized = 1;
219+
}
220+
207221
int idx = ( D9A5 n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA;
208222
if (idx == WIFI_IF_STA) {
209223
return MP_OBJ_FROM_PTR(&wlan_sta_obj);
@@ -284,7 +298,7 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
284298
lan_if_obj_t* self = MP_OBJ_TO_PTR(&lan_obj);
285299

286300
if (self->initialized) {
287-
mp_raise_msg(&mp_type_OSError, "ethernet already initialized");
301+
return MP_OBJ_FROM_PTR(&lan_obj);
288302
}
289303

290304
enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type };
@@ -345,15 +359,6 @@ STATIC mp_obj_t esp_initialize() {
345359
tcpip_adapter_init();
346360
ESP_LOGD("modnetwork", "Initializing Event Loop");
347361
ESP_EXCEPTIONS( esp_event_loop_init(event_handler, NULL) );
348-
ESP_LOGD("modnetwork", "esp_event_loop_init done");
349-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
350-
ESP_LOGD("modnetwork", "Initializing WiFi");
351-
ESP_EXCEPTIONS( esp_wifi_init(&cfg) );
352-
ESP_EXCEPTIONS( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
353-
ESP_LOGD("modnetwork", "Initialized");
354-
ESP_EXCEPTIONS( esp_wifi_set_mode(0) );
355-
ESP_EXCEPTIONS( esp_wifi_start() );
356-
ESP_LOGD("modnetwork", "Started");
357362
initialized = 1;
358363
}
359364
return mp_const_none;

0 commit comments

Comments
 (0)
0