Ah yes, well this brings us to the nub of the problem. It isn't so much that an ISR can disable interrupts. According to the data sheet (page 14):
When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled. The user software can write logic one to the I-bit to enable nested interrupts. All enabled interrupts can then interrupt the current interrupt routine. The I-bit is automatically set when a Return from Interrupt instruction – RETI – is executed.
Interrupts are already disabled in an ISR, according to the above. And an ISR can re-enable them, to allow nested interrupts. However when the ISR returns interrupts are automatically enabled.
So no, an ISR cannot disable interrupts. And it would be a very badly behaved ISR that even attempted to do so. Bearing in mind an ISR can be called any time, it should leave things how it found them (hence it saves the status register and restores it). However since the processor clears the interrupt flag (so the ISR cannot itself be interrupted) the processor has to restore it on the RETI instruction. And clearly, interrupts were enabled when the ISR was called.
So your hypothetical situation of the interrupt flag being cleared, by an interrupt handler, just cannot happen.
I'll address your question about the I2C slave in my next post, and if the code is wrong, I'll be the first to admit it ... after all we all want reliable code.
sbr_:
You don't want the receiveEvent() (which is called by an ISR) routine disabled during printing in loop(), in order not to lose data (after all, the circular buffer serves that purpose). 'inpoint' and 'outpoint' are 16 bit variables, and 'inpoint' could change during the 'if (outpoint == inpoint)' test. No?
It looks like you are right in this case, and this confirms what I said above about having a flag if you change data of more than 8 bits. There is a chance (small, but there it is) that the high-order byte will be read, and then the ISR will kick in, and then the low-order byte be read. So, good point.
Probably loop should be changed as follows:
void loop (void)
{
// insertion and removal point the same, nothing there
noInterrupts (); // atomic test of a 16-bit variable
if (outpoint != inpoint)
{
interrupts ();
return;
}
interrupts ();
// display anything found in the circular buffer
Serial.print (buf [outpoint++]);
noInterrupts (); // atomic test of a 16-bit variable
if (outpoint >= sizeof buf)
outpoint = 0; // wrap around
interrupts ();
} // end of loop
That gives you the two atomic tests. You don't need to worry on the Serial.print because the ISR doesn't change outpoint.
In this case we don't need to worry about saving and restoring the SREG because we know we have interrupts enabled. Something like millis () does, because it doesn't know. However millis(), in version 0022 at least, doesn't use the ATOMIC_BLOCK(ATOMIC_RESTORESTATE) construct because I don't think it is needed.
I've amended my forum posts to reflect the more correct code, in the interests of encouraging the use of code that works 100% of the time.
Hm. Imagine the situation when the main loop() is interrupted during updating outpoint, then the ISR will get an inconsistent value. So I guess I disagree with you on this one.
I've amended my forum posts to reflect the more correct code, in the interests of encouraging the use of code that works 100% of the time.
Cool!
BTW, apologies to the OP, my intent was not to hijack the thread in any way.
sbr_:
Hm. Imagine the situation when the main loop() is interrupted during updating outpoint, then the ISR will get an inconsistent value. So I guess I disagree with you on this one.
You are right of course. Amended code follows:
void loop (void)
{
// insertion and removal point the same, nothing there
noInterrupts (); // atomic test of a 16-bit variable
if (outpoint != inpoint)
{
interrupts ();
return;
}
interrupts ();
// display anything found in the circular buffer
Serial.print (buf [outpoint]);
noInterrupts ();
if (++outpoint >= sizeof buf)
outpoint = 0; // wrap around
interrupts ();
} // end of loop
It just goes to show how easy it is to make subtle mistakes with ISRs. A good code review is very helpful, plus you should really pump "torture" test data through to try to show up such problems.
nickgammon:
Ah yes, well this brings us to the nub of the problem. It isn't so much that an ISR can disable interrupts. According to the data sheet (page 14):
Interrupts are already disabled in an ISR, according to the above. And an ISR can re-enable them, to allow nested interrupts. However when the ISR returns interrupts are automatically enabled.
so if understand you correct , no sense to make cli/sei within ISR ?
ISR(){ //before entering to ISR is calling cli();
..
} //after exiting from ISR is calling sei();
BUT , I noticed Nick do not enter anymore to community
so could some one explain please why
here Nick says that using detach within ISR is good practice:
nickgammon:
Is it safe to use detachInterrupt(X) at the end of ISR?
Yes it is safe, and indeed good practice in some cases. To not detach the interrupt from within the ISR leaves you open to the program running extremely slowly.
BUT here he says:
"However we should be cautious about putting the detachInterrupt actually inside the "wake" ISR, because it is possible the interrupt might fire between attaching the interrupt and going to sleep, in which case we would never wake up."
SO , good practice or NOT good practice to use detach within ISR ?
I'm confused totally
alexblade:
BUT here he say "However we should be cautious about putting the detachInterrupt actually inside the "wake" ISR, "
I got this
he talking about this scenario, I added commentary for clarification
// interrupt service routine in sleep mode
void wake ()
{
sleep_disable (); // first thing after waking from sleep:
detachInterrupt (digitalPinToInterrupt (2)); // stop LOW interrupt on D2
//B) Here we are disable interrupt, so it never fire anymore and go to C)
} // end of wake
void sleepNow ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable (); // enables the sleep bit in the mcucr register
attachInterrupt (digitalPinToInterrupt (2), wake, LOW); // wake up on low level on D2
//A) Nick saying at this point can fire interrupt and we go to B)
//C) Now we go to sleep w/o interrupt and we would never wake up
sleep_mode (); // here the device is actually put to sleep!!
} // end of sleepNow
and he added
[EDIT] (14th July 2013)
Correction: code to fix this issue.
So we can and should detach INT within ISR just need to do this correctly