|
1 | 1 | Reference
|
2 | 2 | =========
|
3 | 3 |
|
| 4 | +Interrupts |
| 5 | +---------- |
| 6 | + |
| 7 | +Interrupts can be used on the ESP8266, but they must be used with care |
| 8 | +and have several limitations: |
| 9 | + |
| 10 | +* Interrupt callback functions must be in IRAM, because the flash may be |
| 11 | + in the middle of other operations when they occur. Do this by adding |
| 12 | + the ``ICACHE_RAM_ATTR`` attribute on the function definition. If this |
| 13 | + attribute is not present, the sketch will crash when it attempts to |
| 14 | + ``attachInterrupt`` with an error message. |
| 15 | + |
| 16 | +.. code:: cpp |
| 17 | +
|
| 18 | + ICACHE_RAM_ATTR void gpio_change_handler(void *data) {... |
| 19 | +
|
| 20 | +* Interrupts must not call ``delay()`` or ``yield()``, or call any routines |
| 21 | + which internally use ``delay()`` or ``yield()`` either. |
| 22 | + |
| 23 | +* Long-running (>1ms) tasks in interrupts will cause instabilty or crashes. |
| 24 | + WiFi and other portions of the core can become unstable if interrupts |
| 25 | + are blocked by a long-running interrupt. If you have much to do, you can |
| 26 | + set a volatile global flag that your main ``loop()`` can check each pass |
| 27 | + or use a scheduled function (which will be called outside of the interrupt |
| 28 | + context when it is safe) to do long-running work. |
| 29 | + |
| 30 | +* Memory operations can be dangerous and should be avoided in interrupts. |
| 31 | + Calls to ``new`` or ``malloc`` should be minimized because they may require |
| 32 | + a long running time if memory is fragmented. Calls to ``realloc`` and |
| 33 | + ``free`` must NEVER be called. Using any routines or objects which call |
| 34 | + ``free`` or ``realloc`` themselves is also forbidden for the same reason. |
| 35 | + This means that ``String``, ``std::string``, ``std::vector`` and other |
| 36 | + classes which use contiguous memory that may be resized must be used with |
| 37 | + extreme care (ensuring strings aren't changed, vector elements aren't |
| 38 | + added, etc.). |
| 39 | + |
4 | 40 | Digital IO
|
5 | 41 | ----------
|
6 | 42 |
|
|
0 commit comments