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

查看:34
本文介绍了暂时禁用 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 的最佳选择是使用 cps:

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天全站免登陆