在使用 STM32 的外部中断功能时,我们经常需要确认是否真的产生了外部中断,查看库函数,我们发现了这两个函数:EXTI_GetFlagStatus 和 EXTI_GetITStatus 。原型如下:
FlagStatus EXTI_GetFlagStatus ( uint32_t EXTI_Line );
ITStatus EXTI_GetITStatus ( uint32_t EXTI_Line );
可以看出,这两个函数是十分相似的,EXTI_GetFlagStatus 的作用是 Checks whether the specified EXTI line flag is set or not. 即检查指定的外部中断线的标志是否被置位;而 EXTI_GetITStatus 的作用是 Checks whether the specified EXTI line is asserted or not. 即检查指定外部中断线的状态是否有效。
也就是说前者是检查中断标志的,后者是检查中断状态的。我们追踪一下这两个库函数的实现即可发现区别。
【库函数 EXTI_GetFlagStatus】
/**
* @brief Checks whether the specified EXTI line flag is set or not.
* @param EXTI_Line: specifies the EXTI line flag to check.
* This parameter can be:
* @arg EXTI_Linex: External interrupt line x where x(0..19)
* @retval The new state of EXTI_Line (SET or RESET).
*/
FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_GET_EXTI_LINE(EXTI_Line));
if ((EXTI->PR & EXTI_Line) != (uint32_t)RESET) {
bitstatus = SET;
}
else {
bitstatus = RESET;
}
return bitstatus;
}
【库函数 EXTI_GetITStatus】
/**
* @brief Checks whether the specified EXTI line is asserted or not.
* @param EXTI_Line: specifies the EXTI line to check.
* This parameter can be:
* @arg EXTI_Linex: External interrupt line x where x(0..19)
* @retval The new state of EXTI_Line (SET or RESET).
*/
ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
{
ITStatus bitstatus = RESET;
uint32_t enablestatus = 0;
/* Check the parameters */
assert_param(IS_GET_EXTI_LINE(EXTI_Line));
enablestatus = EXTI->IMR & EXTI_Line;
if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET)) {
bitstatus = SET;
}
else {
bitstatus = RESET;
}
return bitstatus;
}
可以看到区别在于 EXTI_GetITStatus 比 EXTI_GetFlagStatus 多做了一个判断:
enablestatus = EXTI->IMR & EXTI_Line;
if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
EXTI->PR 是 STM32 的挂起寄存器(EXTI_PR),其中的各个位被称为挂起位(Pending bit)。当外部中断线上发生了选择的边沿事件时,EXTI_PR 中相应的 Pending 位被置‘1’,也就是上面提到的 Flag。
而 EXTI->IMR 是中断屏蔽寄存器(EXTI_IMR),其中各个位表示相应中断线上的中断屏蔽。‘0’表示屏蔽来自线x上的中断请求,‘1’开放来自线x上的中断请求。
所以,EXTI_GetFlagStatus 只是纯粹读取中断标志位的状态,但是不一定会响应中断(EXT_IMR 寄存器对该中断进行屏蔽);而 EXTI_GetITStatus 除了读取中断标志位,还查看 EXT_IMR 寄存器是否对该中断进行屏蔽,在中断挂起 & 没有屏蔽的情况下就会响应中断。