conditional transition to sleep mode
In many aplications i am using simple template, where interrupt routines (for example UART receive) raise flags (volatile variables) and main() function polling these flags and processing these events (for example parsing incoming UART charaters). I would like to put MCU to sleep when there is nothing to do - when all flags are cleared. But i can not simply write:
if(!flag1 && !flag2 ...){
go_to_sleep();
}because of potential race condition. Flag can be set during if statement evaluation and there is risk, that MCU will go sleep with raised some flag. Is there any mechanism how to sleep STM8S (and possibly STM8L) safe - only if flags are cleared ?
just for interest:
STM32 handles that problem by calling "send event" instruction (at place where flag is raised), which prevents falling asleep.
AVR have separated bit for "sleep enable" and this bit can be cleared in IRQ routine, where flag is raised and also prevent MCU from going to sleep mode.
How to do that with STM8 ?
