Union and structure for implementing a USB driver

I am trying to create usb drivers for a printer device using the PIC32mx series. I am using a sample Microchip library application. In this case, I see that the structure below is used to send the BDT. Now the BDT should be in all 8 bytes, as described in the note to the application, But when I check the size of the BD_ENTRY union variable, I see that it is different from 12 bytes. It should be 8 bytes if I am right. I tried to compile this part of the same code with Mikroc and simulate it with protus, and I find that the byte length (sizeof) is 8 bytes.

I am a little confused, also I am new to the world of pointers, structures and unions.

typedef union _BD_STAT {
    BYTE Val;
    struct {
        //If the CPU owns the buffer then these are the values
        unsigned BC8:1;         //bit 8 of the byte count
        unsigned BC9:1;         //bit 9 of the byte count
        unsigned BSTALL:1;      //Buffer Stall Enable
        unsigned DTSEN:1;       //Data Toggle Synch Enable
        unsigned INCDIS:1;      //Address Increment Disable
        unsigned KEN:1;         //BD Keep Enable
        unsigned DTS:1;         //Data Toggle Synch Value
        unsigned UOWN:1;        //USB Ownership
    };

    struct {
        //if the USB module owns the buffer then these are
        // the values
        unsigned :2;
        unsigned PID0:1;        //Packet Identifier
        unsigned PID1:1;
        unsigned PID2:1;
        unsigned PID3:1;
        unsigned :1;
    };

    struct {
        unsigned :2;
        unsigned PID:4;         //Packet Identifier
        unsigned :2;
    };
} BD_STAT;    

typedef union __attribute__ ((packed))__BDT {
    //typedef union __BDT{
    struct __attribute__ ((packed)) {
        //struct   
        BD_STAT     STAT;
        WORD        CNT:10;
        WORD        ADR;                      
        WORD        ADRH;                   
    };
    struct __attribute__ ((packed)) {
        //struct   
        DWORD       res  :16;
        DWORD       count:10;
    };

    DWORD           w[2];
    WORD            v[4];
    QWORD           Val;
} BDT_ENTRY;
+4
source share
2

(), sizeof. , , , ( gcc, , , ) . , unsigned bit:1, Byte BD_STAT, 4- unsigned, BD_STAT . , sizeof Byte , .

0

BD_STAT unsigned char unsigned. - , . , BD_STAT 4 .

0

Source: https://habr.com/ru/post/1528033/


All Articles