用户中断控制相关寄存器16~479用户中断相关寄存器中断使能、失能寄存器NVIC-ISER[0] ~ NVIC-ISER[15]NVIC-ICER[0] ~ NVIC-ICER[15]voidNVIC_EnableIRQ(IRQn_Type IRQn);// Enables an interruptvoidNVIC_DisableIRQ(IRQn_Type IRQn);// Disables an interrupt中断挂起、解挂寄存器NVIC-ISPR[0] ~ NVIC-ISPR[15]NVIC-ICPR[0] ~ NVIC-ICPR[15]voidNVIC_SetPendingIRQ(IRQn_Type IRQn);// Sets the pending status of an interruptvoidNVIC_ClearPendingIRQ(IRQn_Type IRQn);// Clears the pending status of an interruptuint32_tNVIC_GetPendingIRQ(IRQn_Type IRQn);// Reads the pending status of a interrupt中断活跃状态寄存器font stylecolor:rgb(51,51,51);NVIC-IABR[0] ~ NVIC-IABR[15]/fontuint32_tNVIC_GetActive(IRQn_Type IRQn);中断目标非安全寄存器NVIC-ITNS[0] ~ NVIC-ITNS[15]uint32_tNVIC_SetTargetState(IRQn_Type IRQn);// Sets interrupt as Non-secureuint32_tNVIC_ClearTargetState(IRQn_Type IRQn);// Sets interrupt as Secureuint32_tNVIC_GetTargetState(IRQn_Type IRQn);// Reads the target security state优先级寄存器NVIC-IPR[0] ~ NVIC-IPR[495]voidNVIC_SetPriority(IRQn_Type IRQn,uint32_tpriority);// Sets the priority level of an IRQ/ exceptionuint32_tNVIC_GetPriority(IRQn_Type IRQn);// Obtains the priority level of an interrupt or exception软件触发中断寄存器ArmV8-M 独有NVIC-STIRNVIC-STIR3;// Triggers IRQ #3系统异常相关SCB寄存器1~15系统异常相关寄存器中断控制和状态寄存器SCB-ICSR设置和清除系统异常的挂起状态通过读取 VECTACTIVE 字段决定当前执行的异常/中断号配置SysTick的安全状态系统中断优先级寄存器SCB-SHP[0] ~ SCB-[11]只有SVCPendSVSysTick异常的优先级能编程应用中断和复位控制寄存器SCB-AIRCR[10:8] PRIGROUP: 控制优先级分组//Set Priority GroupvoidNVIC_SetPriorityGrouping(uint32_tPriorityGroup);//Get Priority Groupuint32_tNVIC_GetPriorityGrouping(uint32_tPriorityGroup);//Encodes the priority for an interrupt with the given priority groupuint32_tNVIC_EncodePriority(uint32_tPriorityGroup,uint32_tPreemptPriority,uint32_tSubpriority);//Decodes an interrupt priority value with a given priority group to\ preemptive priority value and subpriority value.voidNVIC_DecodePriority(uint32_tPriority,uint32_tPriorityGroup,uint32_t*constpPreemptPriority,uint32_t*constpSubPriority);系统处理控制和状态寄存器SCB-SHCSR配置错误异常的使能和挂起指示系统异常是否在活跃的状态异常或中断屏蔽寄存器PRIMASK使能后所有优先级大于等于0的中断都会被屏蔽除了NMI, HardFaultvoid__enable_irq();// Clears PRIMASKvoid__disable_irq();// Sets PRIMASKvoid__set_PRIMASK(uint32_tpriMask);// Sets PRIMASK to valueuint32_t__get_PRIMASK(void);// Reads the PRIMASK valueFAULTMASK只有ArmV8-MCortex-M33和 ArmV7-MCortex-M3/M4//M7有此寄存器使能后比PRIMASK寄存器多屏蔽了HardFault中断void__enable_fault_irq(void);// Clears FAULTMASKvoid__disable_fault_irq(void);// Sets FAULTMASK to disable interruptsvoid__set_FAULTMASK(uint32_tfaultMask);// Sets FAULTMASKuint32_t__get_FAULTMASK(void);// Reads FAULTMASKBASEPRI只有ArmV8-MCortex-M33和 ArmV7-MCortex-M3/M4//M7有此寄存器设置值为优先级设置后屏蔽优先级≥设置优先级的中断void__set_BASEPRI(uint32_tpriMask);//Sets the BASEPRI registeruint32_t__get_BASEPRI(void);//Reads the BASEPRI registerVTORVector Table Offset Resgister 向量表偏移量寄存器用于向量表重定位$VectorAddress Exception Number * 4 Vector Table Offset$向量表重定向可能的用途把向量表从FLASH重定位到RAM加快访问速度向量表重定位到RAM可以动态修改向量表中的中断处理程序地址代码区存在不同的运行代码比如用户boot 和 用户APP需要各自的向量表向量表偏移后的起始地址必须是128字节的倍数M23核或256字节的倍数M33核外设中断处理流程声明一个中断处理函数函数名需要和启动代码中的中断处理函数名一样确保中断处理函数清除了中断请求如果是脉冲方式的中断请求这个操作不是必须的确保下面的软件初始化步骤设置中断优先级从NVIC中使能中断初始化外设功能外设中断使能// Set Timer0_IRQn priority level to 0xC0 (4 bit priority)NVIC_SetPriority(Timer0_IRQn,0xC0);//Shift to 0xC0 by CMSIS function// Enable Timer 0 interrupt at NVICNVIC_EnableIRQ(Timer0_IRQn);Timer0_initialize();// Device specific code to initialize timer 0...voidTimer0_Handler(void){...// timer 0 interrupt processing...// Clear timer 0 IRQ request (needed for level triggered IRQs)return;}