user34344: I'm using a teensy 4.0 with two mcp23s17 gpio expanders where #1 has 16 buttons wired to it, and #2 has 4 buttons and six rotary encoders. To run everything I'm using majenkos mcp23s17 library. Recently, I refactored my code, and suddenly I'm unable to setup the mcp's to have interrupts working properly.
I've tried to narrow the problem down by commenting out code, and it appears that as soon as I call         gpioExpander.enableInterrupt(pin, CHANGE); in gpioSetup() the problem occurs. In detail, loop() runs fine, but as soon as I press a button, the teensy hangs. Also, to restore the teensy I have to upload the code with the call to gpioExpander.enableInterrupt(pin, CHANGE); commented out in order to have the teensy up and running again.
I've been staring myself blind on the code, and I suspect I'm missing something obvious. Your help is appreciated.
//main.cpp
#include <Arduino.h>

#include "GPIO.h"
#include "MCP23S17.h"

extern MCP23S17 gpioExpander1;
extern MCP23S17 gpioExpander2;

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

void loop() {
    GPIOtest(gpioExpander1, gpioExpander2);
}

//GPIO.h
#ifndef GPIO_H
#define GPIO_H

#include <Arduino.h>

#include "MCP23S17.h"

constexpr uint8_t address = 0;
constexpr uint8_t interruptPin1 = 8;
constexpr uint8_t interruptPin2 = 22;
constexpr uint8_t chipSelectPin1 = 7;
constexpr uint8_t chipSelectPin2 = 21;

void setGPIO1Pins(MCP23S17 &gpioExpander);
void setGPIO2Pins(MCP23S17 &gpioExpander);
void gpioSetup();
void GPIO1interrupt();
void GPIO2interrupt();
void GPIOtest(MCP23S17 &gpioExpander1, MCP23S17 &gpioExpander2);

#endif

//GPIO.cpp
#include "GPIO.h"

#include <Arduino.h>
#include <MCP23S17.h>

volatile bool interruptFlag1 = false;
volatile bool interruptFlag2 = false;

MCP23S17 gpioExpander1(&SPI, chipSelectPin1, address);
MCP23S17 gpioExpander2(&SPI, chipSelectPin2, address);

//  Set all Port Expander pins to desired mode INPUT_PULLUP
//  Set all Port Expander input pins interrupts
void setGPIOPins(MCP23S17& gpioExpander) {
    for (uint8_t pin = 0; pin <= 15; ++pin) {
        gpioExpander.pinMode(pin, INPUT_PULLUP);
        gpioExpander.enableInterrupt(pin, CHANGE); 
    }

    // set Port Expander Interrupt configuratons
    gpioExpander.setMirror(true);
    gpioExpander.setInterruptOD(false);
    gpioExpander.setInterruptLevel(LOW);
}

void gpioSetup() {
    gpioExpander1.begin();
    gpioExpander2.begin();
    digitalWrite(chipSelectPin1, HIGH);
    digitalWrite(chipSelectPin2, HIGH);

    pinMode(interruptPin1, INPUT_PULLUP);
    pinMode(interruptPin2, INPUT_PULLUP);

    attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, LOW);
    attachInterrupt(digitalPinToInterrupt(interruptPin2), GPIO2interrupt, LOW);

    setGPIOPins(gpioExpander1);
    setGPIOPins(gpioExpander2);

    // GPIO registers are read to be cleared
    gpioExpander1.getInterruptValue();
    gpioExpander2.getInterruptValue();
}

void GPIO1interrupt() {
    interruptFlag1 = true;
}

void GPIO2interrupt() {
    interruptFlag2 = true;
}

void GPIOtest(MCP23S17& gpioExpander1, MCP23S17& gpioExpander2) {
    if (interruptFlag1 == true) {
        Serial.println("interrupt 1 = true");
        interruptFlag1 = false;  
    }

    if (interruptFlag2 == true) {
        Serial.println("interrupt 2 = true");
        interruptFlag2 = false;
    }
}


user70020: Short version, try:
attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin2), GPIO2interrupt, FALLING);

FALLING (instead of LOW) being the active ingredient. I'm assuming FALLING is supported. If not you can try synthesizing this with CHANGE and reading the port to see whether or not it is currently LOW.
And then call .getInterruptValue(); to clear the MCP23S17's interrupt condition where you clear the global volatile flags:
void GPIOtest(MCP23S17& gpioExpander1, MCP23S17& gpioExpander2) {
    if (interruptFlag1 == true) {
        Serial.println("interrupt 1 = true");
        interruptFlag1 = false;
        gpioExpander1.getInterruptValue(); // <------
    }

    if (interruptFlag2 == true) {
        Serial.println("interrupt 2 = true");
        interruptFlag2 = false;
        gpioExpander2.getInterruptValue(); // <------
    }
}


Long version:
This a guess, since I'm unable to test. But this is maybe not a bad guess:
You have configured the MCP23S17 device to drive active low on/during an MCP23S17 interrupt condition:
    gpioExpander.setMirror(true);
    gpioExpander.setInterruptOD(false);
    gpioExpander.setInterruptLevel(LOW);

So, were an MCP23S17 interrupt to occur (gpioExpander.enableInterrupt(pin, CHANGE);), the interrupt flag within the MCP23S17 would be set, and INTA/INTB of the MCP23S17 would become active low.
The MCP23S17 datasheet says:

The interrupt condition is cleared after the LSb of the data is
clocked out during a read command of GPIO or INTCAP

You have configured your interrupt pins for interrupting on a LOW level interrupt:
attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, LOW);

So, if I understand this correctly, your Teensy continually re-enter the GPIO#interrupt() functions until something read causes the interrupt condition to be cleared. You are not clearing the interrupt condition inside the ISR, you're setting a flag to do that later in the main line of execution, which is reasonable by itself. One thing I can guess is that had you'd previous had something to clear the interrupt condition inside the interrupt and it is now outside the interrupt, namely your call to .getInterruptValue(), which reads the INTCAP register mentioned above. Or perhaps you'd be using one of the functions that reads a pin/port that would have cleared the interrupt condition. Clearing the global variable alone will not clear the interrupt condition, and so you're going to go right back into ISR which will reassign the global variable.
An AVR will execute a single instruction outside of the ISR before reentering the ISR. So the effect of remaining in the interrupt condition is to make your code slow, potentially very slow. I don't know off the top of my head what the Teensy 4.x core will do in this case. Either way, it would lock up or possibly look locked up.
If you were reading/getting-interrupt-value inside the ISR it would have cleared it and so it would not re-entered the ISR repeatedly. Or rather, it wouldn't do that in definitely (or just for long). It possible that the Teensy is so much faster that it might re-enter a few times until the MCP23S17 got around to acknowledging that the interrupt condition had be cleared. I'm not advocating for this so, so I'll leave it at that.
I believe ultimately what you want is to set a FALLING edge interrupt:
attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, FALLING);

Anyway, with FALLING the initial falling edge of the INTA/INTB line sets the interrupt condition in the Teensy (not MCP23S17) but it is cleared when your ISR executes.  So you will not keep re-entering the ISR. So your ISR will enter once to set the global volatile flag. Later when you pick up on the set value of your global volatile flag(s), you need to call .getInterruptValue() (or otherwise read a pin) to clear the interrupt condition in your MCP23S17 (not Teensy) which will de-assert/raise INTA/INTB, at which point the ISR in the Teensy is ready for to get trigger by another falling edge of INTA/INTB.
It's also probably possible to do this with a LOW-level interrupt, but you'd have keep un-configuring them in the ISR and re-configuring them after you clear the MCP23S17's condition. Not sure that would be recommended. But, I think it's possible.

34344: <p>I'm using a teensy 4.0 with two mcp23s17 gpio expanders where #1 has 16 buttons wired to it, and #2 has 4 buttons and six rotary encoders. To run everything I'm using majenkos mcp23s17 library. Recently, I refactored my code, and suddenly I'm unable to setup the mcp's to have interrupts working properly.</p>
<p>I've tried to narrow the problem down by commenting out code, and it appears that as soon as I call         <code>gpioExpander.enableInterrupt(pin, CHANGE);</code> in <code>gpioSetup()</code> the problem occurs. In detail, <code>loop()</code> runs fine, but as soon as I press a button, the teensy hangs. Also, to restore the teensy I have to upload the code with the call to <code>gpioExpander.enableInterrupt(pin, CHANGE);</code> commented out in order to have the teensy up and running again.</p>
<p>I've been staring myself blind on the code, and I suspect I'm missing something obvious. Your help is appreciated.</p>
<pre class="lang-cpp prettyprint-override"><code>//main.cpp
#include &lt;Arduino.h&gt;

#include &quot;GPIO.h&quot;
#include &quot;MCP23S17.h&quot;

extern MCP23S17 gpioExpander1;
extern MCP23S17 gpioExpander2;

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

void loop() {
    GPIOtest(gpioExpander1, gpioExpander2);
}

//GPIO.h
#ifndef GPIO_H
#define GPIO_H

#include &lt;Arduino.h&gt;

#include &quot;MCP23S17.h&quot;

constexpr uint8_t address = 0;
constexpr uint8_t interruptPin1 = 8;
constexpr uint8_t interruptPin2 = 22;
constexpr uint8_t chipSelectPin1 = 7;
constexpr uint8_t chipSelectPin2 = 21;

void setGPIO1Pins(MCP23S17 &amp;gpioExpander);
void setGPIO2Pins(MCP23S17 &amp;gpioExpander);
void gpioSetup();
void GPIO1interrupt();
void GPIO2interrupt();
void GPIOtest(MCP23S17 &amp;gpioExpander1, MCP23S17 &amp;gpioExpander2);

#endif

//GPIO.cpp
#include &quot;GPIO.h&quot;

#include &lt;Arduino.h&gt;
#include &lt;MCP23S17.h&gt;

volatile bool interruptFlag1 = false;
volatile bool interruptFlag2 = false;

MCP23S17 gpioExpander1(&amp;SPI, chipSelectPin1, address);
MCP23S17 gpioExpander2(&amp;SPI, chipSelectPin2, address);

//  Set all Port Expander pins to desired mode INPUT_PULLUP
//  Set all Port Expander input pins interrupts
void setGPIOPins(MCP23S17&amp; gpioExpander) {
    for (uint8_t pin = 0; pin &lt;= 15; ++pin) {
        gpioExpander.pinMode(pin, INPUT_PULLUP);
        gpioExpander.enableInterrupt(pin, CHANGE); 
    }

    // set Port Expander Interrupt configuratons
    gpioExpander.setMirror(true);
    gpioExpander.setInterruptOD(false);
    gpioExpander.setInterruptLevel(LOW);
}

void gpioSetup() {
    gpioExpander1.begin();
    gpioExpander2.begin();
    digitalWrite(chipSelectPin1, HIGH);
    digitalWrite(chipSelectPin2, HIGH);

    pinMode(interruptPin1, INPUT_PULLUP);
    pinMode(interruptPin2, INPUT_PULLUP);

    attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, LOW);
    attachInterrupt(digitalPinToInterrupt(interruptPin2), GPIO2interrupt, LOW);

    setGPIOPins(gpioExpander1);
    setGPIOPins(gpioExpander2);

    // GPIO registers are read to be cleared
    gpioExpander1.getInterruptValue();
    gpioExpander2.getInterruptValue();
}

void GPIO1interrupt() {
    interruptFlag1 = true;
}

void GPIO2interrupt() {
    interruptFlag2 = true;
}

void GPIOtest(MCP23S17&amp; gpioExpander1, MCP23S17&amp; gpioExpander2) {
    if (interruptFlag1 == true) {
        Serial.println(&quot;interrupt 1 = true&quot;);
        interruptFlag1 = false;  
    }

    if (interruptFlag2 == true) {
        Serial.println(&quot;interrupt 2 = true&quot;);
        interruptFlag2 = false;
    }
}
</code></pre>
 70020: <h3>Short version, try:</h3>
<pre class="lang-cpp prettyprint-override"><code>attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(interruptPin2), GPIO2interrupt, FALLING);
</code></pre>
<p><strong><code>FALLING</code></strong> (instead of <code>LOW</code>) being the <a href="https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/" rel="nofollow noreferrer">active ingredient</a>. I'm assuming <code>FALLING</code> is supported. If not you can try synthesizing this with <code>CHANGE</code> and reading the port to see whether or not it is currently <code>LOW</code>.</p>
<p>And then call <code>.getInterruptValue();</code> to clear the MCP23S17's interrupt condition where you clear the global volatile flags:</p>
<pre class="lang-cpp prettyprint-override"><code>void GPIOtest(MCP23S17&amp; gpioExpander1, MCP23S17&amp; gpioExpander2) {
    if (interruptFlag1 == true) {
        Serial.println(&quot;interrupt 1 = true&quot;);
        interruptFlag1 = false;
        gpioExpander1.getInterruptValue(); // &lt;------
    }

    if (interruptFlag2 == true) {
        Serial.println(&quot;interrupt 2 = true&quot;);
        interruptFlag2 = false;
        gpioExpander2.getInterruptValue(); // &lt;------
    }
}
</code></pre>
<hr />
<h3>Long version:</h3>
<p>This a guess, since I'm unable to test. But this is maybe not a bad guess:</p>
<p>You have configured the MCP23S17 device to <strong>drive</strong> active <strong>low</strong> on/during an MCP23S17 interrupt condition:</p>
<pre class="lang-cpp prettyprint-override"><code>    gpioExpander.setMirror(true);
    gpioExpander.setInterruptOD(false);
    gpioExpander.setInterruptLevel(LOW);
</code></pre>
<p>So, were an MCP23S17 interrupt to occur (<code>gpioExpander.enableInterrupt(pin, CHANGE);</code>), the interrupt flag within the MCP23S17 would be set, and INTA/INTB of the MCP23S17 would become active low.</p>
<p>The MCP23S17 datasheet says:</p>
<blockquote>
<p>The interrupt condition is cleared after the LSb of the data is
clocked out during a read command of GPIO or INTCAP</p>
</blockquote>
<p>You have configured your interrupt pins for interrupting on a LOW <strong>level</strong> interrupt:</p>
<pre class="lang-cpp prettyprint-override"><code>attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, LOW);
</code></pre>
<p>So, if I understand this correctly, your Teensy continually re-enter the <code>GPIO#interrupt()</code> functions until something read causes the interrupt condition to be cleared. You are not clearing the interrupt condition <strong>inside</strong> the ISR, you're setting a flag to do that later in the main line of execution, which is reasonable by itself. One thing I can guess is that had you'd previous had something to clear the interrupt condition <strong>inside</strong> the interrupt and it is now outside the interrupt, namely your call to <code>.getInterruptValue()</code>, which <a href="https://github.com/MajenkoLibraries/MCP23S17/blob/6f6bac/src/MCP23S17.cpp#L503-L505" rel="nofollow noreferrer">reads the INTCAP register</a> mentioned above. Or perhaps you'd be using <a href="https://github.com/MajenkoLibraries/MCP23S17/blob/6f6bac/src/MCP23S17.cpp#L348-L352" rel="nofollow noreferrer">one of the functions</a> that reads a pin/port that would have cleared the interrupt condition. Clearing the global variable alone will not clear the interrupt condition, and so you're going to go right back into ISR which will reassign the global variable.</p>
<p>An AVR will execute a single instruction outside of the ISR before reentering the ISR. So the effect of remaining in the interrupt condition is to make your code slow, potentially <strong>very</strong> slow. I don't know off the top of my head what the Teensy 4.x core will do in this case. Either way, it <em>would</em> lock up or possibly <em>look</em> locked up.</p>
<p><em>If</em> you were reading/getting-interrupt-value inside the ISR it would have cleared it and so it would not re-entered the ISR repeatedly. Or rather, it wouldn't do that in definitely (or just for long). <em>It possible that the Teensy is so much faster that it might re-enter a few times until the MCP23S17 got around to acknowledging that the interrupt condition had be cleared. I'm not advocating for this so, so I'll leave it at that.</em></p>
<p>I believe ultimately what you want is to set a <code>FALLING</code> edge interrupt:</p>
<pre class="lang-cpp prettyprint-override"><code>attachInterrupt(digitalPinToInterrupt(interruptPin1), GPIO1interrupt, FALLING);
</code></pre>
<p>Anyway, with <code>FALLING</code> the initial falling edge of the INTA/INTB line sets the interrupt condition in the Teensy (not MCP23S17) but it is cleared when your ISR executes.  So you will not keep re-entering the ISR. So your ISR will enter <em>once</em> to set the global <code>volatile</code> flag. Later when you pick up on the set value of your global <code>volatile</code> flag(s), you need to call <code>.getInterruptValue()</code> (or otherwise read a pin) to clear the interrupt condition in your MCP23S17 (not Teensy) which will de-assert/raise INTA/INTB, at which point the ISR in the Teensy is ready for to get trigger by another falling edge of INTA/INTB.</p>
<p>It's also probably possible to do this with a <code>LOW</code>-level interrupt, but you'd have keep un-configuring them in the ISR and re-configuring them after you clear the MCP23S17's condition. Not sure that would be recommended. But, I think it's possible.</p>
