anilgkts / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

attachInterrupt(...) should ignore pending interrupts (clear interrupt flag) #852

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have observed that attachInterrupt() does not clear the interrupt flag before 
doing the "attachment". I think it would be a good idea to do it. Otherwise, 
pending interrupts could be executed immediatly after calling 
attachInterrupt(). Although we could discuss a lot about it, I would say 95% of 
cases this would not be the desired behavior, specially taking into account the 
average Arduino user.  

What steps will reproduce the problem?
1. attach and detach an interrupt (i.e:attachInterrupt(0, miFunc, CHANGE), then 
detachInterrupt(0))
2. Toggle the INT input pin (pin 2 in our example)
3. After that, use attachInterrupt(0, miFunc, CHANGE) again to execute myFunc 
on pin 2 changes

What is the expected output? What do you see instead?
This could be discussed, but normally you would expect that the function myFunc 
is not called until you toggle the input pin after attaching the interrupt, but 
as the interrupt flag has been set by the previous toggle, the function will be 
called inmediatly after the "attachment"

What version of the Arduino software are you using? On what operating
system?  Which Arduino board are you using?
I think this will happend on every version and board

Please provide any additional information below.

This can be easily done in the file WInterrupts.c, clearing the interrupt flag 
before the mask activation, i.e.

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
  ...
  ...
    switch (interruptNum) {
#if defined(EICRA) && defined(EICRB) && defined(EIMSK)
    case 2:
      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
      EIFR |= (1 << INT0);  <<<<<<<<<<<<<<<<<<<<<<<<<<  possible pending interrupt clearing
      EIMSK |= (1 << INT0);
      break;
  ... and so on
  ...

Other posibility would be to reprogram interrupts to be no active (but in any 
case, interrupt flag should be cleared!)

Thanks :-)

Yago

Original issue reported on code.google.com by yago.tor...@gmail.com on 8 Mar 2012 at 6:29

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 11 Mar 2012 at 4:59

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 11 Mar 2012 at 5:17