Pages: [1]   Go Down
Author Topic: SOLVED: Interrupt queuing  (Read 866 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've searched for this, but cannot find a definative answer.

Say for example, a rising interrupt is used to increment a counter, if in the loop of a code the interrupt is detached to prevent another interrupt disrupting a calculation, then reattached - what happens if that interrupt pin goes high during the period it is detached?

When it becomes reattached, is there a 'pending' or 'queued' interrupt that is then serviced? If not, is there any way to do this?
« Last Edit: October 30, 2012, 01:47:09 pm by jtw11 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, the interrupts are queued. But, only one of each kind. If there is more than one of a given kind, the extra(s) are ignored.
Logged

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Say for example, a rising interrupt is used to increment a counter, if in the loop of a code the interrupt is detached to prevent another interrupt disrupting a calculation, then reattached - what happens if that interrupt pin goes high during the period it is detached?

The event is lost. There's no per-event queueing.

Quote
If not, is there any way to do this?

Write your interrupt handler(s) to be as small & fast as possible so these conditions won't occur. Set flags, push entries on a private stack or make entries in an array, etc. inside the interrupt handler to log the event, then have the main loop process the events as it can.

Good luck!
Logged

http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A number of libraries make a point of clearing pending interrupt flags before they start, attachInterrupt probably does this which is what might be causing your confusion.

You do not need to attach/detach the interrupt, you can simply disable interrupts temporarily. Any interrupts which happen while disabled will be held in a pending state and processed when interrupts are re enabled, this is standard practice for accessing shared variables etc.

Code:

loop()
{
..
..
..
nointerrupts(); // disable interrupts while we do something sensitive like access a shared variable which is also accessed in the interrupt
..
..
..
interrupts(); // reenable interrupts, any that occured between nointerrupts and now, will be serviced now.
..
..
}

Duane B

rcarduino.blogspot.com
Logged


UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks very much, I will use the interrupts(); method. However, will there indeed only be 1 queued interrupt allowed, or more? There seems to be conflicting replies.

Yes, the interrupts are queued. But, only one of each kind. If there is more than one of a given kind, the extra(s) are ignored.

You do not need to attach/detach the interrupt, you can simply disable interrupts temporarily. Any interrupts which happen while disabled will be held in a pending state and processed when interrupts are re enabled, this is standard practice for accessing shared variables etc.
« Last Edit: October 30, 2012, 01:13:34 pm by jtw11 » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When it becomes reattached, is there a 'pending' or 'queued' interrupt that is then serviced?

Normally, no.  attachInterrupt has (had?) a bug that works the way you've described.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks very much, I will use the interrupts(); method. However, will there indeed only be 1 queued interrupt allowed, or more?

Each interrupt source has a queue depth of one.  (Except in one reasonable case where the depth is zero.)
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Each interrupt source has a queue depth of one.  (Except in one reasonable case where the depth is zero.)


Interrupt source being that specific interrupt pin?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Not exactly.  For a given pin, there could be several interrupt sources.  All pins support "pin change" interrupts; that is one source.  A few pins support "external interrupts"; that is another source.  (attachInterrupt uses "external interrupts")
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see, so if there were two external interrupts, only one queue event would be logged even though there are 2 requesting a log (if for example, they were)?

Also, what is a pin change interrupt? Is that adding another interrupt pin using the PinChangeInt library?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12739
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I see, so if there were two external interrupts, only one queue event would be logged even though there are 2 requesting a log (if for example, they were)?

No.  Each is a separate source.

Quote
Also, what is a pin change interrupt?

It's similar to CHANGE available for external interrupts (attachInterrupt( pin, CHANGE );).

Quote
Is that adding another interrupt pin using the PinChangeInt library?

Yes.  But only for CHANGE.  LOW, RISING, and FALLING are not available (but can be simulated and are simulated in one of the pin-change interrupt libraries).
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see - once again, many thanks for all the quick replies.
Logged

Pages: [1]   Go Up
Jump to: