Hi,
I am getting this odd problem with structs. If the size of the struct 
is larger than 4 bytes I can't access it.. It's most peculiar.

I have a test case (attached).

Build it with..
avr-gcc -O2 -fno-strict-aliasing -pipe  -g -Wall -Wunreachable-code 
-DF_CPU=16000000 -mmcu=atmega32 -Wa,-adhlmsn=testcase.lst -c testcase.c -o 
testcase.o
avr-gcc -O2 -fno-strict-aliasing -pipe  -g -Wall -Wunreachable-code 
-DF_CPU=16000000 -mmcu=atmega32  -Wl,-Map=testcase.map,--cref  -g testcase.o -o 
testcase.elf 
avr-objcopy -j .text -O ihex testcase.elf testcase.hex
avr-objdump -S testcase.elf > testcase.dmp

If BREAKME is defined all the struct accesses return 255 instead of what
they should be (I think :)

I am wondering if there is some oddity with GCC optimising for <= 4 byte
structs which unbreaks my code, but I can't see why it is broken.

Any help gratefully received!

-- 
Daniel O'Connor software and network engineer
for Genesis Software - http://www.gsoft.com.au
"The nice thing about standards is that there
are so many of them to choose from."
  -- Andrew Tanenbaum
GPG Fingerprint - 5596 B766 97C0 0E94 4347 295E E593 DC20 7B3F CE8C
#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
#define UART_BAUD_RATE		38400

int
uart_putc(char c) {
    loop_until_bit_is_set(UCSRA, UDRE);
    UDR = c;

    return(0);
}

void
uart_putsP(const char *addr) {
    char c;

    while ((c = pgm_read_byte_near(addr++)))
	uart_putc(c);
}

void
uart_puts(const char *addr) {
    while (*addr)
	uart_putc(*addr++);
}

void
uart_puts_dec(uint8_t a, uint8_t l) {
    char	s[4];
    
    if (l && a < 10)
	uart_putsP(PSTR("0"));
    uart_puts(utoa(a, s, 10));
}

void
uart_puts_hex(uint8_t a) {
    char	s[3];
    
    if (a < 0x10)
	uart_putc('0');
    
    uart_puts(utoa(a, s, 16));
}

int
main(void) {
    /* Init UART */
    UBRRH = UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU) >> 8;
    UBRRL = (uint8_t)UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU);
    
    /* Enable receiver and transmitter. Turn on transmit interrupts */
    UCSRA = 0;
    UCSRB = _BV(RXEN) | _BV(TXEN) | _BV(RXCIE);
    UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
    uart_putsP(PSTR("\n\r\n\r===============\n\r"
		    "Inited!\n\r\n\r"));
    struct foo { 
	uint8_t a;
	uint8_t b;
	uint8_t c;
	uint8_t d;
#ifdef BREAKME
	uint8_t e;
#endif
    } abc = {
	1,
	2,
	3,
	4,
#ifdef BREAKME
	5
#endif
    };
    
    uart_puts_hex(abc.a);
    uart_putsP(PSTR("\n\r"));
    uart_puts_hex(abc.b);
    uart_putsP(PSTR("\n\r"));
    uart_puts_hex(((uint8_t *)&abc)[0]);
    uart_putsP(PSTR("\n\r"));

    for (;;)
	;
}

Attachment: pgptSpQaHNsKF.pgp
Description: PGP signature

_______________________________________________
AVR-chat mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-chat

Reply via email to