zero attachInterrupt()

Well, I've looked for some documentation on attachInterrupt() for ZERO and I assumed it would be like DUE. Ref manual says there are 16 external interrupts and nearly all pins are mapped to one of those channels. ZERO product web page says external interrupt on all pins except 4 (NMI?) I tried the following simple sketch

volatile int tick;
void ding() {
  tick++;
}
void setup() {

 int i;
 for(i=0;i<20;i++) {
  pinMode(i,INPUT_PULLUP);
  attachInterrupt(i,ding,FALLING);
 }
 Serial.begin(9600);
}

void loop() {
  while(!tick); 
  for(int i=0;i<20;i++) if (!digitalRead(i)) Serial.print(i);
  Serial.print("  ");
  Serial.println(tick);
  tick = 0;
}

and then jumper ground to a pin. I only get a response from pins 0,1,4,5,11,12, 13

I even looked at firmware variant.cpp and most pins are mapped to an external interrupt.

Is my test bogus, and/or which pins should support attachInterrupt()?

answering my own query: part of the problem was my test sketch which tried to attachInterrupt on 20 pins. The EIC only has 16 interrupt pins, so some pins will clearly have to share. So the pins that were "working" in the test sketch were only those that were not sharing an EIC pin.(0,1,5,11,12,13). pin 4 is maybe the NMI ??

so if i just attached one pin in the sketch, then I got all the pins to work EXCEPT 2, 8, 9. I'm not sure about 2 (are 2 and 4 switched?)

BUG >:(

For pins 8 and 9 variant.cpp is wrong. pin 8 needs to use EXTERNAL_INT_6. pin 9 needs to use EXTERNAL_INT_7 (reference manual table 5-1). I edited variant.cpp and got 8 and 9 to work.

Not yet sure how to fix pin 2. pin 4 only works if pin 2 is also configured for attachInterrupt()

(running IDE 1.6.5)

EIC mapping from corrected variant.cpp

         EIC  ZERO pins
           0   11
           1   13
           2   10 a0 a5
           3   12
           4   6 a3
           5   7 a4
           6   8  sda(20)
           7   9  scl(21)
           8   a1
           9   3 a2
           10  1 MOSI(23)
           11  0  SCK(24)
           12  MISO(22)
           13
           14  2
           15  5

Hi @mantoui

thanks for the report, your analysis is correct, it was indeed a bug in variant and in the Core.

I've slighlty modified your test so it uses only the pins from 0 to 13 (that have no overlapping interrupts number):

const int LAST_PIN = 13;

volatile int tick;

void ding() {
  tick++;
}

void setup() {
  int i;
  for (i = 0; i <= LAST_PIN; i++) {
    pinMode(i, INPUT_PULLUP);
    attachInterrupt(i, ding, FALLING);
  }
  Serial.begin(9600);
}

void loop() {
  while (!tick);
  for (int i = 0; i <= LAST_PIN; i++)
    if (!digitalRead(i))
      Serial.print(i);
  Serial.print("  ");
  Serial.println(tick);
  tick = 0;
}

and now I can see that the pins interrupt works as expected (except pin 4 that is NMI so it's not available as external pin interrupt).

The fix is already merged upstream, it will be available with the next release of SAMD core (1.6.2).

If you want to try it now you can use the "hourly build": GitHub - arduino/ArduinoCore-samd: Arduino Core for SAMD21 CPU

Thanks mantoui.

I was wondering why some of the channels on my RC receiver weren't working. Your modification to the "variant.cpp" file fixed it. Much appreciated.

Sorry to revive a really old thread, but I was just curious about the NMI interrupt and how it compares with the other EIC interrupts.

If I understand correctly, the fix that was brought in with 1.6.2 removed the ability to attach an interrupt on D4 which uses NMI line.

I was wondering why the NMI interrupt was removed completely? Was it just because it required specialised code for handling that one interrupt (as well as hard coding of the pin value)? Or is there some other fundamental difference with the NMI that makes it unsuitable to be used in the same way as the other external interrupts?

Is it posdible to use i2c and interrupts on D8 and D9? They are using the same EIC6/7...

Hi Adminius,

Unfortunately it's not possible to use I2C on D8 and D9, as although these pins have SERCOM (serial communications) functionality, they're not suitable for I2C.

Your other options for I2C include D3/D4 and D11/D13. Adafruit's excellent tutorial on creating additional I2C ports can be found here: Creating a new Wire | Using ATSAMD21 SERCOM for more SPI, I2C and Serial ports | Adafruit Learning System.

Ah, sorry, i mean another thing:

6   8  sda(20)
7   9  scl(21)

i mean i2c on SDA/SCL-Pins (20/21) and interrupt on 8/9. This pins shares EIC[6] and EIC[7].

But similar qustion was already answered here: UART on D0/D1 + SPI on D23-24 at same time? - Arduino Zero - Arduino Forum

that means YES, it's possible :slight_smile:

thx any way!