Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Allow enabling/disabling external interrupts and clear pending interrupts on attach

Open matthijskooijman opened this issue 11 years ago • 7 comments

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

matthijskooijman avatar Jul 02 '14 20:07 matthijskooijman

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).

matthijskooijman avatar Jul 07 '14 10:07 matthijskooijman

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...

matthijskooijman avatar Oct 27 '14 17:10 matthijskooijman

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() {
}

matthijskooijman avatar Oct 29 '14 16:10 matthijskooijman

Good to know that this exists. Can you build it please?

NicoHood avatar Sep 02 '15 09:09 NicoHood

@ArduinoBot build this please

matthijskooijman avatar Sep 03 '15 06:09 matthijskooijman

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

ArduinoBot avatar Sep 03 '15 07:09 ArduinoBot

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Apr 09 '21 13:04 CLAassistant