In both of these cases, "volatile" is insufficient because it does
not
imply "atomic". In the first case, you need an atomic
read-modify-write, and in the second you need an atomic read of a
wide
value. In both cases you can achieve that by enclosing the the
variable
access in a cli-sei pair. For example, the latter case can be fixed
by
changing the routine to something like
main() // or "int main(void)", yada yada
{
uint32_t ms; // does not need to be volatile
do{ cli(); ms = Milliseconds; sei(); } while (ms < SOME_LIMIT);
}
No, you can not. You need the macros in util/atomic.h because cli /
sei
are not memory barriers and accesses to variables done inside a pair
like that can be moved out by the compiler.
Just search the archives for previous discussions and examples...
Could a pair disabling a specific interrupt (rather than global
interrupts) also be susceptible to the compiler re-ordering it outside
the pair?
For instance, in this example I read a structure modified by a timer
ISR:
ETIMSK &= ˜_BV(OCIE3A);
memcpy(&tryconnect,&rtc,sizeof(struct datetime));
ETIMSK |= _BV(OCIE3A);