I'm new here, but i've just started my first project and ran into a few issues which i think i've now ironed out. According to the spec of the atmel mega and tiny processors after calling sli() one following instruction is guaranteed to run.
In the case of sleep_mode in sleep.h i noted the implementation is as follows:
sleep_enable(); \ sleep_cpu(); \ sleep_disable(); \
sleep_cpu is the "second" instruction and i am supposing that these map one for one at compile time down to a single asm instruction. Surely that's a timing bug right there, since now you can accidentally trigger an interrupt before the sleep occurs. I'm not sure what the process is for writing these libraries that come included so except for manually fixing it myself not sure what i can do.
I beleive the older examples that didn't use sleep_mode and directly use sli(), sleep_cpu(); are far more clear.
Unless anyone else knows otherwise why that sleep_enable needs to be before sleep_cpu when all examples i have read show the following:
... cli(); sleep_enable(); //maybeAttachSomeWakeInterruptsHere sli(); sleep_mode() //sleeping sleep_disable();
then i'll continue with my modified header.