STM32 NVIC
在stm32中是要配置nvic的。何为nvic,对于我这样的初学者来说,直观感受就是在设置为中断后 还需要配置 中断的优先级nvic就是搞这个的、
那么具体的需要配置些什么那?
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
}
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键WK_UP所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
STM32有43个channel的settable的中断源;AIRC(Application Interrupt and Reset Register)寄存器中有用于指定优先级的4 bits。这4个bits用于分配preemption优先级和sub优先级,在STM32的固件库中定义如下
#define NVIC_PriorityGroup_0 ((u32)0x700)
#define NVIC_PriorityGroup_1 ((u32)0x600)
#define NVIC_PriorityGroup_2 ((u32)0x500)
#define NVIC_PriorityGroup_3 ((u32)0x400)
#define NVIC_PriorityGroup_4 ((u32)0x300)
每个工程只能有一种分组方式:
按NVIC_PriorityGroup_0来分组:抢占优先级为1个,子优先级为16个;
按NVIC_PriorityGroup_1来分组:抢占优先级为2个,子优先级为8个;
按NVIC_PriorityGroup_2来分组:抢占优先级为4个,子优先级为4个;
按NVIC_PriorityGroup_3来分组:抢占优先级为8个,子优先级为2个;
按NVIC_PriorityGroup_0来分组:抢占优先级为16个,子优先级为1个;
抢占优先级高的中断可以打断抢占优先级低的中断;抢占优先级相同时比较响应优先级(子优先级)。
先设定分组方式
例:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //按NVIC_PriorityGroup_4来分组
此时,有一中断EXTI0_IRQChannel,你指定他为抢占优先级8,则
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8; // 指定抢占式优先级别,可取0-15
又有1中断SPI1_IRQChannel,设定如下
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先级别,可取0-15
则SPI1_IRQChannel的抢占优先级高,EXTI0_IRQChannel做事的时候可以打断(嵌套)
又:若分组方式为:NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); //按NVIC_PriorityGroup_3来分组
此时就有8个抢占优先级,2个子优先级;
EXTI0_IRQChannel中断,你指定他为抢占优先级3,则:
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 指定抢占式优先级别,可取0-7
还需要指定他的阶层:
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定响应优先级别,可取0-1
又有TIM2_IRQn中断,设定如下:
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;// 指定抢占式优先级别,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // 指定响应优先级别,可取0-1
由于抢占优先级相同,响应优先级EXTI0_IRQChannel中断高于TIM2_IRQn中断,所以EXTI0_IRQChannel可以打断TIM2_IRQn;
还有一USB_LP_CAN1_RX0_IRQn,设定如下:
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; //CAN1 RX0中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级别0,可取0-1
USB_LP_CAN1_RX0_IRQn优先级最高,前两中断发生时,它都可以打断。
附:
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);//一个工程中只能使用一种分组方式
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; //CAN1 RX0中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;// 指定抢占式优先级别,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // 指定响应优先级别,可取0-1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 指定抢占式优先级别,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定响应优先级别,可取0-1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}