Skip to content

Commit 1b1b0a2

Browse files
Add stack repainting call to ESP class (#5221)
Allow the unused stack to be reset to the check value at any time in the application, allowing for delta-stack calculations to be done. Add ESP.resetFreeContStack() class method for general use. Add in some dumping in the BearSSL_Validation example to show the usage for those that care.
1 parent d17ffc2 commit 1b1b0a2

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

Diff for: cores/esp8266/Esp.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ uint32_t EspClass::getFreeContStack()
183183
return cont_get_free_stack(g_pcont);
184184
}
185185

186+
void EspClass::resetFreeContStack()
187+
{
188+
cont_repaint_stack(g_pcont);
189+
}
190+
186191
uint32_t EspClass::getChipId(void)
187192
{
188193
return system_get_chip_id();

Diff for: cores/esp8266/Esp.h

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class EspClass {
111111
void getHeapStats(uint32_t* free = nullptr, uint16_t* max = nullptr, uint8_t* frag = nullptr);
112112

113113
uint32_t getFreeContStack();
114+
void resetFreeContStack();
114115

115116
const char * getSdkVersion();
116117
String getCoreVersion();

Diff for: cores/esp8266/cont.h

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ int cont_get_free_stack(cont_t* cont);
7474
// continuation stack
7575
bool cont_can_yield(cont_t* cont);
7676

77+
// Repaint the stack from the current SP to the end, to allow individual
78+
// routines' stack usages to be calculated by re-painting, checking current
79+
// free, running the routine, then checking the max free
80+
void cont_repaint_stack(cont_t *cont);
81+
82+
7783
#ifdef __cplusplus
7884
}
7985
#endif

Diff for: cores/esp8266/cont_util.c

+15
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@ bool ICACHE_RAM_ATTR cont_can_yield(cont_t* cont) {
6464
return !ETS_INTR_WITHINISR() &&
6565
cont->pc_ret != 0 && cont->pc_yield == 0;
6666
}
67+
68+
// No need for this to be in IRAM, not expected to be IRQ called
69+
void cont_repaint_stack(cont_t *cont)
70+
{
71+
register uint32_t *sp asm("a1");
72+
// Ensure 64 bytes adjacent to the current SP don't get touched to endure
73+
// we don't accidentally trounce over locals or IRQ temps.
74+
uint32_t sp_safe = CONT_STACKSIZE/4 - ((sp - &cont->stack[0] - 64)/4);
75+
76+
// Fill stack with magic values
77+
for(uint32_t pos = 0; pos < sp_safe; pos++)
78+
{
79+
cont->stack[pos] = CONT_STACKGUARD;
80+
}
81+
}

Diff for: libraries/ESP8266WiFi/examples/BearSSL_Validation/BearSSL_Validation.ino

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_
3838
path = "/";
3939
}
4040

41+
ESP.resetFreeContStack();
42+
uint32_t freeStackStart = ESP.getFreeContStack();
4143
Serial.printf("Trying: %s:443...", host);
4244
client->connect(host, port);
4345
if (!client->connected()) {
@@ -72,7 +74,8 @@ void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_
7274
} while (millis() < to);
7375
}
7476
client->stop();
75-
Serial.printf("\n-------\n\n");
77+
uint32_t freeStackEnd = ESP.getFreeContStack();
78+
Serial.printf("\nCONT stack used: %d\n-------\n\n", freeStackStart - freeStackEnd);
7679
}
7780

7881
void fetchNoConfig() {

0 commit comments

Comments
 (0)