查了一下公司的系统服务器,这个问题早在2008年已经以FAQ的形式解释过,并给出了分析和解决方法,我以前也没有注意到这个问题,这次有网友提出才去关注。
在ARM的系统架构下,这是一个普遍的问题,而不是STM32所特有的问题。
这个FAQ的内容如下:
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.
ST网站的FAQ页面入口是:http://www.st.com/stonline/faq/user_faqbrowser.php
这几天这个页面出了点问题,正在检查维护,很快会恢复。
详情回复
发表于 2010-7-21 10:02
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.