Skip to content

Commit 1f88f28

Browse files
committed
Automatic stack location selection (SYS or HEAP), remove unnecessary board generator option
1 parent f776454 commit 1f88f28

File tree

12 files changed

+206
-136
lines changed

12 files changed

+206
-136
lines changed

cores/esp8266/cont.h

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ typedef struct cont_ {
4545
unsigned* struct_start;
4646
} cont_t;
4747

48+
/* Not static, used in core_esp8266_postmortem.c.
49+
* Placed into noinit section because we assign value to this variable
50+
* before .bss is zero-filled, and need to preserve the value.
51+
*/
52+
extern cont_t* g_pcont __attribute__((section(".noinit")));
53+
4854
// Initialize the cont_t structure before calling cont_run
4955
void cont_init(cont_t*);
5056

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This is the original app_entry() not providing extra 4K heap, but allowing
3+
* the use of WPS.
4+
*
5+
* see comments in core_esp8266_main.cpp's app_entry()
6+
*
7+
*/
8+
9+
#include <c_types.h>
10+
#include "cont.h"
11+
#include "coredecls.h"
12+
13+
// calls to this function must *always* be inlined
14+
void disable_extra4k_at_link_time (void)
15+
{
16+
/*
17+
* does nothing
18+
* allows overriding the core_esp8266_main.cpp's app_entry()
19+
* by this one below, at link time
20+
*
21+
*/
22+
}
23+
24+
/* the following code is linked only if a call to the above function is made somewhere */
25+
26+
extern "C" void call_user_start();
27+
28+
/* this is the default NONOS-SDK user's heap location */
29+
static cont_t g_cont __attribute__ ((aligned (16)));
30+
31+
extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void)
32+
{
33+
/* this is the default NONOS-SDK user's heap location */
34+
g_pcont = &g_cont;
35+
36+
/* Call the entry point of the SDK code. */
37+
call_user_start();
38+
}

cores/esp8266/core_esp8266_main.cpp

+18-19
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,15 @@ void init_done() {
175175
176176
WPS beeing flawed by its poor security, or not beeing used by lots of
177177
users, it has been decided that we are still going to use that memory for
178-
user's stack and disable the use of WPS, with an option to revert that
179-
back at the user's discretion. This selection can be done with the
180-
global define NO_EXTRA_4K_HEAP. An option has been added to the board
181-
generator script.
178+
user's stack and disable the use of WPS.
179+
180+
app_entry() jumps to app_entry_custom() defined as "weakref" calling
181+
itself a weak customizable function, allowing to use another one when
182+
this is required (see core_esp8266_app_entry_noextra4k.cpp, used by WPS).
183+
184+
(note: setting app_entry() itself as "weak" is not sufficient and always
185+
ends up with the other "noextra4k" one linked, maybe because it has a
186+
default value in linker scripts).
182187
183188
References:
184189
https://github.com/esp8266/Arduino/pull/4553
@@ -188,31 +193,25 @@ void init_done() {
188193
189194
*/
190195

191-
#ifdef NO_EXTRA_4K_HEAP
192-
/* this is the default NONOS-SDK user's heap location */
193-
cont_t g_cont __attribute__ ((aligned (16)));
194-
#endif
195-
196-
extern "C" void ICACHE_RAM_ATTR app_entry(void)
196+
extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void) __attribute__((weak));
197+
extern "C" void ICACHE_RAM_ATTR app_entry_redefinable(void)
197198
{
198-
#ifdef NO_EXTRA_4K_HEAP
199-
200-
/* this is the default NONOS-SDK user's heap location */
201-
g_pcont = &g_cont;
202-
203-
#else
204-
205199
/* Allocate continuation context on this SYS stack,
206200
and save pointer to it. */
207201
cont_t s_cont __attribute__((aligned(16)));
208202
g_pcont = &s_cont;
209203

210-
#endif
211-
212204
/* Call the entry point of the SDK code. */
213205
call_user_start();
214206
}
215207

208+
static void ICACHE_RAM_ATTR app_entry_custom (void) __attribute__((weakref("app_entry_redefinable")));
209+
210+
extern "C" void ICACHE_RAM_ATTR app_entry (void)
211+
{
212+
return app_entry_custom();
213+
}
214+
216215
extern "C" void user_init(void) {
217216
struct rst_info *rtc_info_ptr = system_get_rst_info();
218217
memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo));

cores/esp8266/coredecls.h

+6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ extern "C" {
88

99
// TODO: put declarations here, get rid of -Wno-implicit-function-declaration
1010

11+
1112
extern bool timeshift64_is_set;
1213

14+
void esp_yield();
15+
void esp_schedule();
1316
void tune_timeshift64 (uint64_t now_us);
1417
void settimeofday_cb (void (*cb)(void));
1518

19+
// calls to this function must *always* be inlined
20+
void disable_extra4k_at_link_time (void) __attribute__((noinline));
21+
1622
#ifdef __cplusplus
1723
}
1824
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
#include "ESP8266WiFi.h"
3+
#include "ESP8266WiFiGeneric.h"
4+
#include "ESP8266WiFiSTA.h"
5+
6+
void wifi_wps_status_cb(wps_cb_status status);
7+
8+
/**
9+
* WPS config
10+
* so far only WPS_TYPE_PBC is supported (SDK 1.2.0)
11+
* @return ok
12+
*/
13+
bool beginWPSConfig(void) {
14+
15+
if(!WiFi.enableSTA(true)) {
16+
// enable STA failed
17+
return false;
18+
}
19+
20+
WiFi.disconnect();
21+
22+
DEBUGV("wps begin\n");
23+
24+
if(!wifi_wps_disable()) {
25+
DEBUGV("wps disable failed\n");
26+
return false;
27+
}
28+
29+
// so far only WPS_TYPE_PBC is supported (SDK 1.2.0)
30+
if(!wifi_wps_enable(WPS_TYPE_PBC)) {
31+
DEBUGV("wps enable failed\n");
32+
return false;
33+
}
34+
35+
if(!wifi_set_wps_cb((wps_st_cb_t) &wifi_wps_status_cb)) {
36+
DEBUGV("wps cb failed\n");
37+
return false;
38+
}
39+
40+
if(!wifi_wps_start()) {
41+
DEBUGV("wps start failed\n");
42+
return false;
43+
}
44+
45+
esp_yield();
46+
// will return here when wifi_wps_status_cb fires
47+
48+
return true;
49+
}
50+
51+
/**
52+
* WPS callback
53+
* @param status wps_cb_status
54+
*/
55+
void wifi_wps_status_cb(wps_cb_status status) {
56+
DEBUGV("wps cb status: %d\r\n", status);
57+
switch(status) {
58+
case WPS_CB_ST_SUCCESS:
59+
if(!wifi_wps_disable()) {
60+
DEBUGV("wps disable failed\n");
61+
}
62+
wifi_station_connect();
63+
break;
64+
case WPS_CB_ST_FAILED:
65+
DEBUGV("wps FAILED\n");
66+
break;
67+
case WPS_CB_ST_TIMEOUT:
68+
DEBUGV("wps TIMEOUT\n");
69+
break;
70+
case WPS_CB_ST_WEP:
71+
DEBUGV("wps WEP\n");
72+
break;
73+
case WPS_CB_ST_UNK:
74+
DEBUGV("wps UNKNOWN\n");
75+
if(!wifi_wps_disable()) {
76+
DEBUGV("wps disable failed\n");
77+
}
78+
break;
79+
}
80+
// TODO user function to get status
81+
82+
esp_schedule(); // resume the beginWPSConfig function
83+
}
84+

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp

-84
Original file line numberDiff line numberDiff line change
@@ -571,90 +571,6 @@ int32_t ESP8266WiFiSTAClass::RSSI(void) {
571571
// -------------------------------------------------- STA remote configure -----------------------------------------------
572572
// -----------------------------------------------------------------------------------------------------------------------
573573

574-
#ifdef NO_EXTRA_4K_HEAP
575-
/* NO_EXTRA_4K_HEAP's description in cores/esp8266/core_esp8266_main.cpp */
576-
577-
void wifi_wps_status_cb(wps_cb_status status);
578-
579-
/**
580-
* WPS config
581-
* so far only WPS_TYPE_PBC is supported (SDK 1.2.0)
582-
* @return ok
583-
*/
584-
bool ESP8266WiFiSTAClass::beginWPSConfig(void) {
585-
586-
if(!WiFi.enableSTA(true)) {
587-
// enable STA failed
588-
return false;
589-
}
590-
591-
disconnect();
592-
593-
DEBUGV("wps begin\n");
594-
595-
if(!wifi_wps_disable()) {
596-
DEBUGV("wps disable failed\n");
597-
return false;
598-
}
599-
600-
// so far only WPS_TYPE_PBC is supported (SDK 1.2.0)
601-
if(!wifi_wps_enable(WPS_TYPE_PBC)) {
602-
DEBUGV("wps enable failed\n");
603-
return false;
604-
}
605-
606-
if(!wifi_set_wps_cb((wps_st_cb_t) &wifi_wps_status_cb)) {
607-
DEBUGV("wps cb failed\n");
608-
return false;
609-
}
610-
611-
if(!wifi_wps_start()) {
612-
DEBUGV("wps start failed\n");
613-
return false;
614-
}
615-
616-
esp_yield();
617-
// will return here when wifi_wps_status_cb fires
618-
619-
return true;
620-
}
621-
622-
/**
623-
* WPS callback
624-
* @param status wps_cb_status
625-
*/
626-
void wifi_wps_status_cb(wps_cb_status status) {
627-
DEBUGV("wps cb status: %d\r\n", status);
628-
switch(status) {
629-
case WPS_CB_ST_SUCCESS:
630-
if(!wifi_wps_disable()) {
631-
DEBUGV("wps disable failed\n");
632-
}
633-
wifi_station_connect();
634-
break;
635-
case WPS_CB_ST_FAILED:
636-
DEBUGV("wps FAILED\n");
637-
break;
638-
case WPS_CB_ST_TIMEOUT:
639-
DEBUGV("wps TIMEOUT\n");
640-
break;
641-
case WPS_CB_ST_WEP:
642-
DEBUGV("wps WEP\n");
643-
break;
644-
case WPS_CB_ST_UNK:
645-
DEBUGV("wps UNKNOWN\n");
646-
if(!wifi_wps_disable()) {
647-
DEBUGV("wps disable failed\n");
648-
}
649-
break;
650-
}
651-
// TODO user function to get status
652-
653-
esp_schedule(); // resume the beginWPSConfig function
654-
}
655-
656-
#endif // NO_EXTRA_4K_HEAP
657-
658574
bool ESP8266WiFiSTAClass::_smartConfigStarted = false;
659575
bool ESP8266WiFiSTAClass::_smartConfigDone = false;
660576

libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#include "ESP8266WiFiType.h"
2828
#include "ESP8266WiFiGeneric.h"
2929
#include "user_interface.h"
30+
#include "coredecls.h" // disable_extra4k_at_link_time()
3031

32+
extern bool beginWPSConfig(void);
3133

3234
class ESP8266WiFiSTAClass {
3335
// ----------------------------------------------------------------------------------------------
@@ -93,14 +95,12 @@ class ESP8266WiFiSTAClass {
9395

9496
public:
9597

96-
#ifdef NO_EXTRA_4K_HEAP
97-
bool beginWPSConfig(void);
98-
#else
99-
inline bool beginWPSConfig(void) __attribute__((always_inline)) {
100-
return WPS_is_unavailable_in_this_configuration__Please_check_FAQ_or_board_generator_tool();
98+
inline bool beginWPSConfig(void) __attribute__((always_inline))
99+
{
100+
disable_extra4k_at_link_time(); // this call must always be inlined
101+
return ::beginWPSConfig();
101102
}
102-
#endif
103-
103+
104104
bool beginSmartConfig();
105105
bool stopSmartConfig();
106106
bool smartConfigDone();

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1260,13 +1260,13 @@ bool WiFiClientSecure::loadPrivateKey(Stream& stream, size_t size) {
12601260

12611261
extern "C" {
12621262
#include <cont.h>
1263-
extern cont_t g_cont;
1263+
extern cont_t *g_pcont;
12641264
extern size_t br_esp8266_stack_proxy_usage();
12651265

12661266
void _BearSSLCheckStack(const char *fcn, const char *file, int line) {
12671267
static int cnt = 0;
12681268
register uint32_t *sp asm("a1");
1269-
int freestack = 4 * (sp - g_cont.stack);
1269+
int freestack = 4 * (sp - g_pcont->stack);
12701270
int freeheap = ESP.getFreeHeap();
12711271
static int laststack, lastheap, laststack2;
12721272
if ((laststack != freestack) || (lastheap != freeheap) || (laststack2 != (int)br_esp8266_stack_proxy_usage())) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
/*
3+
This is a debugging / validation test only
4+
5+
It demonstrates the effect of calling
6+
disable_extra4k_at_link_time()
7+
8+
Currently only WPS is using that call
9+
10+
released to public domain
11+
*/
12+
13+
#include <ESP8266WiFi.h>
14+
#include <cont.h>
15+
16+
#define USE_WPS 0 // try me with 0 or 1
17+
18+
extern cont_t* g_pcont;
19+
20+
void setup() {
21+
22+
Serial.begin(115200);
23+
Serial.setDebugOutput(true);
24+
WiFi.mode(WIFI_STA);
25+
bool sysstack = (((unsigned long)g_pcont) >> 16) == 0x3fff;
26+
Serial.printf("\nUsing sys stack: %s\ncont: %p (USER:0x3ffe SYS:0x3fff)\n"
27+
"check https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map)\n",
28+
sysstack ? "YES" : "NO",
29+
g_pcont);
30+
31+
Serial.printf("FreeHeap: %d (~52112 without WPS, ~46832 with)\n", ESP.getFreeHeap());
32+
33+
#if USE_WPS
34+
Serial.printf("starting WPS...\n");
35+
Serial.printf("wps: %d\n", WiFi.beginWPSConfig());
36+
#else
37+
Serial.printf("done\n");
38+
#endif
39+
}
40+
41+
void loop() {
42+
}

0 commit comments

Comments
 (0)