packed struct difficults (CW 6.3)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

packed struct difficults (CW 6.3)

2,915 Views
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

Labels (1)
0 Kudos
Reply
2 Replies

1,072 Views
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 Kudos
Reply

1,072 Views
mjbcswitzerland
Specialist V
Hi Neil

Many thanks, that is what it needed!

Best regards

Mark

0 Kudos
Reply