On Cortex M3, how can a piece of code determine whether interrupts are enabled, that is, the status of the I bit in the Program Status Register (as manipulated by cpsid and cpsie)? On older ARMs, I was able to read the cpsr register, but that seems to no longer be possible.
I need this information because I have a function which is called both from main and from interrupts, and it needs to perform a certain operation atomically, while interrupts are disabled. Essentially:
bool interrupts_enabled = InterruptsEnabled();
if (interrupts_enabled) {
__disable_irq();
}
Critical Code;
if (interrupts_enabled) {
__enable_irq();
}
Additionally, I'd like to know how the I flag behaves when an interrupt is executed. Does execution of an interrupt ever set the I flag, as in to prevent nesting? Is interrupt nesting enabled by default, and how can I prevent nesting?