|
From: | Bernard Fouché |
Subject: | Re: [avr-gcc-list] memcpy() : problem when passing destination pointer |
Date: | Wed, 11 Feb 2009 11:08:10 +0100 |
User-agent: | Thunderbird 2.0.0.19 (Windows/20081209) |
third case: if you use a multi-threaded operating system and a memory location is modified by at least one thread and read by other threads (one may also need a lock mechanism in such a case)IIRC it said volatile must be used to keep the compiler from optimizingaway access to a variable that he thinks might be pointless. Like writing a variable that is never read back.The way volatile was explained to me many years ago, is that a variable must be declared as "volatile" if it's value can be changed by something outside of the mainline code. This boils down to two use cases: - A memory location that can be changed by hardware (i.e. a register) - A memory location that can be changed by an interrupt service routine (ISR).
fourth case: nasty situations where 'volatile' is only a part of the solution but does not insure a correct result:
For instance if ISR1 and ISR2 are *nested* ISRs, IsrCounter does not correctly hold the count of interrupts:
volatile uint8_t IsrCounter; ISR1() { IsrCounter++; } ISR2() { IsrCounter++; }Consider also memory locations larger than the MCU data processing unit size (8 bits for AVR).
The following code gives bad results on an AVR but works on a i386: volatile uint32_t MilliSeconds; ISR() { MilliSeconds++; } main() { while(MilliSeconds < SOME_LIMIT) ... } Bernard
[Prev in Thread] | Current Thread | [Next in Thread] |