I have some old code for STM32F4 in which a critical section looks like
uint32_t primask;
primask = __get_PRIMASK();
__disable_irq();
/* ... Critical code ... */
__set_PRIMASK(primask);
I read in this article that the right way to restore interrupt mask after a critical section is instead:
if (!primask) {
__enable_irq();
}
The old code "seems to works", but I couldn't find any similar example, and am concerned about possible side effects. The documentation is quite ambiguous about PRIMASK register. Here an excerpt:
There are two things here that concern me:
- Bits 1-31 are "reserved", is it safe to assume that they are zeroes? If not, we should rather write
if ((primask & 1) == 0)
. - Writing 0 in Bit 0 is labeled as "No effect". It does not seem to be true (indeed previos interrupt mask is restored), but it does explain why the suggested way to leave the critical section is by
__enable_irq()
.
How should I interpret that "No effect"?
Are there any drawbacks in using __set_PRIMASK()
to leave a critical section?