Befuddled about interrupts and pin assignments

Hi,

I'm new to the MKR WiFi 1010, but think I know my way around an Uno well enough. I'm having a hard time making the transition, though. I'd like to port an Uno sketch that uses interrupts to the 1010, but it's not straightforward :slight_smile:

For instance, it seems that attachInterrupt() is still supported, but what's the correct way to use it? That is, how do I specify a pin? And which pins can I choose from for interrupts?

See example below. The only thing this code does that I expect it to do is to blink an LED on startup. It does not write a message to the terminal window (set to 115200); it does not seem to run the ISR. The ISR trigger is to unground a pin (the one labeled "A5") that has a 2K pull-up resistor on it.

My interpretation of the A5 pin is that it's also D20, hence my choice for INT_IN. Any and all assistance welcome!

Thanks,

Charles

#define INT_IN 20
#define LED_OUT 5

int state = 0;
bool firstTime;

void myIsr()
{
  state = !state;
}

void setup()
{
  Serial.begin(115200);

  pinMode(INT_IN, INPUT_PULLUP);
  pinMode(LED_OUT, OUTPUT);

  firstTime = true;

  // Prove we're alive
  digitalWrite(LED_OUT, HIGH);
  delay(100);
  digitalWrite(LED_OUT, LOW);

  attachInterrupt(digitalPinToInterrupt(INT_IN),myIsr, RISING);
}

void loop()
{
  if (firstTime)
  {
    Serial.println("MKR Wifi 1010");
    firstTime = false;
  }

  digitalWrite(LED_OUT, state);

  delay(10);
}

Welcome to the forum

Welcome to the world of ARM Cortex-M processors.

ARM Cortex-M processors have a standardized Vector Interrupt Controller (NVIC). Interrupts that are directly connected are handled by CMSIS defined handler functions (named with _Handler). You can find the ones for your SAMD in the following file.

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\1.2.0\CMSIS\Device\ATMEL\samd21\include\samd21g18a.h

The port pin interrupts on the SAMD21 are not handled by the NVIC but an additional interrupt controller called EIC – External Interrupt Controller (see SAMD21 datasheet).

The EIC has 16 interrupts. Arduino has assigned these to pins. There are more pins than interrupt lines on the SAMD21. You can find these in the following file.

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.6\variants\mkrwifi1010\variant.cpp

This file will show you that the pin you chose is not configured for external interrupt. When you use a pin that has been assigned everything else should work. I tested this on my Arduino Nano 33 IoT which is like the little brother of your board.

D0, D1, D4-D7, D8, D9, D16, D17, D32 and D34 should work
D11 seems to be connected to NMI, I suspect you would need to use the NMI_Handler

The interrupt routines you assign is called by the EIC_Handler. You can find all the code for that in:

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.6\cores\arduino\WInterrupts.c

Holy meatballs, it worked!

Thanks for the skeleton key to figuring this out. After reading your reply, I realized that the pin I wanted was #6, not #20, because digitalPinToInterrupt() wants the Arduino pin name.

So now I can catch an interrupt. Onward to the next puzzle!

Best regards,

Charles