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: Keith Gudger
Subject: RE: [avr-gcc-list] "Volatile"
Date: Fri, 15 Apr 2005 14:43:14 -0700 (PDT)

So I guess this is sort of my question:

You cast a variable as "volatile" when it could possibly change in an
interrupt.  This forces the compiler to read it from RAM whenever it's
used, and not use a register to store it.

Is there a corresponding "cast" or anything that would force the compiler
to _cli & _sei around any change in a variable?  Or is it up to my
(forgetful) self?  Thanks.

Keith

On Fri, 15 Apr 2005, David Harper wrote:

> I'm certainly no expert, but when I'm modifying variables that could also 
> potentially be modified at the interrupt level, I disable interrupts before I 
> change the variable and reenable them afterwards.
> 
> Dave
> 
> ---------------
> Dave Harper             Chiaro Networks
> 972-739-7919 (voice)  251 Renner Pkwy.
> 972-739-8020 (fax)    Richardson, TX 75080
> 
> 
> 
> 
> -----Original Message-----
> From: Keith Gudger [mailto:address@hidden
> Sent: Friday, April 15, 2005 3:48 PM
> To: address@hidden
> Subject: [avr-gcc-list] "Volatile"
> 
> 
> Those of us on this list all know the "volatile" drill - it's FAQ #1.
> This is sort of the same issue, and I'm wondering if the code that "got
> me" is as obvious as the other volatile stuff.
> 
> I have a volatile variable, "Flags", that is set and cleared in many
> routines, including an interrupt.  Here is the disassembly for the
> Flags_Clear function:
> 
> 00000f3e <Flags_Clear>:
>      f3e:       98 2f           mov     r25, r24  
>      f40:       90 95           com     r25
>      f42:       80 91 a4 00     lds     r24, 0x00A4
>      f46:       89 23           and     r24, r25       ; <- interrupt here
>      f48:       80 93 a4 00     sts     0x00A4, r24
>      f4c:       08 95           ret
> 
> The interrupt occured right before f46, and set the flag.  The interrupt
> pushed and popped r24 & r25's values, so when the interrupt returned to
> f46, the flags were restored to their *previous* state (the flag unset!
> Ack!  I'm screwed!)
> 
> Two questions:  1)  Is this something I should have learned in Embedded
> 101?  If so, I"ll slink quitely away...   and if not..  2)  How would the
> experts on this list have prevented this?
> 
> Thank you.
> Keith
> 
> 
> 
> _______________________________________________
> AVR-GCC-list mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
> 
> 
> _______________________________________________
> 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]