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.