Recently, I discovered a phenomenon: in the AUTOSAR OS, if an interrupt occurs after task scheduling, the BASEPRI is used to control the enabling and disabling of interrupts. If it occurs before task scheduling, PRIMASK is used for control.
This article introduces the differences between these two.
1. Differences
<span>PRIMASK</span>: All or nothing switch. It either masks all maskable interrupts or none at all.<span>BASEPRI</span>: A configurable “threshold”. It only masks interrupts with a priority lower than or equal to a specific value, while allowing higher priority interrupts to continue responding.
2. Detailed Comparison
Comparison table:
| Feature | <span>PRIMASK</span> |
<span>BASEPRI</span> |
|---|---|---|
| Chinese Name | Main Interrupt Mask Register | Base Priority Mask Register |
| Function | Disables all maskable exceptions (usually interrupts) | Disables interrupts with priority greater than or equal to a certain threshold |
| Behavior Metaphor | Master Switch | Adjustable Dam |
| Masking Granularity | Coarse. Only two states: on and off. | Fine. Can set a specific priority level. |
| Usage Scenario | Protect very short, critical time periods that must not be interrupted. | Protect a longer code section but allow urgent high-priority interrupts to interrupt. |
| How to Enable Masking | <span>CPSID I</span> (assembly) or <span>__disable_irq()</span> (C) |
Write a priority value (e.g., <span>0x50</span>) |
| How to Disable Masking | <span>CPSIE I</span> (assembly) or <span>__enable_irq()</span> (C) |
Write <span>0</span> |
| Value Representation | 0 = Interrupt enabled 1 = Interrupt disabled | 0 = Masking function off non-0 (e.g., 0x50) = Mask all interrupts with priority values ≥ 0x50 |