Skip to content

Commit 216680b

Browse files
d-a-vearlephilhower
authored andcommitted
weak hook preinit() #2111 #2133 #2136 (#5395)
* weak hook early_setup() #2111 #2133 #2136 * rename to early_init (more "c" vs early_setup which is more "c++arduino") * example * improve earlyWiFi example, slightly change AddrList interface, move WiFi sketches into WiFi examples * fix CI * fix local CI runner * fix local CI runner * rename early_init() to preinit() * + static ESP8266WiFiClass::preinit_wifi_off() * update early disable wifi example * example update * IPv6 example update * Update ESP8266WiFiGeneric.h camelCase for static method name * Update ESP8266WiFiGeneric.cpp camelCase for static method name * Update EarlyDisableWiFi.ino Expand comment, fix static method name * Update core_esp8266_main.cpp Expanded comment. * Update core_esp8266_main.cpp Expanded comment * Update EarlyDisableWiFi.ino Expanded comment
1 parent 2ec3daa commit 216680b

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

cores/esp8266/Arduino.h

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
219219
void attachInterrupt(uint8_t pin, void (*)(void), int mode);
220220
void detachInterrupt(uint8_t pin);
221221

222+
void preinit(void);
222223
void setup(void);
223224
void loop(void);
224225

cores/esp8266/core_esp8266_main.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static uint32_t s_micros_at_task_start;
6363

6464
extern "C" {
6565
extern const uint32_t __attribute__((section(".ver_number"))) core_version = ARDUINO_ESP8266_GIT_VER;
66-
const char* core_release =
66+
const char* core_release =
6767
#ifdef ARDUINO_ESP8266_RELEASE
6868
ARDUINO_ESP8266_RELEASE;
6969
#else
@@ -243,18 +243,26 @@ extern "C" void ICACHE_RAM_ATTR app_entry (void)
243243
return app_entry_custom();
244244
}
245245

246+
extern "C" void preinit (void) __attribute__((weak));
247+
extern "C" void preinit (void)
248+
{
249+
/* do nothing by default */
250+
}
251+
246252
extern "C" void user_init(void) {
247253
struct rst_info *rtc_info_ptr = system_get_rst_info();
248254
memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo));
249255

250256
uart_div_modify(0, UART_CLK_FREQ / (115200));
251257

252-
init();
258+
init(); // in core_esp8266_wiring.c, inits hw regs and sdk timer
253259

254260
initVariant();
255261

256262
cont_init(g_pcont);
257263

264+
preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable.
265+
258266
ets_task(loop_task,
259267
LOOP_TASK_PRIORITY, s_loop_queue,
260268
LOOP_QUEUE_SIZE);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#include <ESP8266WiFi.h>
3+
4+
#ifndef STASSID
5+
#define STASSID "your-ssid"
6+
#define STAPSK "your-password"
7+
#endif
8+
9+
// preinit() is called before system startup
10+
// from nonos-sdk's user entry point user_init()
11+
12+
void preinit() {
13+
// Global WiFi constructors are not called yet
14+
// (global class instances like WiFi, Serial... are not yet initialized)..
15+
// No global object methods or C++ exceptions can be called in here!
16+
//The below is a static class method, which is similar to a function, so it's ok.
17+
ESP8266WiFiClass::preinitWiFiOff();
18+
}
19+
20+
void setup() {
21+
Serial.begin(115200);
22+
Serial.setDebugOutput(true);
23+
Serial.println("sleeping 5s");
24+
25+
// during this period, a simple amp meter shows
26+
// an average of 20mA with a Wemos D1 mini
27+
// a DSO is needed to check #2111
28+
delay(5000);
29+
30+
Serial.println("waking WiFi up, sleeping 5s");
31+
WiFi.forceSleepWake();
32+
33+
// amp meter raises to 75mA
34+
delay(5000);
35+
36+
Serial.println("connecting to AP " STASSID);
37+
WiFi.mode(WIFI_STA);
38+
WiFi.begin(STASSID, STAPSK);
39+
40+
for (bool configured = false; !configured;) {
41+
for (auto addr : addrList)
42+
if ((configured = !addr.isLocal() && addr.ifnumber() == STATION_IF)) {
43+
Serial.printf("STA: IF='%s' hostname='%s' addr= %s\n",
44+
addr.ifname().c_str(),
45+
addr.ifhostname(),
46+
addr.toString().c_str());
47+
break;
48+
}
49+
Serial.print('.');
50+
delay(500);
51+
}
52+
53+
// amp meter cycles within 75-80 mA
54+
55+
}
56+
57+
void loop() {
58+
}

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,25 @@ void wifi_dns_found_callback(const char *name, CONST ip_addr_t *ipaddr, void *ca
579579
esp_schedule(); // resume the hostByName function
580580
}
581581

582-
582+
//meant to be called from user-defined preinit()
583+
void ESP8266WiFiGenericClass::preinitWiFiOff () {
584+
// https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391
585+
// WiFi.persistent(false);
586+
// WiFi.mode(WIFI_OFF);
587+
// WiFi.forceSleepBegin();
588+
589+
//WiFi.mode(WIFI_OFF) equivalent:
590+
// datasheet:
591+
// Set Wi-Fi working mode to Station mode, SoftAP
592+
// or Station + SoftAP, and do not update flash
593+
// (not persistent)
594+
wifi_set_opmode_current(WIFI_OFF);
595+
596+
//WiFi.forceSleepBegin(/*default*/0) equivalent:
597+
// sleep forever until wifi_fpm_do_wakeup() is called
598+
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
599+
wifi_fpm_open();
600+
wifi_fpm_do_sleep(0xFFFFFFF);
601+
602+
// use WiFi.forceSleepWake() to wake WiFi up
603+
}

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class ESP8266WiFiGenericClass {
8888
bool forceSleepBegin(uint32 sleepUs = 0);
8989
bool forceSleepWake();
9090

91+
static void preinitWiFiOff (); //meant to be called in user-defined preinit()
92+
9193
protected:
9294
static bool _persistent;
9395
static WiFiMode_t _forceSleepLastMode;

0 commit comments

Comments
 (0)