How To Clear An Interrupt Flag On Arduino Zero

To set (enable) an external interrupt on a given port pin, in this example digital pin 7, which is port pin PA21 or external interrupt EXTINT[5], (SAMD21 datasheet, Table 6.1 PORT Function Multiplexing):

REG_EIC_INTENSET = EIC_INTENSET_EXTINT5;     // Set EXTINT5

...likewise to clear (disable) the interrupt:

REG_EIC_INTENCLR = EIC_INTENCLR_EXTINT5;     // Clear EXTINT5

To clear the interrupt flag after it's been set:

REG_EIC_INTFLAG = EIC_INTFLAG_EXTINT5;     // Clear the EXTINT5 flag

Before using interrupts on the SAMD21 you'll also have to enable the port multiplexer for that pin, in this case digital pin 7:

PORT->Group[g_APinDescription[7].ulPort].PINCFG[g_APinDescription[7].ulPin].bit.PMUXEN = 1;

... and switch the multiplexer to peripheral A (EIC), again for digital pin 7:

PORT->Group[g_APinDescription[6].ulPort].PMUX[g_APinDescription[6].ulPin >> 1].reg |= PORT_PMUX_PMUXO_A;

Note that the SAMD21's multiplexer registers are arranged as odd and even pairs. There are 16 multiplexer registers for the 32 port pins on Port A. Digital pin 6 is port pin PA20, which is even, while digital pin 7 is PA21, which is odd. The >> 1 is essentially divide by 2, so we're essentially addressing multiplexer register 10 (PA20/2) for the PA20 and PA21 pair.

1 Like