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
This commit is contained in:
me-no-dev 2016-11-28 00:50:21 +02:00
parent f4c2135a30
commit 1d1aeecde2
2 changed files with 69 additions and 50 deletions

View File

@ -8,15 +8,11 @@ extern "C" void initArduino();
extern void loop(); extern void loop();
extern void setup(); extern void setup();
void startWiFi() __attribute__((weak));
void startWiFi() {}
void loopTask(void *pvParameters) void loopTask(void *pvParameters)
{ {
bool setup_done = false; bool setup_done = false;
for(;;) { for(;;) {
if(!setup_done) { if(!setup_done) {
startWiFi();
setup(); setup();
setup_done = true; setup_done = true;
} }

View File

@ -40,14 +40,75 @@ extern "C" {
#include "lwip/opt.h" #include "lwip/opt.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/dns.h" #include "lwip/dns.h"
#include "esp_ipc.h"
#include "esp32-hal-log.h" #include "esp32-hal-log.h"
/**
* Boot and start WiFi
* This method get's called on boot if you use any of the WiFi methods.
* If you do not link to this library, WiFi will not be started.
* */
static bool _esp_wifi_initalized = false;
extern void initWiFi()
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
tcpip_adapter_init();
esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL);
esp_wifi_init(&cfg);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
_esp_wifi_initalized = true;
} }
} //extern "C"
#undef min #undef min
#undef max #undef max
#include <vector> #include <vector>
static bool _esp_wifi_start()
{
static bool started = false;
esp_err_t err;
if(!_esp_wifi_initalized){
initWiFi();
if(!_esp_wifi_initalized){
log_w("not initialized");
return false;
}
}
if(started){
return true;
}
started = true;
err = esp_wifi_start();
if (err != ESP_OK) {
log_e("%d", err);
return false;
}
#if CONFIG_AUTOCONNECT_WIFI
wifi_mode_t mode = WIFI_MODE_NULL;
bool auto_connect = false;
err = esp_wifi_get_mode(&mode);
if (err != ESP_OK) {
log_e("esp_wifi_get_mode: %d", err);
return false;
}
err = esp_wifi_get_auto_connect(&auto_connect);
if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) {
err = esp_wifi_connect();
if (err != ESP_OK) {
log_e("esp_wifi_connect: %d", err);
return false;
}
}
#endif
return true;
}
// ----------------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------- Generic WiFi function ----------------------------------------------- // ------------------------------------------------- Generic WiFi function -----------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------------
@ -167,7 +228,11 @@ void WiFiGenericClass::persistent(bool persistent)
*/ */
bool WiFiGenericClass::mode(wifi_mode_t m) bool WiFiGenericClass::mode(wifi_mode_t m)
{ {
if(getMode() == m) { wifi_mode_t cm = getMode();
if(cm == WIFI_MODE_MAX){
return false;
}
if(cm == m) {
return true; return true;
} }
return esp_wifi_set_mode(m) == ESP_OK; return esp_wifi_set_mode(m) == ESP_OK;
@ -180,6 +245,9 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
wifi_mode_t WiFiGenericClass::getMode() wifi_mode_t WiFiGenericClass::getMode()
{ {
uint8_t mode; uint8_t mode;
if(!_esp_wifi_start()){
return WIFI_MODE_MAX;
}
esp_wifi_get_mode((wifi_mode_t*)&mode); esp_wifi_get_mode((wifi_mode_t*)&mode);
return (wifi_mode_t)mode; return (wifi_mode_t)mode;
} }
@ -275,48 +343,3 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca
_dns_busy = false; _dns_busy = false;
} }
/**
* Boot and start WiFi
* This method get's called on boot if you use any of the WiFi methods.
* If you do not link to this library, WiFi will not be started.
* */
#include "nvs_flash.h"
extern "C" void initWiFi()
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
nvs_flash_init();
tcpip_adapter_init();
esp_event_loop_init(WiFiGenericClass::_eventCallback, NULL);
esp_wifi_init(&cfg);
}
void startWiFi()
{
esp_err_t err;
err = esp_wifi_start();
if (err != ESP_OK) {
log_e("esp_wifi_start: %d", err);
return;
}
#if CONFIG_AUTOCONNECT_WIFI
wifi_mode_t mode = WIFI_MODE_NULL;
bool auto_connect = false;
err = esp_wifi_get_mode(&mode);
if (err != ESP_OK) {
log_e("esp_wifi_get_mode: %d", err);
return;
}
err = esp_wifi_get_auto_connect(&auto_connect);
if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) {
err = esp_wifi_connect();
if (err != ESP_OK) {
log_e("esp_wifi_connect: %d", err);
}
}
#endif
}