Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Disable WiFi at boot by default #7902

Merged
merged 29 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ab16b8c
Disable WiFi at boot by default
d-a-v Mar 2, 2021
6310da8
+define WIFI_IS_OFF_AT_BOOT
d-a-v Mar 2, 2021
330b229
remove now useless example
d-a-v Mar 3, 2021
68d338e
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 3, 2021
253bbc5
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 3, 2021
5ead6a2
Merge branch 'WiFiOffAtBoot' of github.com:d-a-v/Arduino into WiFiOff…
d-a-v Mar 3, 2021
b2a53b7
mv enableWiFiAtBootTime() to core_esp8266_features.h
d-a-v Mar 3, 2021
7756427
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 15, 2021
64ff4b0
Merge branch 'WiFiOffAtBoot' of github.com:d-a-v/Arduino into WiFiOff…
d-a-v Mar 15, 2021
2d4ca1d
sync with master
d-a-v Mar 15, 2021
8510670
Merge branch 'master' into WiFiOffAtBoot
earlephilhower Mar 15, 2021
1059beb
Merge branch 'master' into WiFiOffAtBoot
earlephilhower Mar 16, 2021
7fd4116
per @earlephilhower review: a file was missing
d-a-v Mar 16, 2021
c15cb5d
doc
d-a-v Mar 16, 2021
9d7eb2e
WiFi persistence is now false by default
d-a-v Mar 16, 2021
227e557
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 16, 2021
eca1367
fix doc
d-a-v Mar 16, 2021
8b0d641
Merge branch 'WiFiOffAtBoot' of github.com:d-a-v/Arduino into WiFiOff…
d-a-v Mar 16, 2021
6d9ec26
ditto
d-a-v Mar 16, 2021
8ecc7f0
doc: remove sphinx warnings (fix links and formatting)
d-a-v Mar 16, 2021
8332c42
fix link name
d-a-v Mar 16, 2021
df8f8a7
fix doc
d-a-v Mar 18, 2021
20020c0
legacy: restore persistence
d-a-v Mar 18, 2021
4e5abc6
Merge branch 'master' into WiFiOffAtBoot
d-a-v Mar 31, 2021
2eb8bb1
undeprecate preinit()
d-a-v Mar 31, 2021
da5966b
move force modem up to when mode has changed (per @mcspr review)
d-a-v Mar 31, 2021
f0f1f55
do not wake up from sleep when mode if OFF
d-a-v Mar 31, 2021
77087f0
Merge branch 'master' into WiFiOffAtBoot
d-a-v Apr 9, 2021
5d12aa2
fix doc per review
d-a-v Apr 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cores/esp8266/core_esp8266_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define CORE_HAS_UMM

#define WIFI_HAS_EVENT_CALLBACK
#define WIFI_IS_OFF_AT_BOOT

#include <stdlib.h> // malloc()
#include <stddef.h> // size_t
Expand Down Expand Up @@ -104,6 +105,8 @@ uint64_t micros64(void);
void delay(unsigned long);
void delayMicroseconds(unsigned int us);

void enableWiFiAtBootTime (void) __attribute__((noinline));

#if defined(F_CPU) || defined(CORE_MOCK)
#ifdef __cplusplus
constexpr
Expand Down
16 changes: 14 additions & 2 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,18 @@ extern "C" void app_entry (void)
extern "C" void preinit (void) __attribute__((weak));
extern "C" void preinit (void)
{
/* do nothing by default */
/* does nothing, kept for backward compatibility */
}

extern "C" void __disableWiFiAtBootTime (void) __attribute__((weak));
extern "C" void __disableWiFiAtBootTime (void)
{
// Starting from arduino core v3: wifi is disabled at boot time
// WiFi.begin() or WiFi.softAP() will wake WiFi up
wifi_set_opmode_current(0/*WIFI_OFF*/);
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(0xFFFFFFF);
}

extern "C" void user_init(void) {
Expand Down Expand Up @@ -360,7 +371,8 @@ extern "C" void user_init(void) {
#if defined(MMU_IRAM_HEAP)
umm_init_iram();
#endif
preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable.
preinit(); // deprecated
__disableWiFiAtBootTime(); // default weak function disables WiFi
d-a-v marked this conversation as resolved.
Show resolved Hide resolved

ets_task(loop_task,
LOOP_TASK_PRIORITY, s_loop_queue,
Expand Down
3 changes: 2 additions & 1 deletion cores/esp8266/coredecls.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void esp_schedule();
void tune_timeshift64 (uint64_t now_us);
void disable_extra4k_at_link_time (void) __attribute__((noinline));
bool sntp_set_timezone_in_seconds(int32_t timezone);
void __disableWiFiAtBootTime (void) __attribute__((noinline));
void __real_system_restart_local() __attribute__((noreturn));

uint32_t sqrt32 (uint32_t n);
Expand All @@ -34,6 +35,6 @@ using TrivialCB = std::function<void()>;
void settimeofday_cb (const BoolCB& cb);
void settimeofday_cb (const TrivialCB& cb);

#endif
#endif // __cplusplus

#endif // __COREDECLS_H
20 changes: 20 additions & 0 deletions cores/esp8266/enable_wifi_at_boot_time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* empty wrappers to play with linker and reenable wifi at boot time
*/

#include "coredecls.h"

extern "C" void enableWiFiAtBootTime()
{
/*
* Called by user from anywhere, does nothing and allows overriding
* the core_esp8266_main.cpp's default disableWiFiAtBootTime() by the
* one below, at link time.
*/
}

extern "C" void __disableWiFiAtBootTime()
{
// overrides the default __disableWiFiAtBootTime:
// Does nothing: WiFi is enabled by default in nonos-sdk
}

This file was deleted.

5 changes: 0 additions & 5 deletions libraries/ESP8266WiFi/examples/WiFiShutdown/WiFiShutdown.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ WiFiState state;
const char* ssid = STASSID;
const char* password = STAPSK;

void preinit(void) {
// Make sure, wifi stays off after boot.
ESP8266WiFiClass::preinitWiFiOff();
}

void setup() {
Serial.begin(74880);
//Serial.setDebugOutput(true); // If you need debug output
Expand Down
24 changes: 3 additions & 21 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m, WiFiState* state) {
}

if (wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
// wifi may have been put asleep by ESP8266WiFiGenericClass::preinitWiFiOff
// wifi starts asleep by default
Copy link
Collaborator

@mcspr mcspr Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at esp32's ::mode:
https://github.com/espressif/arduino-esp32/blob/46d5afb17fb91965632dc5fef237117e1fe947fc/libraries/WiFi/src/WiFiGeneric.cpp#L532-L542

Should this be more in-line with that?
i.e.

  • remove this forced modem up, it happens even if we don't change the mode
  • getMode() checks for fpm -> returns null-mode when sleeping
  • mode() consecutively turns off the wifi when WIFI_OFF without an additional call to force sleep and turns it back on as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks !

I added 1) and 3).
I'm reticent about 2) because WiFi.mode(WIFI_OFF) will not put the modem to sleep, or puting modem to sleep does not necessarily switches the mode to WIFI_OFF. 2) should come with that no ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant exactly that, to do forcesleep with WiFi.mode(WIFI_OFF) and resume with any real mode.
Not noticed the special shutdown and resume modes though, I wonder if those should be separate methods instead and not a 'mode'?

Copy link
Collaborator Author

@d-a-v d-a-v Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are indeed "fake" modes and described as such somewhere. There are two examples making use of them.
They are designed to import the deep sleep procedure into the core and allow users to not use random fw-snippets to put the chip to sleep (and wake-up fast)

Copy link
Collaborator

@mcspr mcspr Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah, both are already implemented as methods. So examples could use those directly?

wifi_fpm_do_wakeup();
wifi_fpm_close();
}
Expand Down Expand Up @@ -855,25 +855,7 @@ bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState* state)
return true;
}

//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
// It was meant to be called from user-defined ::preinit()
// It is now deprecated by enableWiFiAtBootTime() and __disableWiFiAtBootTime()
}
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ESP8266WiFiGenericClass {

static uint32_t shutdownCRC (const WiFiState* state);
static bool shutdownValidCRC (const WiFiState* state);
static void preinitWiFiOff (); //meant to be called in user-defined preinit()
static void preinitWiFiOff () __attribute__((deprecated("WiFi is off by default at boot, use enableWiFiAtBoot() for legacy behavior")));

protected:
static bool _persistent;
Expand Down
6 changes: 0 additions & 6 deletions libraries/esp8266/examples/SerialStress/SerialStress.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ void error(const char* what) {
}
}

void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}

void setup() {
pinMode(LED_BUILTIN, OUTPUT);

Expand Down
6 changes: 0 additions & 6 deletions libraries/esp8266/examples/UartDownload/UartDownload.ino
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ void proxyEspSync() {
//
////////////////////////////////////////////////////////////////////////////////

void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}

void setup() {
// For `proxyEspSync()` to work, the Serial.begin() speed needs to be
// 115200bps. This is the data rate used by esptool.py. It expects the Boot
Expand Down
7 changes: 0 additions & 7 deletions libraries/lwIP_w5500/examples/TCPClient/TCPClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//or #include <ENC28J60lwIP.h>

#include <WiFiClient.h> // WiFiClient (-> TCPClient)
#include <ESP8266WiFi.h> // ESP8266WiFiClass::preinitWiFiOff()

const char* host = "djxmmx.net";
const uint16_t port = 17;
Expand All @@ -19,12 +18,6 @@ using TCPClient = WiFiClient;
#define CSPIN 16 // wemos/lolin/nodemcu D0
Wiznet5500lwIP eth(CSPIN);

void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}

void setup() {
Serial.begin(115200);

Expand Down
6 changes: 0 additions & 6 deletions tests/device/test_serial/test_serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ static uint64_t in_total = 0, in_prev = 0;
static uint64_t start_ms, last_ms;
static uint64_t timeout;

void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}

void setup()
{
Serial.begin(SSBAUD);
Expand Down
6 changes: 0 additions & 6 deletions tests/device/test_spi_flash/test_spi_flash.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@

BS_ENV_DECLARE();

void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}

void setup()
{
Serial.begin(115200);
Expand Down