You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/faq/a02-my-esp-crashes.rst
+34-14Lines changed: 34 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -236,6 +236,7 @@ If you don't have any code for troubleshooting, use the example below:
236
236
237
237
void loop(){}
238
238
239
+
239
240
Enable the Out-Of-Memory (*OOM*) debug option (in the *Tools > Debug Level*
240
241
menu), compile/flash/upload this code to your ESP (Ctrl+U) and start Serial
241
242
Monitor (Ctrl+Shift+M). You should shortly see ESP restarting every couple
@@ -270,31 +271,50 @@ Decoder <https://github.com/me-no-dev/EspExceptionDecoder>`__ you can
270
271
track down where the module is crashing whenever you see the stack trace
271
272
dropped. The same procedure applies to crashes caused by exceptions.
272
273
273
-
Note: To decode the exact line of code where the application
274
+
Notes:
275
+
276
+
To decode the exact line of code where the application
274
277
crashed, you need to use ESP Exception Decoder in context of sketch
275
278
you have just loaded to the module for diagnosis. Decoder is not
276
279
able to correctly decode the stack trace dropped by some other
277
280
application not compiled and loaded from your Arduino IDE.
278
281
282
+
To improve the results of the Exception Decoder, you can add
283
+
`-fno-optimize-sibling-calls` to your build options. For details on how
284
+
to do this, review `Global Build Options <a06-global-build-options.rst>`__.
285
+
Turning off this optimization allows more caller addresses to be written
286
+
to the stack aiding in crash decoding. Because of the limited stack space,
287
+
there is the potential that removing this optimization could overflow the
288
+
stack and cause a crash.
289
+
290
+
A crash in a leaf function may not leave the caller's address on the stack.
291
+
The return address can stay in a register for the duration of the call.
292
+
Resulting in a crash report identifying the crashing function without a
293
+
trace of who called. You can encourage the compiler to save the caller's
294
+
return address by adding an inline assembly trick
295
+
`__asm__ __volatile__("" ::: "a0", "memory");` at the beginning of the
296
+
function's body. Or instead, for a debug build conditional option, use the
297
+
macro `DEBUG_LEAF_FUNCTION()` from `#include <debug.h>`.
298
+
279
299
280
300
Other Causes for Crashes
281
301
~~~~~~~~~~~~~~~~~~~~~~~~
282
302
283
303
Interrupt Service Routines
284
-
By default, all functions are compiled into flash, which means that the
285
-
cache may kick in for that code. However, the cache currently can't be used
286
-
during hardware interrupts. That means that, if you use a hardware ISR, such as
287
-
attachInterrupt(gpio, myISR, CHANGE) for a GPIO change, the ISR must have the
288
-
IRAM_ATTR attribute declared. Not only that, but the entire function tree
304
+
By default, all functions are compiled into flash, which means that the
305
+
cache may kick in for that code. However, the cache currently can't be used
306
+
during hardware interrupts. That means that, if you use a hardware ISR, such as
307
+
attachInterrupt(gpio, myISR, CHANGE) for a GPIO change, the ISR must have the
308
+
IRAM_ATTR attribute declared. Not only that, but the entire function tree
289
309
called from the ISR must also have the IRAM_ATTR declared.
290
310
Be aware that every function that has this attribute reduces available memory.
291
311
292
-
In addition, it is not possible to execute delay() or yield() from an ISR,
312
+
In addition, it is not possible to execute delay() or yield() from an ISR,
293
313
or do blocking operations, or operations that disable the interrupts, e.g.: read
294
314
a DHT.
295
315
296
316
Finally, an ISR has very high restrictions on timing for the executed code, meaning
297
-
that executed code should not take longer than a very few microseconds. It is
317
+
that executed code should not take longer than a very few microseconds. It is
298
318
considered best practice to set a flag within the ISR, and then from within the loop()
299
319
check and clear that flag, and execute code.
300
320
@@ -303,16 +323,16 @@ Asynchronous Callbacks
303
323
than ISRs, but some restrictions still apply.
304
324
It is not possible to execute delay() or yield() from an asynchronous callback.
305
325
Timing is not as tight as an ISR, but it should remain below a few milliseconds. This
306
-
is a guideline. The hard timing requirements depend on the WiFi configuration and
326
+
is a guideline. The hard timing requirements depend on the WiFi configuration and
307
327
amount of traffic. In general, the CPU must not be hogged by the user code, as the
308
328
longer it is away from servicing the WiFi stack, the more likely that memory corruption
309
329
can happen.
310
330
311
331
Memory, memory, memory
312
332
Running out of heap is the **most common cause for crashes**. Because the build process for
313
333
the ESP leaves out exceptions (they use memory), memory allocations that fail will do
314
-
so silently. A typical example is when setting or concatenating a large String. If
315
-
allocation has failed internally, then the internal string copy can corrupt data, and
334
+
so silently. A typical example is when setting or concatenating a large String. If
335
+
allocation has failed internally, then the internal string copy can corrupt data, and
316
336
the ESP will crash.
317
337
318
338
In addition, doing many String concatenations in sequence, e.g.: using operator+()
@@ -348,9 +368,9 @@ Memory, memory, memory
348
368
* If you use std libs like std::vector, make sure to call its ::reserve() method before filling it. This allows allocating only once, which reduces mem fragmentation, and makes sure that there are no empty unused slots left over in the container at the end.
349
369
350
370
Stack
351
-
The amount of stack in the ESP is tiny at only 4KB. For normal development in large systems, it
371
+
The amount of stack in the ESP is tiny at only 4KB. For normal development in large systems, it
352
372
is good practice to use and abuse the stack, because it is faster for allocation/deallocation, the scope of the object is well defined, and deallocation automatically happens in reverse order as allocation, which means no mem fragmentation. However, with the tiny amount of stack available in the ESP, that practice is not really viable, at least not for big objects.
353
-
373
+
354
374
* Large objects that have internally managed memory, such as String, std::string, std::vector, etc, are ok on the stack, because they internally allocate their buffers on the heap.
355
375
* Large arrays on the stack, such as uint8_t buffer[2048] should be avoided on the stack and should be dynamically allocated instead (consider smart pointers).
356
376
* Objects that have large data members, such as large arrays, should also be avoided on the stack, and should be dynamically allocated (consider smart pointers).
@@ -392,7 +412,7 @@ or `esp8266 / Arduino <https://github.com/esp8266/Arduino>`__ core,
392
412
types and versions of O/S, you need to provide exact information on what
393
413
your application is about. Only then, people willing to look into your
394
414
issue may be able to compare it to a configuration they are familiar with.
395
-
If you are lucky, they may even attempt to reproduce your issue on their
415
+
If you are lucky, they may even attempt to reproduce your issue on their
396
416
own equipment!
397
417
This will be far more difficult if you provide only vague details,
398
418
so somebody would need to ask you to find out what is really happening.
0 commit comments