|
| From: | Chip Webb |
| Subject: | Re: [avr-gcc-list] Global variables and ISRs ??? |
| Date: | Thu, 15 Sep 2005 10:32:36 -0500 |
| User-agent: | Mozilla Thunderbird 1.0.6 (Windows/20050716) |
|
When the volatile attribute is used on a global variable, the compiler
will not optimize away accesses to that variable. If you are using a global variable to communicate between an ISR and a "main" routine, you probably need to use the volatile keyword in the variable declaration in order to ensure that the compiler doesn't get fooled into optimizing away important parts of your code. A more subtle issue is ensuring atomic accesses to multi-byte variables (or data structures). A common example is an ISR that does serial receive into a char buffer and updates a pointer into that buffer). The problem is that if the main routine does not protect it's accesses to to the shared global variables with cli() and sei() (and they're not already atomic [single-cycle] instructions) it is possible for the main routine to read or corrupt the values of these variables. Chip
|
| [Prev in Thread] | Current Thread | [Next in Thread] |