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

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

Re: AW: [avr-gcc-list] Gcc bug??


From: Simon Han
Subject: Re: AW: [avr-gcc-list] Gcc bug??
Date: Wed, 3 Mar 2004 19:44:35 -0800 (PST)


On Wed, 3 Mar 2004, Peter Hierholzer wrote:

> > I'm having some problems with an app that uses AvrX and gcc (v. 3.3.1) on
> > an
> > ATmega16. In the interrupt routine for the TWI  I write a static with a
> > code
> > that indicates its current state. In an effort to debug a different
> > problem
> > I compiled the code without any optimization.
> >
> > While I did discover the original problem I had been hunting, I started to
> > see some very unreliable behavior with the TWI. After some investigation I
> > determined that the state variable (mentioned above) was getting written
> > with an unreasonable value (not a valid state). The correct state value
> > that
> > should have been written into the variable is "0". Further experiments
> > revealed that memory corruption (at least directly into the variable's
> > memory location) was not the problem.
> >
> > What I then determined is that the "0" register (r1) contained the value
> > 8!
> > This, not coincidentally (and looking at the disassembled code) is exactly
> > the value that's being incorrectly written into the static state variable.
> > Interestingly, turning on any level of optimization seems to fix it, at
> > least as observed by the TWI port handler not hanging.
> >
> > I was wondering if anyone else has seen anything like this, or has any
> > advice or suggestions.
> >
> > Thanks
> >
> > -brian
>
>
> Hi Brian,
>
> Did you declare the state variable as volatile?
> If you access a variable in your normal program AND in the interrupt routine
> you must declare it as volatile. Otherwise the compiler is not aware that
> the variable is modified in the interrupt routine and generates wrong code.
>

A quick question about "volatile".  If I have follow code.

=============================
static uint8_t cnt;

void incr_cnt()
{
  cnt++;
}

SIGNAL(SIG_OUTPUT_COMPARE0)
{
  uint8_t tmp;
  tmp = cnt;
}
=====================

Since the variable is static, it should be easy for gcc to recongize this
"cnt" variable and not generate wrong code.  On the other hand, if
incr_cnt() and SIGNAL(SIG_OUTPUT_COMPARE0) are in two different files, and
there is no "volatile" declaration, then gcc might generate wrong code.

Am I correct on this?  Could some compiler experts help to explain the
detail about volatile keyword in the context of embedded system?  A
pointer to a page might work as well.

Thank you very much,
Simon

_______________________________________________
avr-gcc-list mailing list
address@hidden
http://www.avr1.org/mailman/listinfo/avr-gcc-list


reply via email to

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