
用户中断控制相关寄存器
- 16~479用户中断
- 相关寄存器

中断使能、失能寄存器
NVIC->ISER[0] ~ NVIC->ISER[15]NVIC->ICER[0] ~ NVIC->ICER[15]
c
void NVIC_EnableIRQ (IRQn_Type IRQn); // Enables an interrupt
void NVIC_DisableIRQ (IRQn_Type IRQn); // Disables an interrupt
中断挂起、解挂寄存器
NVIC->ISPR[0] ~ NVIC->ISPR[15]NVIC->ICPR[0] ~ NVIC->ICPR[15]
c
void NVIC_SetPendingIRQ(IRQn_Type IRQn); // Sets the pending status of an interrupt
void NVIC_ClearPendingIRQ(IRQn_Type IRQn); // Clears the pending status of an interrupt
uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn); // Reads the pending status of a interrupt
中断活跃状态寄存器
<font style="color:rgb(51,51,51);">NVIC->IABR[0] ~ NVIC->IABR[15]</font>
c
uint32_t NVIC_GetActive(IRQn_Type IRQn);
中断目标非安全寄存器
NVIC->ITNS[0] ~ NVIC->ITNS[15]
c
uint32_t NVIC_SetTargetState(IRQn_Type IRQn); // Sets interrupt as Non-secure
uint32_t NVIC_ClearTargetState(IRQn_Type IRQn); // Sets interrupt as Secure
uint32_t NVIC_GetTargetState(IRQn_Type IRQn); // Reads the target security state
优先级寄存器
NVIC->IPR[0] ~ NVIC->IPR[495]
c
void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority); // Sets the priority level of an IRQ/ exception
uint32_t NVIC_GetPriority(IRQn_Type IRQn); // Obtains the priority level of an interrupt or exception
软件触发中断寄存器(ArmV8-M 独有)
NVIC->STIR
c
NVIC->STIR = 3; // Triggers IRQ #3
系统异常相关SCB寄存器
- 1~15系统异常
- 相关寄存器

中断控制和状态寄存器
SCB->ICSR- 设置和清除系统异常的挂起状态
- 通过读取 VECTACTIVE 字段决定当前执行的异常/中断号
- 配置SysTick的安全状态
系统中断优先级寄存器
SCB->SHP[0] ~ SCB->[11]- 只有SVC,PendSV,SysTick异常的优先级能编程
应用中断和复位控制寄存器
SCB->AIRCR-
10:8\] PRIGROUP: 控制优先级分组
c
//Set Priority Group
void NVIC_SetPriorityGrouping (uint32_t PriorityGroup);
//Get Priority Group
uint32_t NVIC_GetPriorityGrouping (uint32_t PriorityGroup);
//Encodes the priority for an interrupt with the given priority group
uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t PreemptPriority,
uint32_t Subpriority);
//Decodes an interrupt priority value with a given priority group to\
preemptive priority value and subpriority value.
void NVIC_DecodePriority (uint32_t Priority, uint32_t PriorityGroup, uint32_t*
const pPreemptPriority, uint32_t* const pSubPriority);
系统处理控制和状态寄存器
SCB->SHCSR- 配置错误异常的使能和挂起
- 指示系统异常是否在活跃的状态
异常或中断屏蔽寄存器

PRIMASK
- 使能后,所有优先级大于等于0的中断都会被屏蔽(除了NMI, HardFault)
c
void __enable_irq(); // Clears PRIMASK
void __disable_irq(); // Sets PRIMASK
void __set_PRIMASK(uint32_t priMask); // Sets PRIMASK to value
uint32_t __get_PRIMASK(void); // Reads the PRIMASK value
FAULTMASK
- 只有ArmV8-M(Cortex-M33)和 ArmV7-M(Cortex-M3/M4//M7)有此寄存器
- 使能后,比PRIMASK寄存器多屏蔽了HardFault中断
c
void __enable_fault_irq(void); // Clears FAULTMASK
void __disable_fault_irq(void); // Sets FAULTMASK to disable interrupts
void __set_FAULTMASK(uint32_t faultMask); // Sets FAULTMASK
uint32_t __get_FAULTMASK(void); // Reads FAULTMASK
BASEPRI
- 只有ArmV8-M(Cortex-M33)和 ArmV7-M(Cortex-M3/M4//M7)有此寄存器
- 设置值为优先级,设置后,屏蔽优先级≥设置优先级的中断
c
void __set_BASEPRI(uint32_t priMask); //Sets the BASEPRI register
uint32_t __get_BASEPRI(void); //Reads the BASEPRI register
VTOR
- Vector Table Offset Resgister 向量表偏移量寄存器,用于向量表重定位
$VectorAddress = Exception Number * 4 + Vector Table Offset$- 向量表重定向可能的用途:
- 把向量表从FLASH重定位到RAM,加快访问速度
- 向量表重定位到RAM,可以动态修改向量表中的中断处理程序地址
- 代码区存在不同的运行代码(比如用户boot 和 用户APP),需要各自的向量表
- 向量表偏移后的起始地址,必须是128字节的倍数(M23核)或256字节的倍数(M33核)
外设中断处理流程
- 声明一个中断处理函数,函数名需要和启动代码中的中断处理函数名一样
- 确保中断处理函数清除了中断请求,如果是脉冲方式的中断请求,这个操作不是必须的
- 确保下面的软件初始化步骤
- 设置中断优先级
- 从NVIC中使能中断
- 初始化外设功能
- 外设中断使能
c
// 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 NVIC
NVIC_EnableIRQ(Timer0_IRQn);
Timer0_initialize(); // Device specific code to initialize timer 0
...
void Timer0_Handler(void)
{
... // timer 0 interrupt processing
... // Clear timer 0 IRQ request (needed for level triggered IRQs)
return;
}