avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] volatile...


From: Larry Barello
Subject: RE: [avr-gcc-list] volatile...
Date: Thu, 14 Dec 2006 06:43:29 -0800

This is somewhat a compiler issue since it is both a necessary keyword, yet
insufficient to write correct code.  It is a problem that vexes many
newcomers (and old farts like me who forget every now and then) and is
discussed in the FAQ, but is worth burning space here as well.

The GCC compiler is very smart.  Global or local (e.g. static) variables are
stuffed into SRAM.  In a function the compiler will often read the variable
into registers and work on it there, stuffing it back out to SRAM later on.
Depending upon the routine, e.g. a forever() loop in main(), it may *never*
write it back.

This model, although very efficient, breaks if you have an interrupt handler
asynchronously modifying the same variable while it is in registers in the
function.

The "volatile" keyword tells the compiler that *every* access to the
variable has to be done in place, no caching or optimization.  A related,
but very important, is protecting the operation on the variable with cli/sei
instructions.  Access and operations typically take multiple instructions.
e.g. foo++;  Takes three or more instructions.  If the interrupt handler
interrupts between the read & write, well, bad things happen.  So, in
reality not only do you have to say "volatile" you must also protect
operations with the cli/sei instruction.  

As always, when working in these detailed areas it is imperative you inspect
the generated assembly code to make sure you are telling the compiler to do
the right thing - otherwise you will spend endless days with subtle random
bugs that are very hard to track.

Cheers!


| -----Original Message-----
| From: address@hidden [mailto:avr-gcc-
| address@hidden On Behalf Of address@hidden
| Sent: Thursday, December 14, 2006 5:48 AM
| To: address@hidden
| Subject: Re: [avr-gcc-list] volatile...
| 
| Hello Javier,
| 
| Javier Almansa Sobrino wrote:
|  > Hi everybody. I've a little stupid question....
|  >
|  > What's the differece between a volatile variable in a funcion and the
| same variable not volatile?
|  >
|  > I've noted a non volatile variable is like don't exists (I think).
|  >
| 
| Despite what some have said about this question, I don't think it is
| "stupid" as it pertains AVR-GCC (and other microcontroller compilers).
| 
| The way I look at it is:
|  ---If you want a GLOBAL to be changed in BOTH real-time-interrupts AND in
| the foreground, you should use VOLATILE (so that the compiler knows this
| variable is capable of changing at any time and dependent code is not
| optimized out of existence).
| 
|  ---If you want a GLOBAL to be changed in EITHER real-time-interrupts OR
| in the foreground, you don't have to use VOLATILE (but it doesn't hurt).
| 
| Regards,
|  - Mike Ware
| 
| 
| _______________________________________________
| AVR-GCC-list mailing list
| address@hidden
| http://lists.nongnu.org/mailman/listinfo/avr-gcc-list





reply via email to

[Prev in Thread] Current Thread [Next in Thread]