Skip to content

External interrupts flags are not cleared before enabling the interrupt #532

Open
@Sulymarco

Description

@Sulymarco

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions