如果我在其 ISR 中禁用中断会发生什么?

发布于 2024-09-25 00:52:41 字数 192 浏览 0 评论 0原文

如果在该中断的 ISR 内禁用该中断会发生什么?
例如,如果我通过 USART 从缓冲区传输数据,并且该缓冲区耗尽了数据,那么我想暂时停止传输,因此在发送缓冲区中的最后一个字节后,我禁用中断。

(这是在 PIC18F4580 上)

PIC18F4580 的数据表指出,如果在 ISR 内部禁用中断,“可能会出现不稳定的行为”。

What happens if you disable an interrupt inside that interrupt's ISR?
For example, if I am transmitting data over USART from a buffer and that buffer runs out of data then I want to stop transmitting temporarily, so after sending the last byte in the buffer, I disable the interrupt.

(This is on a PIC18F4580)

The datasheet for the PIC18F4580 states that "erratic behaviour may occur" if an interrupt is disabled inside of an ISR.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-10-02 00:52:41

我不太了解 PIC18F4580,但作为一般规则
在 ISR 中禁用中断并没有什么问题

本质上,这将阻止 ISR 再次被调用(即直到中断以某种方式重新启用),但它不应该影响 ISR 的逻辑,导致它终止或其他可能会发生的奇怪的事情。
事实上,ISR 通常会首先禁用中断,以免它们在服务第一个中断时被中断(从而避免各种重入问题,但代价可能是丢失事件)。

当然,如果这对程序/逻辑的其余部分很重要,您需要计划如何/在何处重新启用中断。

I don't know specifically about the PIC18F4580, but as a generaly rule,
there is nothing wrong with disabling an interrupt while in its ISR

Essentially this will prevent the ISR to be called again (i.e. until the interrupt is somehow re-enabled), but it shouldn't affect the logic of the ISR, cause it to terminate or other odd thing one may thing about.
Indeed, may ISRs will typically start by disabling interrupts lest they would get interrupted while servicing a first interrupt (and hence avoiding various re-entrancy issues, at the cost of possibly missing events).

Of course you need to plan on how/where the interrupt would be re-enabled, if that is important to the rest of your program/logic.

你的往事 2024-10-02 00:52:41

免责声明:此处讲述的是 PIC32 和 dsPIC 经验以及我从 PIC18F4580 数据表和勘误表中读到的内容 (PIC18F4580 参考)。

根据 PIC18F4580 数据表的中断部分:

不要使用MOVFF指令修改
任何中断控制寄存器同时
任何中断都被启用。这样做可能会
导致微控制器行为不稳定。

因此,要获得该未知行为,您需要修改 INTCONx 寄存器。当您指定要在处理程序内切换 USART 模块的中断启用时,您不必担心该警告,因为您将更改 PIE 而不是 INTCONx。请注意,PIE 寄存器保存特定外设中断的使能。还有一种通用中断启用 (GIE),可一次性屏蔽所有可屏蔽中断。您不想在中断内弄乱这个,因为它应该由处理程序的进入/退出过程(也在寄存器 INTCON 中)自动为您处理。

因此,正如 mjv 所说,在其处理程序内屏蔽 USART 外设中断是完全合理的事情。

Disclaimer: Speaking from PIC32 and dsPIC experience here combined with what I read from the PIC18F4580 datasheet and errata (PIC18F4580 References).

According to the PIC18F4580 datasheet's Interrupt section:

Do not use the MOVFF instruction to modify
any of the Interrupt Control registers while
any interrupt is enabled. Doing so may
cause erratic microcontroller behavior.

So to get that unknown behaviour you need to modify an INTCONx register. As you specified that you wanted to toggle the interrupt enable for the USART module inside the handler, you shouldn't have to worry about that warning as you will be changing PIE and not INTCONx. Note that the PIE register holds enables for specific peripheral interrupts. There is also a general interrupt enable (GIE) that masks ALL maskable interrupts in one shot. You don't want to be messing with this one inside the interrupt as it should be handled for you automatically by the handler's entry/exit procedure (also its in register INTCON).

So like mjv said, masking the USART peripheral interrupt inside its handler is a perfectly reasonable thing to do.

末蓝 2024-10-02 00:52:41

中断停止发生。但为什么要这样做呢?您是否有计划何时重新启用中断?

如果无事可做,就让 ISR 不做任何事情。

The interrupt stops happening. But why do this? And do you have a plan for when you're going to re-enable the interrupt?

Just have your ISR do nothing if there is nothing to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文