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

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

Re: [avr-gcc-list] Global variables and ISRs ???


From: Keith Gudger
Subject: Re: [avr-gcc-list] Global variables and ISRs ???
Date: Thu, 15 Sep 2005 06:13:39 -0700 (PDT)

Excuse me if I'm wrong, but the following statement:
>If the main
> function has interrupts turned off (either globally, or the specific ISR
> interrupt enable), then it can happily use a non-volatile variable
> shared with the ISR.

I think may cause trouble.  When the compiler optimizes your code, it
might make assumptions about the non-volatile variable that are not true?
The main routine may have loaded a register with your variable a while
before disabling interrupts.  Now your main routine may have an old value
for that variable.

Keith

On Thu, 15 Sep 2005, David Brown wrote:

> 
> ----- Original Message -----
> From: "Lars Noschinski" <address@hidden>
> 
> 
> > * Lars Noschinski <address@hidden> [2005-09-06 21:59]:
> > >You must declare the global variable as volatile (or as register), if
> > >you want to modify in an ISR.
> 
> Wrong - that's neither necessary nor complete (there is just as much of an
> issue reading variables as writing them).
> 
> >
> > Speaking of this, if I have an variable which is initialized during
> > startup and only accessed /in/ an ISR, am I on the safe side, if don't
> > declare it as volatile, right?
> >
> 
> Right.
> 
> The point is that you need to use "volatile" if the data may change or be
> used without the compiler knowing it.  If a function uses a variable, and
> may be interrupted by an ISR which also uses that same variable, then the
> variable should be volatile - but otherwise, there is no need for it to be
> volatile.  So a variable that is only used in an ISR is safe.  If the main
> function has interrupts turned off (either globally, or the specific ISR
> interrupt enable), then it can happily use a non-volatile variable shared
> with the ISR.
> 
> mvh.,
> 
> David.
> 
> 
> 
> 
> _______________________________________________
> 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]