# NVIC：中断优先级分组在主代码与中断服务程序中不一致

- **ID:** `embedded/nvic-priority-group-mismatch`
- **领域:** embedded
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

NVIC优先级分组（如组优先级与子优先级位数分配）在中断处理函数与主初始化路径中配置不一致，导致中断嵌套时行为未定义。

## 解决方案

1. ```
   Ensure NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4) is called exactly once at the start of main(), before any peripheral initialization. Example: void main(void) { NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); /* 4 bits for pre-emption, 0 for subpriority */ HAL_Init(); SystemClock_Config(); ... }
   ```
2. ```
   Use a static assert to check that all modules use the same grouping. For example, in FreeRTOS, ensure configPRIO_BITS matches the hardware grouping.
   ```

## 无效尝试

- **** — Disabling all interrupts globally does not fix the grouping mismatch; interrupts still fire with inconsistent priority logic. (60% 失败率)
- **** — Changing the grouping value inside an ISR leads to immediate hard fault or priority inversion because the NVIC state is inconsistent. (80% 失败率)
