Wait mode and race conditions

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Wait mode and race conditions

跳至解决方案
1,621 次查看
doynax
Contributor I

Hi! I'm new to the this microcontroller and this is my first post, so be kind. Anyway...

 

I often find it convenient to wait for some condition by triggering a future interrupt and putting the processor to sleep, then after waking up again processing whatever the event was in the main loop with only minimal logic in the interrupt handler itself (e.g. to avoid error-prone synchronization logic with volatiles and such.)

 

Alas I'm having a bit of trouble figuring out a clean way of doing this on the 68HCS08 without potential race conditions. That is in the case where the interrupt triggers before the wait instruction and the wait immediately goes to sleep until the next condition arises without having processed the last one.

 

E.g.

int main(...) {  for(;;) {    process(...);    set_pin_irq();    // If the pin interrupt triggers here we're in trouble

    _Wait;  }}
 

On the AVR I would clear the sleep-enable bit from within the interrupt handler to avoid this case. About the best method I've been able to come up with on this MCU is to wrap the whole block with the interrupt trigger and wait instruction in a setjmp, but that's problematic on so many levels that it's not even funny.

标签 (1)
0 项奖励
回复
1 解答
1,017 次查看
jag
Contributor IV

Hi,

you can put a DisableInterrupts; macro just before the set_pin_irq(); call.

This sets the I bit in the CCR register, that masks every interrupts.

 

In order to reset the I bit, and thus reenabling the interrupts, you have to use the EnableInterrupts; or _Wait; or _Stop; macros.

 

Check the datasheets for more infos.

 

Bye Jack 

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,018 次查看
jag
Contributor IV

Hi,

you can put a DisableInterrupts; macro just before the set_pin_irq(); call.

This sets the I bit in the CCR register, that masks every interrupts.

 

In order to reset the I bit, and thus reenabling the interrupts, you have to use the EnableInterrupts; or _Wait; or _Stop; macros.

 

Check the datasheets for more infos.

 

Bye Jack 

0 项奖励
回复
1,017 次查看
doynax
Contributor I

Ah, so WAIT implicitly enables interrupts. I figured the solution had to be something embarrasingly simple.

 

Thank you!

0 项奖励
回复