Skip to content

Commit 594be73

Browse files
author
blue-2357
committed
simplify WiFi boot procedure to prepare for on-demand stack load
currently ```esp_wifi_init``` have to be called in ```app_main``` or WiFi will fail to boot. When possible to boot later, code will be moved into ```_esp_wifi_start``` to be executed when necessary
1 parent a52c95e commit 594be73

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-50
lines changed

cores/esp32/main.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ extern "C" void initArduino();
88
extern void loop();
99
extern void setup();
1010

11-
void startWiFi() __attribute__((weak));
12-
void startWiFi() {}
13-
1411
void loopTask(void *pvParameters)
1512
{
1613
bool setup_done = false;
1714
for(;;) {
1815
if(!setup_done) {
19-
startWiFi();
2016
setup();
2117
setup_done = true;
2218
}

libraries/WiFi/src/WiFiGeneric.cpp

+69-46
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,75 @@ extern "C" {
4040
#include "lwip/opt.h"
4141
#include "lwip/err.h"
4242
#include "lwip/dns.h"
43+
#include "esp_ipc.h"
4344

4445
#include "esp32-hal-log.h"
46+
47+
/**
48+
* Boot and start WiFi
49+
* This method get's called on boot if you use any of the WiFi methods.
50+
* If you do not link to this library, WiFi will not be started.
51+
* */
52+
static bool _esp_wifi_initalized = false;
53+
extern void initWiFi()
54+
{
55+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
56+
tcpip_adapter_init();
57+
esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL);
58+
esp_wifi_init(&cfg);
59+
esp_wifi_set_storage(WIFI_STORAGE_RAM);
60+
_esp_wifi_initalized = true;
4561
}
4662

63+
} //extern "C"
64+
4765
#undef min
4866
#undef max
4967
#include <vector>
5068

69+
static bool _esp_wifi_start()
70+
{
71+
static bool started = false;
72+
esp_err_t err;
73+
74+
if(!_esp_wifi_initalized){
75+
initWiFi();
76+
if(!_esp_wifi_initalized){
77+
log_w("not initialized");
78+
return false;
79+
}
80+
}
81+
if(started){
82+
return true;
83+
}
84+
started = true;
85+
err = esp_wifi_start();
86+
if (err != ESP_OK) {
87+
log_e("%d", err);
88+
return false;
89+
}
90+
#if CONFIG_AUTOCONNECT_WIFI
91+
wifi_mode_t mode = WIFI_MODE_NULL;
92+
bool auto_connect = false;
93+
94+
err = esp_wifi_get_mode(&mode);
95+
if (err != ESP_OK) {
96+
log_e("esp_wifi_get_mode: %d", err);
97+
return false;
98+
}
99+
100+
err = esp_wifi_get_auto_connect(&auto_connect);
101+
if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) {
102+
err = esp_wifi_connect();
103+
if (err != ESP_OK) {
104+
log_e("esp_wifi_connect: %d", err);
105+
return false;
106+
}
107+
}
108+
#endif
109+
return true;
110+
}
111+
51112
// -----------------------------------------------------------------------------------------------------------------------
52113
// ------------------------------------------------- Generic WiFi function -----------------------------------------------
53114
// -----------------------------------------------------------------------------------------------------------------------
@@ -167,7 +228,11 @@ void WiFiGenericClass::persistent(bool persistent)
167228
*/
168229
bool WiFiGenericClass::mode(wifi_mode_t m)
169230
{
170-
if(getMode() == m) {
231+
wifi_mode_t cm = getMode();
232+
if(cm == WIFI_MODE_MAX){
233+
return false;
234+
}
235+
if(cm == m) {
171236
return true;
172237
}
173238
return esp_wifi_set_mode(m) == ESP_OK;
@@ -180,6 +245,9 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
180245
wifi_mode_t WiFiGenericClass::getMode()
181246
{
182247
uint8_t mode;
248+
if(!_esp_wifi_start()){
249+
return WIFI_MODE_MAX;
250+
}
183251
esp_wifi_get_mode((wifi_mode_t*)&mode);
184252
return (wifi_mode_t)mode;
185253
}
@@ -275,48 +343,3 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca
275343
_dns_busy = false;
276344
}
277345

278-
/**
279-
* Boot and start WiFi
280-
* This method get's called on boot if you use any of the WiFi methods.
281-
* If you do not link to this library, WiFi will not be started.
282-
* */
283-
#include "nvs_flash.h"
284-
285-
extern "C" void initWiFi()
286-
{
287-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
288-
nvs_flash_init();
289-
tcpip_adapter_init();
290-
esp_event_loop_init(WiFiGenericClass::_eventCallback, NULL);
291-
esp_wifi_init(&cfg);
292-
}
293-
294-
void startWiFi()
295-
{
296-
esp_err_t err;
297-
298-
err = esp_wifi_start();
299-
if (err != ESP_OK) {
300-
log_e("esp_wifi_start: %d", err);
301-
return;
302-
}
303-
#if CONFIG_AUTOCONNECT_WIFI
304-
wifi_mode_t mode = WIFI_MODE_NULL;
305-
bool auto_connect = false;
306-
307-
err = esp_wifi_get_mode(&mode);
308-
if (err != ESP_OK) {
309-
log_e("esp_wifi_get_mode: %d", err);
310-
return;
311-
}
312-
313-
err = esp_wifi_get_auto_connect(&auto_connect);
314-
if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) {
315-
err = esp_wifi_connect();
316-
if (err != ESP_OK) {
317-
log_e("esp_wifi_connect: %d", err);
318-
}
319-
}
320-
#endif
321-
}
322-

0 commit comments

Comments
 (0)