Pending interrupts and timers, and end() and detach().

DrM

Well-known member
Consider the following ISR, which disables interrupts, increments and checks a counter, conditionally stops an interval timer or detaches interrupts, and re-enables interrupts.

This is a schematic of some code running on a Teensy 4.0.

What happens to pending timer interrupts when end() is called? Are they cleared by calling end()?

Pro-forma, I want to ask the same for the interrupts driven from the digital i/o pin. Is a pending interrupt condition from the digital i/o pin cleared by calling detachInterrupt()?


Thank you


Code:
void anISR() {

  noInterrupts();

  if ( ++counter == 10 ) {
    
    digitalWriteFast(busyPin, LOW);

    if (is_clocked) {
     someTimer.end();
    }

   if (is_triggered) {
      detachInterrupt( digitalPinToInterrupt( somePin ) );
    }

  }    
  
  interrupts();
}
 
What happens to pending timer interrupts when end() is called? Are they cleared by calling end()?

In your example(), it's possible for an IntervalTimer to expire between the time interrupts have been disabled and end() is called. end() disables the interrupt and sets the callback function pointer for that particular PIT timer channel to NULL. It does not clear the interrupt flag, but even if it was set (pending), that particular channel would not trigger an interrupt when interrupts are re-enabled. The interrupt flag for that channel would remain set until it was reassigned to another IntervalTimer. The begin() function for that new timer would clear the interrupt flag for that channel before re-enabling the timer and interrupt.

Pro-forma, I want to ask the same for the interrupts driven from the digital i/o pin. Is a pending interrupt condition from the digital i/o pin cleared by calling detachInterrupt()?

detachInterrupt() will disable the interrupt for that pin, but does not clear the interrupt flag. Does that mean the "pending interrupt condition" is cleared. I would say yes, because no interrupt will occur, but the actual interrupt flag for that pin would not be cleared unless and until attachInterrupt() was called again.
 
Back
Top