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_W RITE
};
struct __attribute__ ((packed)) USB_iobuf_commo n_header_s {
unsigned char function;
unsigned char size_lo;
unsigned char size_hi;
};
typedef struct USB_iobuf_commo n_header_s USB_iobuf_commo n_header;
struct __attribute__ ((packed)) USB_iobuf_i2c_h eader_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_h eader_s USB_iobuf_i2c_h eader;
enum usb_offset {
usb_idx_functio n = 0,
usb_idx_size = usb_idx_functio n + sizeof(unsigned char),
usb_idx_i2c_add r = usb_idx_size + sizeof(unsigned char),
usb_idx_i2c_sub addr = usb_idx_i2c_add r + sizeof(unsigned char),
usb_idx_i2c_dat asize = usb_idx_i2c_sub addr + sizeof(unsigned char),
usb_idx_i2c_dat a = usb_idx_i2c_dat asize + sizeof(unsigned char)
};
char data[MSG_SIZE];
char reg;
....
CDC_Read(..., data, MSG_SIZE);
USB_iobuf_commo n_header *hdr = (USB_iobuf_comm on_header *)data;
switch (hdr->function) {
case CMD_I2C_READ:
AT91F_TWI_ReadS ingleIadr(AT91C _BASE_TWI, (usb_idx_i2c_ad dr << 16),
data[4], AT91C_TWI_IADRS Z_1_BYTE, ®);
...
break;
...
}
At this point I expect to have 'usb_idx_i2c_ad dr' 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: mr*@tusur.ru