Re: [Hampshire] [Slightly OT] Arduino coding help - pointers…

Top Page

Reply to this message
Author: Bob Dunlop
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] [Slightly OT] Arduino coding help - pointerstostructs

> Ah, that's interesting. It *definitely* used to use the base type.


A few oddball compilers might do this but every one I've used in 30 years
has packed the bits (at least those that would fit into a byte).

Ardunio = AVR ATMega

They use the Gcc AVR compiler in the development system.


struct received_data {
    unsigned int start:1;
    unsigned int address:4;
    unsigned int command:5;
    unsigned int flags:4;
    unsigned int padding:1;
    unsigned int parity:1;
} __attribute__((packed));



unsigned int
x( char *in )
{
    struct received_data *r;


    r = (struct received_data *)in;


    return r->command;
}



Compiled with no errors with my avr-gcc (4.3.2) and the assembler looks right
(I've no access to hardware at present).

The structure is 2 bytes long, probably because I included the
__attribute__((packed)) hint for Gcc.


However I wouldn't recommened this sort of programming for reassembling data
from multiple bytes, it's highly non-portable. Bit and shift is better.

First though I'd go and shoot the hardware engineer, who splits a bitfield
across adjacent bytes ?
Since this structure contains bit's labled start and parity are you reassembing
data from some hokey koky 15 bit UART or something ? Perhaps you should
treat the raw datastrea as a series of uint16_t rather than chars ?


ATMega is little-endian BTW.

-- 
        Bob Dunlop