Strange behaviour with InterruptIn

07 Jan 2010

Hi

I've observed some behaviour I can't explain with InterruptIn and wondered if somebody could shed some light.

The code below (and here http://mbed.org/users/mbed714/programs/gpdz5e) is an extended version of the example in the handbook. In my case I have normally open switches wired to ground so require a pull up mode and a fall rather than rise method, and I also have two switches.

#include "mbed.h"

InterruptIn button(p5);
InterruptIn button2(p6);
DigitalOut led(LED1);
DigitalOut led2(LED2);
DigitalOut flash(LED4);

void flip() {
    led = !led;
}

void flip2() {
    led2 = !led2;
}

int main() {
    button.mode(PullUp);
    button2.mode(PullUp);
    button.fall(&flip);  // attach the address of the flip function to the falling edge
    button2.fall(&flip2);
    while (1) {          // wait around, interrupts will interrupt this!
        flash = !flash;
        wait(0.25);
    }
}

When running the program, without pressing any switches LED1 is lit (LED2 is unlit). I would expect both to be unlit until a button press.

Now if I reverse the order of the calls to fall, as follows ...

    button2.fall(&flip2);
    button.fall(&flip);  // attach the address of the flip function to the falling edge

When running the program again, LED2 is lit and LED1 is unlit.

It looks like the first call to fall causes the handler to actually be entered. The second call to fall doesn't. Is this behaviour by design, can it be worked around? I don't want the handler to be called until my button is pressed and I get the real falling edge.

Thanks, Daniel

 

07 Jan 2010 . Edited: 07 Jan 2010

Hi Daniel,

We can repeat this reliably; thanks for the report. It smells like pending interrupts not getting cleared as part of attachment.

As a quick workaround, you could do:

void dummy() {}

int main() {
    button.mode(PullUp);
    button2.mode(PullUp);
  
    button.fall(&dummy); // drop outstanding interrupt with dummy function
    button.fall(&flip);  // attach proper function
    button2.fall(&flip2);

I'll raise a defect on this and we'll investigate and get it fixed. Hope this is enough for now.

Simon

10 Jan 2010

Hi Simon

Thanks for the workaround; it's for the change of display of my project Home energy monitor so nothing mission critical.

Daniel