0

I am trying to store the value in esp32 using preferences library when an interrupt is triggered.

Guru Meditation Error: Core  1 panic'ed (Interrupt wdt timeout on CPU1)
Core 1 register dump:
PC      : 0x4008867a  PS      : 0x00060034  A0      : 0x80087a9b  A1      : 0x3ffbe860  
A2      : 0x3ffb7e0c  A3      : 0x3ffb8074  A4      : 0x00000001  A5      : 0x00000001  
A6      : 0x00060023  A7      : 0x00000000  A8      : 0x3ffb8074  A9      : 0x3ffb8074  
A10     : 0x00000001  A11     : 0x00000001  A12     : 0x00000020  A13     : 0x0000ff00  
A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x0000001e  EXCCAUSE: 0x00000006  
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  
Core 1 was running in ISR context:
EPC1    : 0x40085d07  EPC2    : 0x00000000  EPC3    : 0x00000000  EPC4    : 0x4008867a

ELF file SHA256: 0000000000000000

Backtrace: 0x4008867a:0x3ffbe860 0x40087a98:0x3ffbe880 0x400860cf:0x3ffbe8a0 0x400e2f42:0x3ffbe8e0 0x400e300a:0x3ffbe900 0x40083cad:0x3ffbe920 0x400840c4:0x3ffbe940 0x400e7b01:0x3ffbe990 0x400e5f66:0x3ffbe9b0 0x400e629e:0x3ffbe9d0 0x400e5a92:0x3ffbea50 0x400e4a77:0x3ffbeac0 0x400e4e55:0x3ffbeb10 0x400d1fa4:0x3ffbeb30 0x40080ecd:0x3ffbeb50 0x40080f19:0x3ffbeb70 0x40083969:0x3ffbeb90 0x40081b46:0x3ffb1e00 0x400826ef:0x3ffb1e20 0x40081183:0x3ffb1e40 0x400811a7:0x3ffb1e60 0x400d1cb7:0x3ffb1e80 0x400d1c85:0x3ffb1ea0 0x400d1c30:0x3ffb1ec0 0x400ebb35:0x3ffb1ee0 0x400ebbfe:0x3ffb1f00 0x400d1795:0x3ffb1f30 0x400d18da:0x3ffb1f70 0x400d1b47:0x3ffb1f90 0x400d33e9:0x3ffb1fb0 0x40086295:0x3ffb1fd0

Core 0 register dump:
PC      : 0x40086729  PS      : 0x00060034  A0      : 0x80086bd6  A1      : 0x3ffbc020  
A2      : 0x3ffbdf74  A3      : 0xb33fffff  A4      : 0x00060023  A5      : 0x003fffff  
A6      : 0x00060021  A7      : 0x00000000  A8      : 0x0000abab  A9      : 0x0000cdcd  
A10     : 0x0000abab  A11     : 0x00060023  A12     : 0x00060021  A13     : 0x3ffbc0d0  
A14     : 0x00000003  A15     : 0x00060b23  SAR     : 0x00000013  EXCCAUSE: 0x00000006  
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000

Backtrace: 0x40086729:0x3ffbc020 0x40086bd3:0x3ffbc050 0x40088524:0x3ffbc070 0x400884da:0x3ffbc090 0x400893fb:0x00000000

Rebooting...

But I am getting this error when the interrupt is triggered. Please help me to resolve this

  • The output indicates that the watchdog has stopped your program because some work took too long. It also indicates that it is related to an interrupt handler. What are you doing in the interrupt? Are you writing debug output to the serial port? Please show your code. – Codo May 23 '22 at 11:36
  • 1
    I'm answering your question but in the future please post a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with questions about code, otherwise you're asking us to guess what you're doing. – romkey May 23 '22 at 15:03

1 Answers1

1

It's difficult to be sure as you didn't share any code, but... you should not update Preferences from an ISR. In fact unless you really really know what you're doing, you should do as little as possible inside an ISR.

Interrupts happen unpredictably... they literally interrupt whatever code is currently executing on the CPU. That means that any data structures the code is currently changing can be in an inconsistent state. Calling a function from the interrupt handler that uses those data structures will lead to data corruption and likely a crash.

Unless you wrote the code you're calling, you don't know what it calls. So you may say "it's okay, I'm not using Preferences outside the ISR" but you don't know what other functions Preferences calls.

You may get lucky and it may work at times... and it may also just crash randomly.

Again, unless you really really know what you're doing, there are very few functions that are safe to call inside an ISR. These are generally FreeRTOS functions that are explicitly designated as safe.

Interrupt handlers also block the CPU from doing other things it needs to do, so (unless you really really know what you're doing) you should minimize the amount of time you spend in the handler.

For most people who don't have experience writing interrupt handlers (especially people coding with the Arduino core), the best thing to do is the minimal work possible and set a flag that loop() inspects, and do the work in loop().

It's important that the flag be declared with the volatile keyword so that the compiler knows that its value can be changed unexpectedly.

It's also important that the interrupt handler be declared with IRAM_ATTR, to tell the system to keep its code loaded in instruction RAM so that it will be available when needed. Otherwise your program will crash if the code for the interrupt handler hasn't already been fetched from flash memory.

For instance:

volatile boolean interrupt_happened = false;

IRAM_ATTR void interrupt_handler() {
  interrupt_happened = true;
}

void loop() {
  if(interrupt_happened) {
    do_some_work();
    interrupt_happened = false;
  }
}
romkey
  • 6,218
  • 3
  • 16
  • 12