Skip to content

Commit 542cf55

Browse files
author
Matěj Sychra
committed
Merge branch 'i2c_slave_by_bjoham' of https://github.com/thinx-cloud/Arduino into i2c_slave_by_bjoham
* 'i2c_slave_by_bjoham' of https://github.com/thinx-cloud/Arduino: Update debugging.rst (esp8266#5234) ESP8266httpClient crash-on-destructor bugfix (esp8266#5220) Add stack repainting call to ESP class (esp8266#5221)
2 parents bfcbd71 + bd4a447 commit 542cf55

File tree

8 files changed

+46
-12
lines changed

8 files changed

+46
-12
lines changed

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();

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();

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

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+
}

doc/Troubleshooting/debugging.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Debug Level
7474
All defines for the different levels starts with ``DEBUG_ESP_``
7575

7676
a full list can be found here in the
77-
`boards.txt <https://github.com/esp8266/Arduino/blob/master/boards.txt#L180>`__
77+
`boards.txt <https://github.com/esp8266/Arduino/blob/master/tools/boards.txt.py#L1045-L1047>`__
7878

7979
Example for own debug messages
8080
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ void loop() {
4141
// wait for WiFi connection
4242
if ((WiFiMulti.run() == WL_CONNECTED)) {
4343

44-
BearSSL::WiFiClientSecure client;
45-
client.setFingerprint(fingerprint);
44+
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure;
45+
46+
client->setFingerprint(fingerprint);
4647

4748
HTTPClient https;
4849

4950
Serial.print("[HTTPS] begin...\n");
50-
if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
51+
if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
5152

5253

5354
Serial.print("[HTTPS] GET...\n");
@@ -72,6 +73,8 @@ void loop() {
7273
} else {
7374
Serial.printf("[HTTPS] Unable to connect\n");
7475
}
76+
77+
delete client;
7578
}
7679

7780
delay(10000);

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ HTTPClient::HTTPClient()
123123
HTTPClient::~HTTPClient()
124124
{
125125
if(_client) {
126-
_client->stop();
126+
DEBUG_HTTPCLIENT("[HTTP-Client][~HTTPClient] end() not called before destruction of HTTPClient\n");
127127
}
128128
if(_currentHeaders) {
129129
delete[] _currentHeaders;
@@ -196,7 +196,7 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur
196196
#ifdef HTTPCLIENT_1_1_COMPATIBLE
197197
bool HTTPClient::begin(String url, String httpsFingerprint)
198198
{
199-
if(_client) _canReuse = false;
199+
_canReuse = false;
200200
end();
201201

202202
_port = 443;
@@ -214,7 +214,7 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
214214

215215
bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
216216
{
217-
if(_client) _canReuse = false;
217+
_canReuse = false;
218218
end();
219219

220220
_port = 443;
@@ -237,7 +237,7 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
237237
*/
238238
bool HTTPClient::begin(String url)
239239
{
240-
if(_client) _canReuse = false;
240+
_canReuse = false;
241241
end();
242242

243243
_port = 80;
@@ -299,7 +299,7 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
299299
#ifdef HTTPCLIENT_1_1_COMPATIBLE
300300
bool HTTPClient::begin(String host, uint16_t port, String uri)
301301
{
302-
if(_client) _canReuse = false;
302+
_canReuse = false;
303303
end();
304304

305305
clear();
@@ -325,7 +325,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, Strin
325325

326326
bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFingerprint)
327327
{
328-
if(_client) _canReuse = false;
328+
_canReuse = false;
329329
end();
330330

331331
clear();
@@ -343,7 +343,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFinge
343343

344344
bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20])
345345
{
346-
if(_client) _canReuse = false;
346+
_canReuse = false;
347347
end();
348348

349349
clear();
@@ -367,6 +367,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
367367
*/
368368
void HTTPClient::end(void)
369369
{
370+
_canReuse = false;
370371
disconnect();
371372
clear();
372373
}

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)