packed struct difficults (CW 6.3)

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

packed struct difficults (CW 6.3)

3,118 次查看
mjbcswitzerland
Specialist V
Hi All

I have a struct which is constructed from other structs. Each struct is made up of only bytes.



typedef struct stUSB_CONFIGURATION_DESCRIPTOR_COLLECTION
{
    USB_CONFIGURATION_DESCRIPTOR config_desc;
    USB_INTERFACE_DESCRIPTOR     interface_desc;
    USB_TEST_DESCRIPTOR               test_desc;
    USB_ENDPOINT_DESCRIPTOR      endpoint_desc;
} USB_CONFIGURATION_DESCRIPTOR_COLLECTION;


An example of the individual structs:
typedef struct _PACK stUSB_ENDPOINT_DESCRIPTOR
{
    unsigned char          bLength;                                      // descriptor size in bytes
    unsigned char          bDescriptorType;                              // device descriptor
    unsigned char          bEndpointAddress;                             // direction and address of endpoint
    unsigned char          bmAttributes;                                 // endpoint attributes
    unsigned char          wMaxPacketSize[2];                            // endpoint FIFO size
    unsigned char          bInterval;                                    // polling interval in ms
} USB_ENDPOINT_DESCRIPTOR;



The problem that I have is that there are additional bytes inserted between the individual strucs - eg.
USB_ENDPOINT_DESCRIPTOR is 7 bytes in size (as are others) and each time a struct is uneven, the next is moved to start at an even boundary - thus the complete block is larger than expected and its content is not correctly recognised in use-

I have tried #pragma options align=packed but this didn't have any effect - the situation remained.

Does anyone know how I can force the desired results?

Regards

Mark Butcher

www.uTasker.com

标签 (1)
标记 (1)
0 项奖励
回复
2 回复数

1,275 次查看
Southernboy
Contributor I
Hi, I had a very similar problem when interfacing the MCF5407 to a usb device. The trick is to pack the structure as follows:
 
#pragma pack(1)  // pack all structures to 1 byte
 
typedef struct {
    uint8 data1;
    uint8 data2;
    uint8 data3;
} data_struct1;
 
typedef struct {
   data_struct1 data1;
   uint32 data2;
   uint8 data3;
   uint32 data4;
} data_struct2;
 
// total size of data_struct1 is 3
// total size of data_struct2 is 12
 
// without the pragmas I think there would be 1 pad byte before data2.
// there would also be 3 pad bytes before the data4 member.
 
#pragma pack (0)  // default packing
 
I hope this helps
 
Neil Martin
0 项奖励
回复

1,276 次查看
mjbcswitzerland
Specialist V
Hi Neil

Many thanks, that is what it needed!

Best regards

Mark

0 项奖励
回复