microcontroller - Rock solid timer interrupt handling -
i need execute procedure @ constant time intervals. procedure takes long execute , during time other interrupt must active. also, critical procedure executed @ each timer overflow.
take @ pseudo-code:
isr(timer_overflow) { timer_flag = 1; } main_loop() { if (timer_flag) { long_time_consuming_procedure(); timer_flag = 0; } (* if timer interrupt fires here, procedure executed? *) sleep(); }
if above won't work, below code make things rock-solid?
main_loop() { cli(); if (timer_flag) { sei(); long_time_consuming_procedure(); timer_flag = 0; } sei(); sleep(); }
or maybe better, other interrupt handled quickly:
isr(timer_overflow) { sei(); long_time_consuming_procedure(); } main_loop() { sleep(); }
i'm using avr-gcc
edit
looks shared little detail. i'm afraid of worst-case scenario:
- some interrupt (other timer overflow) wakes uc
- long_time_consuming_procedure not called there no timer overflow
- just before moment cpu goes sleep (betwen
if (timerflag)
,sleep()
) timer overflows - timer interrupt executed correctly
- after returning isr cpu goes sleep without executing
long_time_consuming_procedure
, because we've passedif (timerflag)
- there no other interrupts in following timer cycle, cpu woken after next overflow
this way there 2 timer interrupts , 1 long_time_consuming_procedure
execution. there small chance happen, if can go wrong it'll go worse.
in general number 1 common solution. allows procedure execute while other interrupts still handled.
number 2 should avoided because turning interrupts on , off not great practice. there fringe cases require little more detail why number 1 not work.
number 3 not allow other interrupts active while executing long running procedure should avoided other interrupts not handled within isr.
Comments
Post a Comment