diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 33efe594c..d7bb99f63 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -219,6 +219,7 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); void attachInterrupt(uint8_t pin, void (*)(void), int mode); void detachInterrupt(uint8_t pin); +void preinit(void); void setup(void); void loop(void); diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index cec390d55..a35e3d5a8 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -63,7 +63,7 @@ static uint32_t s_micros_at_task_start; extern "C" { extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER; -const char* core_release = +const char* core_release = #ifdef ARDUINO_ESP8266_RELEASE ARDUINO_ESP8266_RELEASE; #else @@ -243,18 +243,26 @@ extern "C" void ICACHE_RAM_ATTR app_entry (void) return app_entry_custom(); } +extern "C" void preinit (void) __attribute__((weak)); +extern "C" void preinit (void) +{ + /* do nothing by default */ +} + extern "C" void user_init(void) { struct rst_info *rtc_info_ptr = system_get_rst_info(); memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo)); uart_div_modify(0, UART_CLK_FREQ / (115200)); - init(); + init(); // in core_esp8266_wiring.c, inits hw regs and sdk timer initVariant(); cont_init(g_pcont); + preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable. + ets_task(loop_task, LOOP_TASK_PRIORITY, s_loop_queue, LOOP_QUEUE_SIZE); diff --git a/libraries/ESP8266WiFi/examples/EarlyDisableWiFi/EarlyDisableWiFi.ino b/libraries/ESP8266WiFi/examples/EarlyDisableWiFi/EarlyDisableWiFi.ino new file mode 100644 index 000000000..45627bdb1 --- /dev/null +++ b/libraries/ESP8266WiFi/examples/EarlyDisableWiFi/EarlyDisableWiFi.ino @@ -0,0 +1,58 @@ + +#include + +#ifndef STASSID +#define STASSID "your-ssid" +#define STAPSK "your-password" +#endif + +// preinit() is called before system startup +// from nonos-sdk's user entry point user_init() + +void preinit() { + // Global WiFi constructors are not called yet + // (global class instances like WiFi, Serial... are not yet initialized).. + // No global object methods or C++ exceptions can be called in here! + //The below is a static class method, which is similar to a function, so it's ok. + ESP8266WiFiClass::preinitWiFiOff(); +} + +void setup() { + Serial.begin(115200); + Serial.setDebugOutput(true); + Serial.println("sleeping 5s"); + + // during this period, a simple amp meter shows + // an average of 20mA with a Wemos D1 mini + // a DSO is needed to check #2111 + delay(5000); + + Serial.println("waking WiFi up, sleeping 5s"); + WiFi.forceSleepWake(); + + // amp meter raises to 75mA + delay(5000); + + Serial.println("connecting to AP " STASSID); + WiFi.mode(WIFI_STA); + WiFi.begin(STASSID, STAPSK); + + for (bool configured = false; !configured;) { + for (auto addr : addrList) + if ((configured = !addr.isLocal() && addr.ifnumber() == STATION_IF)) { + Serial.printf("STA: IF='%s' hostname='%s' addr= %s\n", + addr.ifname().c_str(), + addr.ifhostname(), + addr.toString().c_str()); + break; + } + Serial.print('.'); + delay(500); + } + + // amp meter cycles within 75-80 mA + +} + +void loop() { +} diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 2e3f4043c..a6239dece 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -579,4 +579,25 @@ void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *ca esp_schedule(); // resume the hostByName function } +//meant to be called from user-defined preinit() +void ESP8266WiFiGenericClass::preinitWiFiOff () { + // https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391 + // WiFi.persistent(false); + // WiFi.mode(WIFI_OFF); + // WiFi.forceSleepBegin(); + //WiFi.mode(WIFI_OFF) equivalent: + // datasheet: + // Set Wi-Fi working mode to Station mode, SoftAP + // or Station + SoftAP, and do not update flash + // (not persistent) + wifi_set_opmode_current(WIFI_OFF); + + //WiFi.forceSleepBegin(/*default*/0) equivalent: + // sleep forever until wifi_fpm_do_wakeup() is called + wifi_fpm_set_sleep_type(MODEM_SLEEP_T); + wifi_fpm_open(); + wifi_fpm_do_sleep(0xFFFFFFF); + + // use WiFi.forceSleepWake() to wake WiFi up +} diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h index 5fba065f5..af3fce3e1 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -88,6 +88,8 @@ class ESP8266WiFiGenericClass { bool forceSleepBegin(uint32 sleepUs = 0); bool forceSleepWake(); + static void preinitWiFiOff (); //meant to be called in user-defined preinit() + protected: static bool _persistent; static WiFiMode_t _forceSleepLastMode;