gpt4 book ai didi

c++ - 我可以在中断中使用 cli() 和 sei() 吗?

转载 作者:行者123 更新时间:2023-12-05 01:09:35 29 4
gpt4 key购买 nike

我可以像这样使用 cli() 和 sei():

ISR(EXT_INT0_vect)
{
cli();
MyFunction();
sei();
}

听说是禁止的。或者我可以像上面一样使用 cli() 和 sei() 吗?

最佳答案

如果您想要正常的中断行为,手动调用 cli() 和 sei() (如您的示例中)是多余的。也就是说,因为 AVR 在执行 ISR 之前会自动清除全局中断标志。嵌套中断的用例很少,您可能希望在 ISR 中使用 sei() 手动重新启用中断。

来自 Nested interrupts :

Normally, the AVR hardware clears the global interrupt flag (in SREG)before entering an interrupt. This means other interrupts are disabledinside the running handler until the handler finishes and exits.

The RETI instruction is the normal function epilogue for an interrupthandler, and thus re-enables interrupts (just like executing an SEIinstruction). For this reason, normally, interrupt handlers can notbe nested.

For most interrupt handlers, this is the desired behavior. For someits even required in order to prevent infinitely recursive interrupts.Recursion could easily exceed the available stack space and crash therunning program. In rare circumstances it might be desirable tore-enable the global interrupt flag as early as possible inside theinterrupt handler, allowing recursive interrupt handling. Carefully.

This could be accomplished by inserting an sei() instruction right atthe beginning of the interrupt handler, but this still leaves a fewinstructions inside the (compiler-generated) interrupt prologue to runwith global interrupts disabled. [...]

关于c++ - 我可以在中断中使用 cli() 和 sei() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65112480/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com