How To Clear An Interrupt Flag On Arduino Zero

I'm trying to port code I have working for Unos and Megas but I've hit a roadblock on how to clear an interrupt flag on the Zero.

For the Unos and Megas I use the following line of code which I borrowed from Nick Gammons excellent article on Interrupts:

EIFR = bit (INTF1);

I've tried to decipher the datasheet for zero's chip but it's quite a different beast and I've got lost. I believe I need to write a 1 to the EXTINTx bit (for external interrupt x) in the INTFLAG register but it's a four byte register which requires the use of offsets which is where I get lost. Any help would be much appreciated.

Thanks & regards,

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

Thanks very much, MartinL.

Hey @MartinL how do you figure Digital pin 7 goes to PA21, from the Schematic it looks like PA21 goes to the ATN jumper. Am I missing somthing?

lpaulson:
Hey @MartinL how do you figure Digital pin 7 goes to PA21, from the Schematic it looks like PA21 goes to the ATN jumper. Am I missing somthing?

Check the variant files of the board, where every pin number are mapped.

That schematic is wrong. That's an older version of the schematic from before Arduino.cc and Arduino.org split, and matches the M0 PRO by Arduino.org.

If you check pins 2 and 4, you will see those are reversed as well.

Adafruit has the correct schematics here:

I guess they posted the Eagle schematic because it is easier for beginners to read than the schematic from what I assume is Altium, but they really should have updated it to be accurate.