STM32之系统中断处理
2017-03-31 14:41
106 查看
分类: C/C++
点击(此处)折叠或打开
/*******************************************************************************
* Function Name : SysTickHandler
* Description : This function handles SysTick Handler.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SysTickHandler(void)
{
__SVC();
/* Toggle PC.04 pin */ LED4
GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_4)));
}
/*******************************************************************************
* Function Name : SVCHandler
* Description : This function handles SVCall exception.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SVCHandler(void)
{
/* Set the PSV system handler pending bit */
NVIC_SetSystemHandlerPendingBit(SystemHandler_PSV);
/* Toggle PC.05 pin */ LED3
GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_5)));
}
/*******************************************************************************
* Function Name : PendSVC
* Description : This function handles PendSVC exception.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void PendSVC(void)
{
/* Set the NMI system handler pending bit */
NVIC_SetSystemHandlerPendingBit(SystemHandler_NMI);
/* Toggle PC.06 pin */ LED2
GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6)));
}
/*******************************************************************************
* Function Name : NMIException
* Description : This function handles NMI exception.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NMIException(void)
{
/* Toggle PC.07 pin */ LED1
GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
}
本例展示了如何运用NVIC和系统Handler(system handler):
各个system handlers拥有如下的强制优先级(preemption priority):
- NMI preemption priority -2 固定优先级
- PSV preemption priority 0 可设置
- SVCall preemption priority 1 可设置
- SysTick preemption priority 2 可设置
首先把Systick定时器设定为每当其计数器为零,这里是1s产生一个Systick中断。
点击(此处)折叠或打开
/* SysTick interrupt each 1 Hz with Counter clock equal to 72MHz/8 = 9MHz */
SysTick_SetReload(9000000);
/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
在Systick handler routine中,设置与PC.04相连的LED4以1秒为周期闪耀。随后执行_SVC(System Service Call)指令。这个指令在cortexm3_macro.h中
执行_SVC()会激活SVCall handler来打断当前的指令流。在SVCall handler routine中,设置与PC.05相连的LED3闪耀,同时设置PSV handler的pending比特。由于PSV(Pendable request for system service)的优先级更高,因此他又会打断SVCall
handler。在PSV handler routine中,设置与PC.06相连的LED2闪耀,并设置NMI(Non maskable interrupt)pending比特,进一步由NMI handler打断当前handler。
最后,在NMI handler中设置与PC.07相连的LED1闪耀。
点击(此处)折叠或打开
/*******************************************************************************
* Function Name : SysTickHandler
* Description : This function handles SysTick Handler.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SysTickHandler(void)
{
__SVC();
/* Toggle PC.04 pin */ LED4
GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_4)));
}
/*******************************************************************************
* Function Name : SVCHandler
* Description : This function handles SVCall exception.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SVCHandler(void)
{
/* Set the PSV system handler pending bit */
NVIC_SetSystemHandlerPendingBit(SystemHandler_PSV);
/* Toggle PC.05 pin */ LED3
GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_5)));
}
/*******************************************************************************
* Function Name : PendSVC
* Description : This function handles PendSVC exception.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void PendSVC(void)
{
/* Set the NMI system handler pending bit */
NVIC_SetSystemHandlerPendingBit(SystemHandler_NMI);
/* Toggle PC.06 pin */ LED2
GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6)));
}
/*******************************************************************************
* Function Name : NMIException
* Description : This function handles NMI exception.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NMIException(void)
{
/* Toggle PC.07 pin */ LED1
GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7)));
}
本例展示了如何运用NVIC和系统Handler(system handler):
各个system handlers拥有如下的强制优先级(preemption priority):
- NMI preemption priority -2 固定优先级
- PSV preemption priority 0 可设置
- SVCall preemption priority 1 可设置
- SysTick preemption priority 2 可设置
首先把Systick定时器设定为每当其计数器为零,这里是1s产生一个Systick中断。
点击(此处)折叠或打开
/* SysTick interrupt each 1 Hz with Counter clock equal to 72MHz/8 = 9MHz */
SysTick_SetReload(9000000);
/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
在Systick handler routine中,设置与PC.04相连的LED4以1秒为周期闪耀。随后执行_SVC(System Service Call)指令。这个指令在cortexm3_macro.h中
执行_SVC()会激活SVCall handler来打断当前的指令流。在SVCall handler routine中,设置与PC.05相连的LED3闪耀,同时设置PSV handler的pending比特。由于PSV(Pendable request for system service)的优先级更高,因此他又会打断SVCall
handler。在PSV handler routine中,设置与PC.06相连的LED2闪耀,并设置NMI(Non maskable interrupt)pending比特,进一步由NMI handler打断当前handler。
最后,在NMI handler中设置与PC.07相连的LED1闪耀。
相关文章推荐
- STM32 bootloader程序跳转到APP卡死在B_ENDP_ALIGN,原因:有未处理的中断。解决方法:重启系统
- 第四步:创建键盘中断处理模块和初步搭建程序执行系统
- Linux0.11内核--系统中断处理程序int 0x80实现原理
- linux系统编程之信号(七):被信号中断的系统调用和库函数处理方式
- Linux系统编程——进程间通信:信号中断处理
- 通过分析system_call中断处理过程来深入理解系统调用
- 三读内核中断处理(5):中断系统提供的API
- linux中断子系统(一) - 注册系统中断处理函数
- 系统调用和中断处理的异同(以Linux MIPS为例)
- 初探STM32 SPI2中断接受多组数据的处理方式
- STM32学习笔记(六) SysTick系统时钟滴答实验(stm32中断入门)
- 别忘了--要处理被中断的系统调用
- STM32中断流程处理
- Linux0.11内核--系统中断处理程序int 0x80实现原理
- linux系统编程之信号(七):被信号中断的系统调用和库函数处理方式
- Linux0.11内核--系统中断处理程序int 0x80实现原理
- STM32外部中断处理流程及注意事项
- linux系统编程之信号(七):被信号中断的系统调用和库函数处理方式
- stm32之中断系统
- 原子战舰STM32——定时器捕获中断库函数程序中中断处理函数说明