Interrupt attach/detach and flag clear order

Hello,

Speaking of ATmega328P, what would be the correct/best order to attach/detach an interrupt and clear its flag?

For example when detaching an interrupt:

  noInterrupts();
  EIFR = bit(INTF0);     //clear interrupt flag
  detachInterrupt(digitalPinToInterrupt(RTC_INT_PIN));
  interrupts();

In case of an edge or logic change happens between flag clear and detaching the interrupt, the flag could be set again and after enabling interrupts globally, the corresponding interrupt vector could be executed?

Edit: I just found a hint at EIFR register description:

Bit 1 – INTF1: External Interrupt Flag 1
When an edge or logic change on the INT1 pin triggers an interrupt request, INTF1 becomes set (one). If the I-bit in SREG and the INT1 bit in EIMSK are set (one), the MCU will jump to the corresponding interrupt vector. The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be cleared by writing a logical one to it. This flag is always cleared when INT1 is configured as a level interrupt.

So External Interrupt Request 0 Enable (INT0 in EIMSK) and External Interrupt Flag 0 (INTF0 in EIFR) bit has to be set for corresponding interrupt vector execution? Based on this, the order might not matter.

why would you clear the flag upon detaching the interrupt? once it's detached, you won't trigger anyway

if it's an issue, it's best done when you attach the ISR if you don't want to be annoyed by pending interrupts

there is an open issue on this topic

So both INT0 in EIMSK and INTF0 in EIFR bit has to be set for corresponding interrupt vector execution.

Thanks, this was not clear.

you can see what they do in the source code for void detachInterrupt() basically playing just with EIMSK

Yes, I saw that. That's why I started to refer to INT0 in EIMSK.

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