Arduino
Arduino copied to clipboard
Allow enabling/disabling external interrupts and clear pending interrupts on attach
This is a fix for ~~#510~~https://github.com/arduino/ArduinoCore-avr/issues/244, plus the addition of a new enableInterrupt and disableInterrupt API to cater for libraries that relied on the broken behaviour of attachInterrupt.
See also this discussion: https://groups.google.com/a/arduino.cc/d/topic/developers/I9iJYIcbPLs/discussion
This code is still unfinished:
- Only quickly compile tested
- Not implemented on SAM yet
I updated the code based on feedback from @PaulStoffregen: enableInterrupt now now never clears pending interrupts, that code is moved into attachInterrupt (which always clears).
I just updated this PR to also support SAM and split a commit in two. For clearing an interrupt on SAM, there is a caveat I also pointed out on the mailling list, see the last commit for details.
I haven't runtime-tested the code on Due due to lack of a Due (I'll probably get one soon, though). Any testing on Due is welcomed.
I haven't runtime-tested the code on AVR after the PR update. The code for AVR shouldn't have changed, but it should probably be tested again nonetheless. I started on this, but then found the bootloader on my Uno was broken en somehow my ICSP programmer was also broken and gave up for today...
I just tested this on my Uno, works as expected: Interrupts occuring while detached are ignored, interrupts while disabled are remembered and fired when enabling. For reference, here is the test sketch I used (25 and 26 are legacy from my Due test sketch):
void int25() {
Serial.println("INT25");
}
void int26() {
Serial.println("INT26");
}
void setup() {
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
attachInterrupt(0, int25, CHANGE);
attachInterrupt(1, int26, CHANGE);
detachInterrupt(0);
Serial.println("INIT");
while(Serial.read() == -1);
attachInterrupt(0, int25, CHANGE);
Serial.println("ATTACH");
while(Serial.read() == -1);
disableInterrupt(0);
Serial.println("DISABLE");
while(Serial.read() == -1);
enableInterrupt(0);
Serial.println("ENABLE");
}
void loop() {
}
Good to know that this exists. Can you build it please?
@ArduinoBot build this please
Build successful. Please test this code using one of the following: http://downloads.arduino.cc/javaide/pull_requests/arduino-PR-2159-BUILD-390-linux32.tar.xz http://downloads.arduino.cc/javaide/pull_requests/arduino-PR-2159-BUILD-390-linux64.tar.xz http://downloads.arduino.cc/javaide/pull_requests/arduino-PR-2159-BUILD-390-windows.zip http://downloads.arduino.cc/javaide/pull_requests/arduino-PR-2159-BUILD-390-macosx.zip