1.1 Priority Grouping
First, let's explain the meaning of manual NVIC to the scale by identifying:
Core-M4 core supports up to 256 programmable priority. With 8 bits to represent priority level priority level ,, divided into eight groups, namely, group 0 to group 7, as shown in Table 1.0 (8 Range: 0 to 255)
Table 1.1 Interrupt packet to explain
Group No | Preemption priority | Priority response |
0 | Express preemption priority high seven 0~127 If the CPU interrupt packet selection is "group 0 ", the Preemption priority may be set to 0 to 127. Note: The smaller the number, the higher the level. |
Response represents the lowest priority 0~1 Response priority may be set to 0 or 1 , Note: The smaller the number the higher the level. |
1 | Express preemption priority high six If the CPU interrupt packet is selected as "group 1" , Preemption priority may be set to 0 to 63 |
Two represent the response of low priority Response priority can be set to 0 to 3, |
2 | High 5 bits representing preemption priority If the CPU interrupt grouping selected as "group 2" , Preemption priority may be set to 0 to 31 |
Low response indicates three priority Response priority can be set to 0 to 7 |
3 | High 4 bits representing preemption priority If the CPU interrupt grouping selected as "group 2" , Preemption priority may be set to 0 to 15 |
The lower four bits represent the response priority Response priority can be set to 0 to 15 |
4 | High 3 represents preemption priority bits If the CPU interrupt grouping selected as "group 2" , Preemption priority may be set to 0 to 7 |
Five-digit response indicates low priority Response priority can be set to 0 to 31 |
5 | High 2 bits representing preemption priority If the CPU interrupt grouping selected as "group 2" , Preemption priority may be set to 0 to 3, |
Low . 6 indicates the response priority Response priority can be set to 0 to 63 |
6 | It represents the highest level preemption priority If the CPU interrupt grouping selected as "group 2" , Preemption priority may be set to 0 to 1 |
Low . 7 indicates the priority of the response Response priority can be set to 0 to 127 |
7 | . 8 indicates the priority of the response Response priority can be set to 0 to 255 |
If the image of a map to represent, then Figure 1.1-8 bit interrupt grouping:
1.1 8 interrupt packet
stm32 order to save material costs, which are not fully using 8 bits it ignores the low 4 bits. Below 1-2
High packets 1.2
表1.2 裁剪分组讲解
组号 | 中断优先级分组说明 | 抢占优先级等级范围 | 响应优先级等级范围 |
3 | 所有4位用于指定抢占优先级 | 0~15 | 不可设置 |
4 | 最高3位用于指定抢占优先级,最低1位用于指定响应优先级 | 0~7 | 0~1 |
5 | 最高2位用于指定抢占优先级,最低2位用于指定响应优先级 | 0~3 | 0~3 |
6 | 最高1位用于指定抢占优先级,低3位用于指定响应优先级 | 0~1 | 0~7 |
7 | 所有4位用于指定响应优先级 | 不可设置 | 0~15 |
1.2 NVIC中断中断控制器相关函数
NVIC分组设置
void NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
位置:core_cm4.h的1453行
作用:设置优先级分组。
参数:PriorityGroup优先级分组组号
举例:STM32的优先级分组设置为组5,则对应的代码如下:NVIC_SetPriorityGrouping(5);
NVIC具体中断优先级编码
uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority)
作用:设置抢占优先级和响应优先级的级别
位置:core_cm4.h的1610行
参数:PriorityGroup优先级分组组号;PreemptPriority:抢占优先级;SubPriority:响应优先级
返回值:32位的编码值,编码值用于中断优先级设置
举例:优先级分组选择为组5,抢占优先级为2,响应优先级为2,代码如下:
u32 prio;
prio = NVIC_EncodePriority(5,2,2);
NVIC中断优先级设置
void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
作用:将优先级分组情况以及抢占优先级和响应优先级设置到响应的中断。
参数:
IRQn :中断通道编号。
priority:是NVIC_EncodePriority函数的返回值
举例:设置串口1全局中断的优先级分组选择为组5,抢占优先级为2,响应优先级为3
对应的代码:
u32 prio;
prio = NVIC_EncodePriority(5,2,3);
NVIC_SetPriority(37,prio);
也可以写成NVIC_SetPriority(USART1_IRQn,prio);
NVIC中断使能
void NVIC_EnableIRQ(IRQn_Type IRQn)
作用:使能中断通道
参数:IRQn :中断通道编号。
举例:使能串口1全局中断,NVIC_EnableIRQ(37); 可以写成NVIC_EnableIRQ(USART1_IRQn);
NVIC中断禁能
void NVIC_DisableIRQ(IRQn_Type IRQn)
作用:禁止中断通道。
参数:IRQn :中断通道编号。
举例:禁止串口1全局中断,NVIC_DisableIRQ(37);
1.3 实例代码之串口中断:
#include "stm32f4xx.h" //PA9 ----TXD--发送数据 (站在芯片角度) //PA10 --- RXD--接收收据 (站在芯片角度) void Usart1_Init(u32 baudRate) { u32 prio; float USARTDIV; u16 Mantissa; //整数 u8 Fraction; //小数 /****使能GPIOA的时钟***/ RCC->AHB1ENR |= 1<<0; /****配置PA10为复用功能+上拉***/ GPIOA->MODER |= 2<<20;//复用功能 GPIOA->PUPDR |= 1<<20;//上拉 /****配置PA9位复用功能+推挽***/ GPIOA->MODER |= 2<<18;//复用功能 GPIOA->OTYPER &=~(1<<9);//推挽 /****PA9选择复用功能7,TXD***/ GPIOA->AFR[1] |= 7<<4 ; /****PA10选择复用功能7,RXD***/ GPIOA->AFR[1] |= 7<<8 ; /****使能串口1的时钟***/ RCC->APB2ENR |= 1<<4; /****设置数据帧格式***/ USART1->CR1 |= 1<<15;//OVER8设置为1 USART1->CR1 &=~(1<<12);//将串口1的数据帧设置为“1 起始位, 8 数据位, n 停止位 ” USART1->CR2 &=~(3<<12);//1位停止位 USART1->CR1 &=~(1<<10);//禁止奇偶校验 /****设置波特率***/ //USARTDIV = fCK/8*(2- OVER8 )/TxRx 波特率 USARTDIV = (float)84000000/8/baudRate; Mantissa = (int)USARTDIV; Fraction = (u8)((USARTDIV-Mantissa)*16); USART1->BRR = Mantissa<<4 | Fraction; USART1->CR1 |= 1<<5; //使能串口接收中断 NVIC_SetPriorityGrouping(5); //设置优先级分组设置为组5,注意:一个工程只能有一个分组 prio = NVIC_EncodePriority(5,2,2); //组5,抢占优先级为2,响应优先级为2 NVIC_SetPriority(USART1_IRQn,prio); NVIC_EnableIRQ(USART1_IRQn); //使能串口1中断通道USART1_IRQn==37 也可以用37表示串口1的通道编号 USART1->CR1 |= 1<<3;//使能发送器 USART1->CR1 |= 1<<2;//使能接收器 USART1->CR1 |= 1<<13;//使能串口1 } //中断服务程序代码,在接收到一个字节是会进入串口中断服务函数 void USART1_IRQHandler(void) { u8 data; if(USART1->SR&(1<<5)) { data = USART1->DR; /*******接收到一个字节原样回发*******/ USART1->DR = data; while(!(USART1->SR&(1<<6))); USART1->SR &=~(1<<6); //清零 } }