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

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

Re: [avr-gcc-list] Bug in avr-gcc


From: Martin Bammer
Subject: Re: [avr-gcc-list] Bug in avr-gcc
Date: Sun, 13 Feb 2005 21:12:18 +0100
User-agent: KMail/1.7.2

Hi!

Sorry. You're right. Here more info.
gcc-avr 3.4.3
avr-libc 1.0.5
binutils 2.15
I have checked now the bug database. Didn't find the bug in the database.

Testprogram:
#define __AVR_ATmega128  // device select: _ATMEGAxxxx
#include "avr/io.h"

uint8_t i2hex(uint8_t d);
void i8hex(uint8_t d, uint8_t *buf);
uint8_t hex2i(uint8_t d);
uint8_t hex8i(uint8_t *buf);
void sendchar(uint8_t ch);
void sendendl(void);
void sendi8hex(uint8_t data);
void sendstring(uint8_t *buf);
void sendline(uint8_t *buf);
uint8_t recchar(void);
uint8_t kbhit(void);

#include "avr/boot.h"
#include "avr/interrupt.h"
#include "avr/signal.h"

static uint8_t buf[16];
static uint8_t wpos = 0;
static uint8_t endl = 0;


SIGNAL(SIG_UART0_RECV)
{
    static uint8_t d;
     
    d = UDR0;
    buf[wpos++] = d;
    if (d == 13) {
        buf[wpos++] = 10;
        buf[wpos] = 0;
        wpos = 0;
        endl = 1;
    }
}

uint8_t i2hex(uint8_t d)
{
    if (d < 10) return '0' + d;
    return 'A' + (d & 0xf) - 10;
}

void i8hex(uint8_t d, uint8_t *buf)
{
    buf[0] = i2hex(d >> 4);
    buf[1] = i2hex(d & 0xf);
}

uint8_t hex2i(uint8_t d)
{
    if ((d >= '0') && (d <= '9')) return d - '0';
    if ((d >= 'A') && (d <= 'F')) return d - 'A' + 10;
    return 0xff;
}

uint8_t hex8i(uint8_t *buf)
{
    return (hex2i(buf[0]) << 4) | hex2i(buf[1]);
}

void sendchar(uint8_t ch)
{
    loop_until_bit_is_set(UCSR0A, UDRE);
    UDR0 = ch;
}

void sendendl(void)
{
    sendchar('\r');
    sendchar('\n');
}

void sendi8hex(uint8_t data)
{
    sendchar(i2hex(data >> 4));
    sendchar(i2hex(data & 0xf));
}

void sendstring(uint8_t *buf)
{
    while (*buf) {
        sendchar(*buf);
        buf++;
    }
}

void sendline(uint8_t *buf)
{
    sendstring(buf);
    sendendl();
}

static void init_hardware(void)
{
    /* Put interrupt vectors in boot section */
    MCUCR = BV(IVCE);
    MCUCR = BV(IVSEL);
    UBRR0H = 0;
    UBRR0L = 25;
    UCSR0A = BV(U2X);
    UCSR0B = BV(RXCIE) | BV(RXEN) | BV(TXEN);
    UCSR0C = BV(UCSZ1) | BV(UPM1);
    sei();
}

int main(void)
{
    uint8_t c = 0, d, crc = 0;

    init_hardware();
    for (;;) {
        if (endl) {
            sendline(buf);
            endl = 0;
            c = hex8i(buf);
            crc = hex8i(buf + 2);
            if (c == crc) {
                sendline("c == crc");
                sendi8hex(c);
                sendstring("=");
                sendi8hex(crc);
                sendendl();
            }
            if (c == -crc) {
                sendline("c == -crc");
                sendi8hex(c);
                sendstring("=");
                sendi8hex(-crc);
                sendendl();
            }
            if (c != crc) {
                sendline("c != crc");
                sendi8hex(c);
                sendstring("!=");
                sendi8hex(crc);
                sendendl();
            }
            if (c != -crc) {
                sendline("c != -crc");
                sendi8hex(c);
                sendstring("!=");
                sendi8hex(-crc);
                sendendl();
            }
            if (c != (-crc)) {
                sendline("c != (-crc)");
                sendi8hex(c);
                sendstring("!=");
                sendi8hex(-crc);
                sendendl();
            }
            if (-c != crc) {
                sendline("-c != crc");
                sendi8hex(-c);
                sendstring("!=");
                sendi8hex(crc);
                sendendl();
            }
        }
    }
    return 0;
}


Sample output:
First: c=0x45, crc=0x45 (-crc=0xbb)
4545

c == crc
45=45
c != -crc
45!=BB
c != (-crc)
45!=BB
-c != crc
BB!=45

Second: c=0x45, crc=0xbb (-crc=0x45)
45BB

c != crc
45!=BB
c != -crc
45!=45
c != (-crc)
45!=45
-c != crc
BB!=BB

In the second sample output you can see the wrong behavior. "45!=45" and 
"BB!=BB".

So this doesn't work:
if (c != -crc) { ...}

This works:
crc=-crc;
if (c != crc) { ... }

Cheers, Martin

> Martin Bammer wrote:
> >Hi!
> >
> >The actual (debian package) avr-gcc has a bug.
> >
> >The following code works:
> >crc=-crc;
> >if (xyz != crc) execute_something();
> >
> >The following code doesn't work:
> >if (xyz != -crc) execute_something();
> >
> >Is this a known bug?
>
> You don't provide enough information.
> 1. You don't explain how it doesn't work.
> 2. You don't provide how xyz and crc are defined.
> 3. You don't provide your version of the tools: binutils, gcc, avr-libc
>
> and you didn't say whether you checked the gcc bug database to see if
> something like this has been reported before.
>
> Eric

reply via email to

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