Hello,
I already asked about my problem and received several valuable advices which
I have followed to. Anyway I should explain briefly.
In the program I'm working on I receive a data stream, having structure as
follows:
| common_header | I2C_header | data ... |
My purpose is to parse the 'common_header', and read the data following it.
This is my code snippet:
#define MSG_SIZE 1000
enum ctrl_cmds {
CMD_ECHO = 0x0,
CMD_I2C_WRITE,
CMD_I2C_READ,
CMD_I2C_BURST_WRITE
};
struct __attribute__ ((packed)) USB_iobuf_common_header_s {
unsigned char function;
unsigned char size_lo;
unsigned char size_hi;
};
typedef struct USB_iobuf_common_header_s USB_iobuf_common_header;
struct __attribute__ ((packed)) USB_iobuf_i2c_header_s {
unsigned char addr;
unsigned char subaddr_lo;
unsigned char subaddr_hi;
unsigned char datasize_lo;
unsigned char datasize_hi;
};
typedef struct USB_iobuf_i2c_header_s USB_iobuf_i2c_header;
enum usb_offset {
usb_idx_function = 0,
usb_idx_size = usb_idx_function + sizeof(unsigned char),
usb_idx_i2c_addr = usb_idx_size + sizeof(unsigned char),
usb_idx_i2c_subaddr = usb_idx_i2c_addr + sizeof(unsigned char),
usb_idx_i2c_datasize = usb_idx_i2c_subaddr + sizeof(unsigned char),
usb_idx_i2c_data = usb_idx_i2c_datasize + sizeof(unsigned char)
};
char data[MSG_SIZE];
char reg;
....
CDC_Read(..., data, MSG_SIZE);
USB_iobuf_common_header *hdr = (USB_iobuf_common_header *)data;
switch (hdr->function) {
case CMD_I2C_READ:
AT91F_TWI_ReadSingleIadr(AT91C_BASE_TWI, (usb_idx_i2c_addr << 16),
data[4], AT91C_TWI_IADRSZ_1_BYTE, ®);
...
break;
...
}
At this point I expect to have 'usb_idx_i2c_addr' equal to 'data[3]', but
it's always 0, though the other end is sending 0xC0. If I use 'data[3]' or
'data[4]' - everything works perfectly, but this is ugly hack.
I'm stuck. What's the problem with that?
The part of problem problem was with alignment of structure, I found out how
to disable padding and I also added the offsets (via 'enum') to be more
readable.
Thanks.
With best regards, Roman Mashak. E-mail: (e-mail address removed)