A question for the interrupt handling experts.
I'm using the optimised digitalWrite library here: Google Code Archive - Long-term storage for Google Code Project Hosting. to speed things up (see extract below), and I understand why it is necessary to disable interrupts (cli()) when updating registers above 32 or when using variables.
However, what I don't understand is when the interrupts get re-enabled after the function call. I don't see a call to sei() or similar, but without that then surely the main program will then be running with interrupts disabled. But I know that can't be true because my program works and does things that need interrupts enabled.
Does the return re-enable interrupts? Any help much appreciated (and as a by-the-way, why the "while(0)" loop, which will only ever execute once?).
#define digitalWrite_implementation(pin, value)
do {
uint8_t oldSREG;
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg = portOutputRegister(port);
if (!__builtin_constant_p(pin) || registerWriteNeedsLocking(reg)) {
oldSREG = SREG;
cli();
}
if (value == LOW) {
*reg &= ~bit;
} else {
*reg |= bit;
}
if (!__builtin_constant_p(pin) || registerWriteNeedsLocking(reg)) {
SREG = oldSREG;
}
} while(0)