What is the difference between PRIMASK, FAULTMASK and BASEPRI?
Short answer: All three are special registers that mask (block) exceptions. PRIMASK = 1 blocks every exception with configurable priority, leaving only NMI and HardFault; __disable_irq() sets it. FAULTMASK = 1 also blocks HardFault (only NMI remains); it is meant for fault handlers and is cleared automatically on exception return. BASEPRI = n blocks only exceptions whose priority value is ≥ n, so more urgent interrupts still run.
| Register | Blocks | Available on | Typical use |
|---|---|---|---|
| PRIMASK | all configurable-priority exceptions | all Cortex-M | short critical sections, __disable_irq() |
| FAULTMASK | the above + HardFault | Armv7-M, Armv8-M Mainline | fault handlers that must not be interrupted; running at priority −1 |
| BASEPRI | priorities numerically ≥ BASEPRI (0 = no masking) | Armv7-M, Armv8-M Mainline | RTOS critical sections that leave high-priority interrupts running |
/* RTOS-style critical section that leaves priorities 0-4 running */
uint32_t old = __get_BASEPRI();
__set_BASEPRI(5u << (8u - __NVIC_PRIO_BITS)); /* mask priority values >= 5 */
/* ... critical work ... */
__set_BASEPRI(old);
__set_BASEPRI_MAX() (the BASEPRI_MAX alias) only raises the masking level, never lowers it, which makes nested use safe.
Why BASEPRI matters: with PRIMASK, every critical section adds latency to every interrupt. With BASEPRI, the kernel's critical sections do not delay the most time-critical interrupts at all (e.g. a motor-control PWM interrupt), as long as those interrupts do not call kernel APIs.
Armv6-M (Cortex-M0/M0+) has only PRIMASK, so RTOS critical sections there disable all interrupts.
Other notes
- Masking also delays the exception, it does not lose it: pending interrupts are taken when the mask is lifted.
- On Armv8-M with TrustZone, each security state has its own PRIMASK/FAULTMASK/BASEPRI, and Secure code can optionally deprioritise Non-secure interrupts.