STM32有43个channel的settable的中止源;AIRC(Application Interruptand Reset Register)寄存器中有用于指定优先级的4 bits。这4个bits用于分配preemption优先级和sub优先级,在STM32的固件库中界说如下
/* Preemption Priority Group————————————————-*/
#define NVIC_PriorityGroup_0((u32)0x700) /* 0 bits for pre-emption priority
4 bits for subpriority */
#defineNVIC_PriorityGroup_1((u32)0x600) /* 1 bits for pre-emption priority
3 bits for subpriority */
#defineNVIC_PriorityGroup_2((u32)0x500) /* 2 bits for pre-emption priority
2 bits for subpriority */
#define NVIC_PriorityGroup_3((u32)0x400) /* 3 bits for pre-emption priority
1 bits for subpriority */
#defineNVIC_PriorityGroup_4((u32)0x300) /* 4 bits for pre-emption priority
0 bits for subpriority */
NVIC_PriorityGroup |
先占优先级取值规模 (PreemptionPriority取值) |
次占优先级取值规模 (SubPriority取值) |
NVIC_PriorityGroup_0 |
0(只看SubPriority取值) |
0到15 |
NVIC_PriorityGroup_1 |
0到1 |
0到7 |
NVIC_PriorityGroup_2 |
0到3 |
0到3 |
NVIC_PriorityGroup_3 |
0到7 |
0到1 |
NVIC_PriorityGroup_4 |
0到15 |
0(只看PreemptionPriority取值) |
阐明一下:
1、所设定的值中,值越小,中止优先级越高。
2、假如选用NVIC_PriorityGroup_0,则函数中NVIC_IRQChannelPreemptionPriority中设置的值对中止通道优先级不发生任何影响。假如选用NVIC_PriorityGroup_4,则函数中NVIC_IRQChannelSubPriority中设置的值对中止通道优先级不发生任何影响。
如下所示:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //选用组0
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//这儿这样设置无效 NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
如上所示,设置TIM3_IRQn的NVIC_IRQChannelPreemptionPriority为2是过错的,由于从表中能够看到,选组0时NVIC_IRQChannelPreemptionPriority的值只能为0。
同理,假如选用NVIC_PriorityGroup_4,那么NVIC_IRQChannelSubPriority的值只能为0。
3、一般装备NVIC代码如下
voidNVIC_Configuration(void){
NVIC_InitTypeDef NVIC_InitStructure;
/*初始化向量表取值*/
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/*挑选所运用的要运用的组别*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //这句代码在整个程序中只能写一次
//设定定时器中止先占优先级为2,低于串口中止优先级
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//设定串口中止先占优先级为0,为最高优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
下面是网上找的一些了解,也挺不错的!
形象化的了解是:
你是天主,
造了43个人,这么多人要分社会阶级和社会阶级了;
由于“阶级”的词性比较重;”阶级”比较中性,
所以preemption优先级->阶级;每个阶级内部,有一些阶级,sub优先级->阶级;
假如依照NVIC_PriorityGroup_4这么分,就分为了16个阶级(1个阶级便是1个preemption优先级),0个阶级;高阶级的人,能够打断低阶级的正在干事的人(嵌套),最多能够完结1个中止和15级嵌套。
每个阶级(每个preemption优先级),你来指定这43人中,谁进入该阶级;一个人叫EXTI0_IRQChannel,你指定他进入“阶级8”,则
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8; // 指定抢占式优先等级1,可取0-15
别的,在同一阶级内部,一个人在干事的时分,别的一个人不能打断他;(preemption优先等级相同的中止源之间没有嵌套联系)
还有,假如他们两个一起想干事,由于没有阶级,那么就依据Vector table中的物理排序,让排名靠前的人去做;
又有1个人SPI1_IRQChannel,设定如下
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 指定抢占式优先等级1,可取0-15
SPI1_IRQChannel的阶级高,EXTI0_IRQChannel干事的时分能够打断(嵌套)。
假如依照NVIC_PriorityGroup_3这么分,就分为了8个阶级(1个阶级是1个preemption优先级),每个阶级内有2个阶级(sub优先级);高阶级的人,能够打断低阶级的正在干事的人(嵌套),最多能够完结1个中止和7级嵌套。
每个阶级(每个preemption优先级),你来指定这43人中,谁进入该阶级;一个人叫EXTI0_IRQChannel,你指定他进入“阶级3”,则:
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; // 指定抢占式优先等级1,可取0-7
还需要指定他的阶级:
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 指定呼应优先等级0,可取0-1
还有1个人叫EXTI9_5_IRQChannel,他的阶级和阶级设定如下
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; // 指定抢占式优先等级0,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 指定呼应优先等级1
那么这两个人是同一阶级的兄弟,一个人在干事的时分,别的一个人不能打断他;(preemption优先等级相同的中止源之间没有嵌套联系)
假如他们两个一起想干事,由于前者的阶级高,所以前者优先。
还有一个人叫USART1_IRQChannel,他的阶级和阶级设定如下
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 指定抢占式优先等级0,可取0-7
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 指定呼应优先等级1
USART1_IRQChannel的优先级最高,当前面两个人干事的时分,他都能够打断(嵌套)。
以下的类推。