stm32的中断嵌套优先级
程序员文章站
2022-04-02 07:54:58
...
stm32的中断嵌套优先级
stm32NVIC优先级分组
中断优先级配置寄存器NVIC_IPRx描述抢占优先级与响应优先级 ,在Cortex_m3中,该寄存器被定义为8bit,考虑stm32允许具有较少中断源时使用较少的寄存器位指定中断源的优先级,因此把指定中断优先级的寄存器位阉割为4位(AIRCR高四位)。
具体分组:
- NVIC_PriorityGroup_0: 0 bits for pre-emption priority 4 bits for subpriority
- NVIC_PriorityGroup_1: 1 bits for pre-emption priority 3 bits for subpriority
- NVIC_PriorityGroup_2: 2 bits for pre-emption priority 2 bits for subpriority
- NVIC_PriorityGroup_3: 3 bits for pre-emption priority 1 bits for subpriority
- NVIC_PriorityGroup_4: 4 bits for pre-emption priority 0 bits for subpriority
组别’,‘抢占优先级’,‘响应优先级’?
组别优先顺序:
NVIC_PriorityGroup_0->NVIC_PriorityGroup_1->NVIC_PriorityGroup_2->NVIC_PriorityGroup_3->NVIC_PriorityGroup_4
三个概念之间的‘先后’关系
‘组优先’>’抢占优先’>’响应优先’
抢占优先与响应优先的联系
1. 在同一个组别优先级下,不同的抢占优先级间,高优先级的可以打断低优先级。
2. 抢占优先级相同,不能抢占对方(同抢占优先级不存在嵌套),在两者同时响应的时候,高响应优先级的先响应。
3. 抢占优先级与响应优先级均相同的情况下,同时发生时,根据具体的硬件编号先后执行。
典型应用配置
static void NVIC_Configuration1(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void NVIC_Configuration2(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
在第0组中,4个位均配置响应优先级,统一组别且相同的抢占优先级的情况下,USART2的响应优先级高于USART3,故两者的同时发生时先响应USART2。这样设置还有一个好处就是两者在正常运行时不会存在相互干扰。
上一篇: 看大数据平台如何打造餐饮业务一体化?