OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 16187|回复: 8

关于STM32,一次中断,多次进入中断服务函数的问题说明

[复制链接]

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165516
金钱
165516
注册时间
2010-12-1
在线时间
2116 小时
发表于 2014-2-20 14:04:58 | 显示全部楼层 |阅读模式
以战舰板的例程(实验8,定时器中断实验)说明:
该例程,定时3中断服务函数如下:
//定时器3中断服务程序  
void TIM3_IRQHandler(void)
{          
if(TIM3->SR&0X0001)//溢出中断
{
LED1=!LED1;              
}   
TIM3->SR&=~(1<<0);//清除中断标志位    
}
仿真如下图:

仿真得出的结论是,第一个断点进入一次,第二个断点会多次进入.
当我们改成:


改成这样以后,就会发现,2个断点,每次中断只会进入一次,而不会像之前的情况,多次进入了.

这个问题,ST官方已经给出了答案:
Q: When I cleared the interrupt as the last instruction in the ISR, 
the ISR code is called immediately upon exit half the time. Is there a 
possibility of race condition ?

Answer:
The core (Cortex-M3) generates bufferable write transfer. This mean 
that the CPU consider that the data is written from an AHB point of 
view while the APB write transfer is managed by the AHB2APB bridge and 
could be written later. In this case the CPU left the interrupt 
routine while the interrupt is not yet cleared the CPU will re-enter  
again on the interrupt handler. To avoid this race condition :

1) ISR routine has to clear the interrupt  peripheral flag when just 
entering in the routine to avoid interrupt missing.

2)ISR routine has to Implement a write to the APB  peripheral register 
( to clear the peripheral flag) then followed by a read  access to the 
same register/flag. This operation will force the write buffer to 
complete the effective write and will stall the CPU until the 
effective write of the bit in the register. Therefore  it is 
independent from the AHB/APB ratio prescaler.
Here an example :

STR R1, [R0, #0] ; Store R1 register  peripheral   register  ( or 
using bit-banding peripheral address) 

LDR R2, [R0, #0] ; Load the peipheral register; This will  hold the 
CPU until the effective write of R1.

Use Cortex-M3 Bit-banding feature for interrupt clearing since it is 
an atomic operation and NVIC pending interrupts will be ignored during 
this operation, however Read-Modify-Write is not.


CPU从AHB的角度来看,数据已经写入(清零SR标志),实际上APB上的写操作需要由AHB2APB桥来管理,需要一定时间才能真正完成写操作(SR才能被清零)。那么当CPU退出中断ISR,此时SR还没有真正被清零,此时对应的pending bit仍旧置位,于是又一次触发了中断。


所以,大家如果想避免一次中断,多次进入,可以采取如下办法:
)进入中断后,判断了相应标志位,就clear之,在作后续的处理。
2)如果你硬是要把清标志放在ISR最后,那么为了避免以上情况的发生,写完SR后,再读出来




我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

17

主题

470

帖子

1

精华

高级会员

Rank: 4

积分
718
金钱
718
注册时间
2013-11-23
在线时间
0 小时
发表于 2014-2-20 14:20:21 | 显示全部楼层
好贴,顶!我已习惯一进来就直接清标志。
回复 支持 反对

使用道具 举报

39

主题

2026

帖子

1

精华

论坛大神

Rank: 7Rank: 7Rank: 7

积分
2020
金钱
2020
注册时间
2013-5-1
在线时间
87 小时
发表于 2014-2-20 14:25:21 | 显示全部楼层
看来以后得注意一下了~
博观而约取,厚积而薄发。
回复 支持 反对

使用道具 举报

120

主题

7878

帖子

13

精华

资深版主

Rank: 8Rank: 8

积分
12012
金钱
12012
注册时间
2013-9-10
在线时间
427 小时
发表于 2014-2-20 14:36:25 | 显示全部楼层
一直都是和1楼一样做法,呵呵
现在,程序把烂铜烂铁变得智能化了,人呢,一旦离开了这烂铜烂铁就不知道干啥了
回复 支持 反对

使用道具 举报

10

主题

40

帖子

0

精华

初级会员

Rank: 2

积分
146
金钱
146
注册时间
2014-8-7
在线时间
4 小时
发表于 2014-8-11 17:16:32 | 显示全部楼层
受教了,原来一直是先执行再清除的,感谢原子哥分享。
回复 支持 反对

使用道具 举报

1

主题

17

帖子

0

精华

新手上路

积分
47
金钱
47
注册时间
2017-7-29
在线时间
18 小时
发表于 2017-8-1 15:35:57 | 显示全部楼层
mark!!
回复 支持 反对

使用道具 举报

5

主题

20

帖子

0

精华

初级会员

Rank: 2

积分
193
金钱
193
注册时间
2019-7-23
在线时间
22 小时
发表于 2020-4-20 10:18:22 | 显示全部楼层
中北的秋雨 发表于 2014-8-11 17:16
受教了,原来一直是先执行再清除的,感谢原子哥分享。

额不应该是先清除再执行的吗
回复 支持 反对

使用道具 举报

17

主题

170

帖子

0

精华

高级会员

Rank: 4

积分
562
金钱
562
注册时间
2018-7-8
在线时间
118 小时
发表于 2020-4-20 11:03:04 | 显示全部楼层
受教了
回复 支持 反对

使用道具 举报

2

主题

17

帖子

0

精华

初级会员

Rank: 2

积分
99
金钱
99
注册时间
2018-4-19
在线时间
31 小时
发表于 2022-1-1 23:17:44 | 显示全部楼层
终于看到有营养的了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-21 01:50

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表