enabling/disabling interrupts

Hi,

I encountered a possible error when disabling/enabling interrupts via 'interrupts' and 'noInterrupts'.
They are defined as:
#define interrupts() sei()
#define noInterrupts() cli()

If you have nested disabling/enabling, then this coding is wrong. eg:
void call2(void)
{
noInterrupts();
//here some code
call1();
//some more code!! here we expect that the interrupts are still disabled, but they are not
interrupts();
}

void call1(void)
{
noInterrupts();
//here some code
interrupts();
}

A mechanism with counting should be better added to those defines.

I disagree. Disable and enable interrupts in the correct way.

http://gammon.com.au/interrupts

Hi Whandall,

I agree with your comment. But this implies that everybody is doing that. If it would have been in the driver, then you are always 100% sure and you don’t need to take care…sei() and cli() shall only be called in those functions.

Like this:
typedef enum
{
INTERRUPT_DISABLED = (uint32_t)0,
INTERRUPT_ENABLED = (uint32_t)1
} InterruptState_t;
static InterruptState_t CurrentCoreInterruptState = INTERRUPT_ENABLED;

void interrupts(uint32_t flags)
{
if(flags == INTERRUPT_ENABLED)
{
sei();

CurrentCoreInterruptState = INTERRUPT_ENABLED;
}
}
uint32_t noInterrupts(void)

{
InterruptState_t previousCoreInterruptState = CurrentCoreInterruptState;

if(CurrentCoreInterruptState == INTERRUPT_ENABLED)
{
cli();

CurrentCoreInterruptState = INTERRUPT_DISABLED;
}

return((uint32_t)previousCoreInterruptState);
}

I much prefer the simplest possible system that does not waste CPU cycles doing stuff that can be done at the program design stage with a little care and thought.

Complex solutions may be appropriate on a PC with bucket-loads of memory and GHz CPU rates.

...R

djphoenix:
But this implies that everybody is doing that.

That would only be relevant to people who call routines with disabled interrupts without making shure that these routines are designed to work in that context.

Keeping the time with interrupts disabled minimal is a primary goal, at least for me.