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

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

Re: [avr-gcc-list] Extremly confusing behaviour... (more detailed explan


From: Mike Panetta
Subject: Re: [avr-gcc-list] Extremly confusing behaviour... (more detailed explanation of error, believe me its not a volatile thing)
Date: 14 May 2003 13:23:13 -0400

Ok, here is what I have found out.  And so I do not confuse anyone, I
will only paste the actual code that is broken...

Here is the snippet of code I have been having problems with:

do
{
        delay = 1000;
        while (delay--);
        PORTA |= _BV (PA1);
        retval = srf08_read(1, 0, pingdata, 2);
        PORTA &= ~(_BV(PA1));
        PORTB = retval;
        //printf("%ld ", retval);
} while (retval != 2);

retval is declared as an int32_t at the top of main, where this snippet
of code resides.  Its declared as an int32, because srf08_read() returns
the number of bytes read as an int32 (was not sure if ssize_t was
declared for AVR, and I was not sure what size it was). The variable
pingdata is declared as uint8_t pingdata[2]; and is only used as dummy
storage in this snippet of code, because all I care about is whether or
not the srf08 sonar module acknowledged the transfer or not (signaled in
the return value of srf08_ping() as -1 for transfer error, or the number
of bytes transferred if success).  Oh, and of course, delay is a
volatile, otherwise the while(delay--); would be optimized away as dead
code.

The above code does not work as written, and as you can see I put some
code in to aid in testing.  The PORTA bit twiddling is there to trigger
my scope on.  The PORTB assignment is there to let me know what the
lower 8 bits of retval are set to (it should be 0xFF if there was an
error, or 0x02 if not, so I only need to watch the lower 2 bits of the
port to check things out).

If I run the code as it is above, I get 0xFF on port B for a few cycles
(as expected, the sensor does not return data immediately) then I get a
constant 0x02 (as the sensor is now ready), and my code stays in the do
{} while(); loop.  Since I am getting 0x02 on the PORTB pins, I would
have expected it to exit the loop and go on to the code below, but it
does not.

Now here is the weird part.  If I change the code to this:

do
{
        delay = 1000;
        while (delay--);
        PORTA |= _BV (PA1);
        retval = srf08_read(1, 0, pingdata, 2);
        PORTA &= ~(_BV(PA1));
        PORTB = retval;
        //printf("%ld ", retval);
} while ((int8_t)retval != 2);         <-------------- Change made here

Then the code works perfectly!!!!

So basicly I am asking, does GCC have some sort of bug where comparing
int32_t's are involved, or am I doing something stupid?  Ill accept
either answer, as long as you do not just say look at the FAQ.  I want
to know exactly what I did wrong if I did indeed do something wrong.

The code is simple enough, so hopefully someone can point me to my
error.


Thanks,
Mike




reply via email to

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