暂时禁用ARM上的中断 [英] Temporarily disable interrupts on ARM

查看:266
本文介绍了暂时禁用ARM上的中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用ARM平台(特别是TI TMS570系列).

I am starting working with the ARM platform (specifically the TI TMS570 family).

我有一些关键区域的代码,我不希望发生异常.因此,我想在进入区域时保存启用IRQ和FIR的标志,并在退出时恢复它们.

I have some code with critical regions where I don't want an exception to occur. So I want to save the IRQ and FIR enabled flags on entering the regions and restore them on exiting.

我该怎么做?

推荐答案

要临时屏蔽CPU上的IRQ和FIQ,ARMv7最好的选择是使用

To temporarily mask IRQs and FIQs at the CPU, the nicest option for ARMv7 is to use cps:

// assembly code assuming interrupts unmasked on entry

cpsid if  // mask IRQ and FIQ
...       // do critical stuff
cpsie if  // unmask

某些编译器提供了一组__disable_irq()等内在函数,可从C代码使用,但是对于其他(例如GCC),将是下降到汇编的情况.

Some compilers provide a set of __disable_irq() etc. intrinsics usable from C code, but for others (like GCC) it's going to be a case of dropping to assembly.

如果您想将关键部分嵌套,重入,放入中断处理程序中或需要恢复先前状态而不是最后无条件地取消屏蔽的其他任何操作,则您需要将该状态复制到CPSR之外在掩盖任何东西之前,然后在退出时将其还原.那时,揭露可能最终更简单地处理了CPSR直接读-修改-写的老式方式.这是我脑海中浮现的一个主意:

If you want critical sections to be nested, reentrant, taken in interrupt handlers or anything else which requires restoring the previous state as opposed to just uncondionally unmasking at the end, then you'll need to copy that state out of the CPSR before masking anything, then restore it on exit. At that point the unmasking probably ends up simpler to handle the old-fashioned way of a direct read-modify-write of the CPSR. Here's one idea off the top of my head:

// int enter_critical_section(void);
enter_critical_section:
mrs r0, cpsr
cpsid if
and r0, r0, #0xc0  // leave just the I and F flags
bx lr

// void leave_critical_section(int flags);
leave_critical_section:
mrs r1, cpsr
bic r1, r1, r0
msr cpsr_c, r1
bx lr

这篇关于暂时禁用ARM上的中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆