arduino / arduino/ArduinoCore-samd

External interrupts flags are not cleared before enabling the interrupt

Open
#532 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
502
Forks
740
PR merge metrics
No merged PRs in 30d

Description

Hi everybody,

**Bug description:**

If we generate a falling edge on an interrupt source pin when the interrupt is disabled, when we enable the interrupt the interrupt service is triggered immediately.
(This has been tested with falling edge mode, but it should happen with every external interrupt mode)

**Setup description to reproduce the bug:**

Arduino Zero with two N.O. momentary switches connected on pins D2 and D3 to GND.

**Sketch used to show up the bug:**

```
//------ External interrupt test for Arduino Zero ------------------------------------------------------------

// Bug description:
// if we generate a falling edge on an interrupt source pin when the interrupt is disabled,
// when we enable the interrupt the interrupt service is triggered immediately.

// Bug analysis:
// the external interrupt flag is not cleared before enabling the interrupt

#define INT_SOURCE 2 // interrupt source
#define INT_ENABLE 3 // interrupt enable
#define LED 13

void setup()
{
pinMode(INT_SOURCE, INPUT_PULLUP);
pinMode(INT_ENABLE, INPUT_PULLUP);
pinMode(LED, OUTPUT);
// this is to allow the bug to show up
// enable the interrupt
// disable the interrupt
attachInterrupt(digitalPinToInterrupt(INT_SOURCE), IntService, FALLING);
detachInterrupt(digitalPinToInterrupt(INT_SOURCE));
}

void loop()
{
if(!digitalRead (INT_ENABLE)) // if the enable switch is pressed
{ // enable the interrupt
attachInterrupt(digitalPinToInterrupt(INT_SOURCE), IntService, FALLING);
//
while(1){} // and wait for ever
}
}

void IntService() // when an interrupt is detected
{ // light on the led
digitalWrite (LED, HIGH); //
} //
```

**Procedure description:**

1) Reset the board
2) Pulse the the switch connected to D2 to genetate a falling edge on the interrupt pin (note that now the interrupt is disabled).
3) Pulse the switch connected to D3 to enable the interrupt.
4) The led lights on immediately indicating that the interrupt has been detected.

**Bug analysis:**

the external interrupt flag in register EIC->INTFLAG is not cleared before enabling the interrupt.

Any comment is welcome.

Marco

Contributor guide

No contributing guide indexed for this repository

Research direction

Trace attachInterrupt() and detachInterrupt() for the SAMD21 external interrupt controller, focusing on how EIC->INTFLAG is handled before an interrupt is enabled. Reproduce the provided Arduino Zero sketch and verify that an edge occurring while the interrupt is disabled does not trigger the service immediately after re-enabling it.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.