Can I reset a Timer ISR within a different ISR for ESP32?

Hi,

I have a working sketch below for an ESP32 (standard wroom-32 model).

It uses 2 interrupt routines, which are 1 x digital pin interrupt and 1 x timer interrupt.

const byte interruptPin = 4;
volatile bool timerUp = false;

hw_timer_t* My_timer = NULL;

void IRAM_ATTR onTimer() {  //timer ISR
  timerUp = true;
}

void digitalPinISR() {      //digital pin ISR
  timerWrite(My_timer, 0);  //restart timer ISR
}

void setup() {
  pinMode(interruptPin, INPUT_PULLUP);
  Serial.begin(115200);

  //start digital pin ISR
  attachInterrupt(digitalPinToInterrupt(interruptPin), digitalPinISR, RISING);

  //start Timer ISR
  My_timer = timerBegin(0, 80, true);
  timerAttachInterrupt(My_timer, &onTimer, true);
  timerAlarmWrite(My_timer, 250000, true);
  timerAlarmEnable(My_timer);  //Enable
}

void loop() {
  if (timerUp == true) {
    timerUp = false;
    Serial.println("Timer Up");
  }
}

Within the digital pin ISR below, I am restarting the timer interrupt. That is, every time the pin goes from LOW to HIGH, the timer resets to 0.

void digitalPinISR() {      //digital pin ISR
  timerWrite(My_timer, 0);  //restart timer
}

I am fairly new to using interrupts and I’m unsure whether it’s good practice to be modifying 1 interrupt routine within a different interrupt routine, as I have read that going into one interrupt routine will block all other interrupts. The above sketch does appear to work fine however in my testing.

Is this ok to do for the ESP32, or should I use a different approach, such as flipping a boolean variable within the digital pin ISR and then resetting the timer in the main loop?

Thanks.

With Your code

You are invoking a timer-interrupt once every 0,25 seconds. (250.000 microseconds)

What is your overall application? I'm pretty sure that your function loop() will iterate much faster than once every 0,25 seconds.

If your complete code uses delay() resetting the timer-interrupt has almost no effect.

Timing-intervalls longer than 0,01 seconds can easily be handeled without interrupts.

The typical use of interrupts is if you have to react within 10 microseconds = 0,00001 seconds to a signal change.

best regards Stefan

Thank you for your insights, Stefan.

There isn't an overall application for this at the moment, I was just unsure about whether it is ok to reset a timer interrupt within a different ISR.

thanks.

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