When or why we must use the key word "volatile" in our embedded program?

Can give some small example?

Reply to
Bruce Sam
Loading thread data ...

This should explain all -

formatting link

cheers,

Al

Reply to
Al Borowski

That's a very useful article. Thanks! (and i'm not even the OP!)

--buddy

Reply to
Buddy Smith

Do not rely on the subject to be part of your message. In many newsreaders it is not even visible.

Imagine a system where the hardware signals new data ready on the 1 bit of an address called DREADY. This would be checked by something like:

int *dready = whatever; ... while (!(*dready & 1)) continue; /* go get the data */

and any optimizer worth anything will change that to:

if (*dready & 1) /* nothing */; /* go get the data */

The solution is to define dready as pointing to a volatile object

volatile int *dready = whatever;

and the code will continue accessing *dready until the bit comes true. volatile means that something other than the actual code can change this object.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
     USE worldnet address!
Reply to
CBFalconer

Copy of Subject: When or why we must use the key word "volatile" in our embedded program?

In a nutshell: Whenever a data object does something that C doesn't know about.

Like: change all by itself, or change by some other part of the C program that doesn't conform to the standard flow of control in a C program (multi-threading, interrupts, you name it).

--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Reply to
Hans-Bernhard Broeker

OR, when you want to make sure the compiler generates a write operation every time the variable is assigned a value.

OR, when you want to make sure the compiler generates a read operation every time the variable is referenced.

You may wish to force a read operation because the value changes "behind-the-scenes" as mentioned above. Or there may be desired side effects of the read operation regardless of whether it's value is changing or not.

--
Grant Edwards                   grante             Yow!  Hey, I LIKE that
                                  at               POINT!!
                               visi.com
Reply to
Grant Edwards

The "volatile" keyword means that the memory location in question can be affected by something outside the running program and/or that the act of reading/writing that location has a side effect.

E.g. supposed padc is the pointer to an ADC control register: you write 0 to it to start it, and wait for it to become non-zero to indicate "conversion complete".

char * padc = (char*);

*padc = 0; while ((*padc)==0) { // wait } .. do something with the data

Any decent compiler will see that *padc gets set to 0 once, and never gets changed BY THE PROGRAM. Thus, it will treat the while() as an infinite loop and optimise it away.

Change the declaration of padc to volatile char *padc = .... whatever and this will force the compiler to make an explicit read of that location at each time (in this case, each iteration of the while loop), knowing that it may change at any time.

Some compilers treat everything as volatile anyway. Depending on your politics/religious convictions, this is either good or bad.

Richard [in PE12]

Reply to
Endymion Ponsonby-Withermoor III

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.