Esp32 crashes on Interrupt

I am trying to add interrupt with my esp32 and came across the following example from Arduino IDE (under esp32 section)

Here is the example code

#include <Arduino.h>

struct Button {
    const uint8_t PIN;
    uint32_t numberKeyPresses;
    bool pressed;
};

Button button1 = {23, 0, false};
Button button2 = {18, 0, false};

void IRAM_ATTR isr(void* arg) {
    Button* s = static_cast<Button*>(arg);
    s->numberKeyPresses += 1;
    s->pressed = true;
}

void IRAM_ATTR isr() {
    button2.numberKeyPresses += 1;
    button2.pressed = true;
}

void setup() {
    Serial.begin(115200);
    pinMode(button1.PIN, INPUT_PULLUP);
    attachInterruptArg(button1.PIN, isr, &button1, FALLING);
    pinMode(button2.PIN, INPUT_PULLUP);
    attachInterrupt(button2.PIN, isr, FALLING);
}

void loop() {
    if (button1.pressed) {
        Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses);
        button1.pressed = false;
    }
    if (button2.pressed) {
        Serial.printf("Button 2 has been pressed %u times\n", button2.numberKeyPresses);
        button2.pressed = false;
    }
    static uint32_t lastMillis = 0;
    if (millis() - lastMillis > 10000) {
      lastMillis = millis();
      detachInterrupt(button1.PIN);
    }
}

Guru Meditation Error: Core 1 panic'ed (Coprocessor exception)
Core 1 register dump:
PC : 0x40080f72 PS : 0x00060231 A0 : 0x800810f8 A1 : 0x3ffbeab0
A2 : 0x3ffbff14 A3 : 0x00000001 A4 : 0x00000000 A5 : 0x00000000
A6 : 0x00000003 A7 : 0x3ffb83ac A8 : 0x80080f6f A9 : 0x3ffbeb50
A10 : 0x0000000c A11 : 0x00000005 A12 : 0x8008a2b5 A13 : 0x3ffb1e40
A14 : 0x3ffb8000 A15 : 0x00000000 SAR : 0x0000000a EXCCAUSE: 0x00000004
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff
Core 1 was running in ISR context:
EPC1 : 0x40080f72 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x400844bb

ELF file SHA256: 0000000000000000

Backtrace: 0x40080f72:0x3ffbeab0 0x400810f5:0x3ffbeb70 0x400810f5:0x3ffbeba0 0x400842a1:0x3ffbebc0 0x4000bfed:0x3ffb1ee0 0x40087da1:0x3ffb1ef0 0x40082cc7:0x3ffb1f10 0x400d3382:0x3ffb1f30 0x400d3399:0x3ffb1f60 0x400d1143:0x3ffb1f80 0x400d49be:0x3ffb1fb0 0x40086c69:0x3ffb1fd0

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
-spd-
Guru Meditation Error: Core 1 panic'ed (Coprocessor exception)
Core 1 register dump:
PC : 0x40080f72 PS : 0x00060231 A0 : 0x800810f8 A1 : 0x3ffbeab0
A2 : 0x3ffbff14 A3 : 0x00000001 A4 : 0x00000000 A5 : 0x00000000
A6 : 0x00000003 A7 : 0x3ffb83ac A8 : 0x80080f6f A9 : 0x3ffbeb50
A10 : 0x0000000c A11 : 0x00000005 A12 : 0x8008a2b5 A13 : 0x3ffb1e40
A14 : 0x3ffb8000 A15 : 0x00000000 SAR : 0x0000000a EXCCAUSE: 0x00000004
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff
Core 1 was running in ISR context:
EPC1 : 0x40080f72 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x400844bb

ELF file SHA256: 0000000000000000

Backtrace: 0x40080f72:0x3ffbeab0 0x400810f5:0x3ffbeb70 0x400810f5:0x3ffbeba0 0x400842a1:0x3ffbebc0 0x4000bfed:0x3ffb1ee0 0x40087da1:0x3ffb1ef0 0x40082cc7:0x3ffb1f10 0x400d3382:0x3ffb1f30 0x400d3399:0x3ffb1f60 0x400d1143:0x3ffb1f80 0x400d49be:0x3ffb1fb0 0x40086c69:0x3ffb1fd0

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8```
type or paste code here

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

There is an utility to analyse the stack trace; I've forgotten the name. Works with IDE 1.8.x, to my knowledge not yet with IDE 2.0.

why does your code detach one of the interrupt pins?

there are two functions with identical name "isr"
does this even compile?
I guess the names should be different

here is a more simple code that demonstrates GPIO-interrupt on IO-pin 15

// ESP32-Democode for a GPIO-Interrupt

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);

#define dbgc(myFixedText, variableName) \
  do { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  } while (false);

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;

const unsigned long ISR_Pin = 15;

volatile int interruptCounter;

void IRAM_ATTR MyISR_func() {
  interruptCounter++;
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  attachInterrupt(digitalPinToInterrupt(ISR_Pin), MyISR_func, RISING);
}

void loop() {
  BlinkHeartBeatLED(OnBoard_LED, 250);
  //dbgi("1:", interruptCounter, 1000);
  dbgc("2:", interruptCounter);
    
}

best regards Stefan

Hmm... your definition of "more simple" is interesting.

I think you could have left it at...


void IRAM_ATTR onTimer()
{
  BaseType_t xHigherPriorityTaskWoken;
  xEventGroupSetBitsFromISR(eg, OneMinuteGroup, &xHigherPriorityTaskWoken);
} // void IRAM_ATTR onTimer()

For the fastest service of an interrupt on a ESP32 use BaseType_t xHigherPriorityTaskWoken; so that the OS sets aside anything it is doing to handle the interrupt.

Do a web search for "function overloading".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.