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:
//here some code
//some more code!! here we expect that the interrupts are still disabled, but they are not
//here some code
A mechanism with counting should be better added to those defines.
I disagree. Disable and enable interrupts in the correct way.
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.
INTERRUPT_DISABLED = (uint32_t)0,
INTERRUPT_ENABLED = (uint32_t)1
static InterruptState_t CurrentCoreInterruptState = INTERRUPT_ENABLED;
void interrupts(uint32_t flags)
if(flags == INTERRUPT_ENABLED)
CurrentCoreInterruptState = INTERRUPT_ENABLED;
InterruptState_t previousCoreInterruptState = CurrentCoreInterruptState;
if(CurrentCoreInterruptState == INTERRUPT_ENABLED)
CurrentCoreInterruptState = INTERRUPT_DISABLED;
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.
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.