This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

USB: sending an structure on InReport instead of BYTE typedef

I want to know how to modify the next function to send an structure and no the typedef BYTE;

USB_WriteEP(0x81,InReport, sizeof(InReport));


the structure is:

typedef struct{
unsigned char   Header;
RTC_TIME TStamp;        // Timestamp Hs,Ms,Ss,mS.
unsigned char IMU[18];
}S_IMUPACKET;


May be there is another way to send the data. I'm all ears :)
Thanks..

Parents
  • You can send any buffer, including structure, using USB_WriteEP()

    typedef struct{
    unsigned char   Header;
    RTC_TIME TStamp;        // Timestamp Hs,Ms,Ss,mS.
    unsigned char IMU[18];
    }S_IMUPACKET;
    
    S_IMUPACKET InReport_of_struct;
    
    USB_WriteEP(0x81, &InReport_of_struct, sizeof(InReport_of_struct));
    

    Of course, you have to declare this size of input report in the report descriptor, if you are working on HID.

    #define INREPORT_SIZE    sizeof(InReport_of_struct)
    
    /* HID Report Descriptor */
    const BYTE HID_ReportDescriptor[] = {
      HID_UsagePageVendor( 0x00                                   ),
      HID_Usage(           0x01                                   ),
      HID_Collection(      HID_Application                        ),
        HID_LogicalMin(    0                                      ),
        HID_LogicalMaxS(   0xFF                                   ),
        HID_ReportSize(    8                                      ),  // bits
        HID_ReportCount(   INREPORT_SIZE                          ),  // bytes
        HID_Usage(         0x01                                   ),
        HID_Input(         HID_Data | HID_Variable | HID_Absolute ),
      HID_EndCollection,
    };
    





    The problem lies in the handling of the word alignment and padding of members of the structure.
    On the firmware side, there are three strategy.

    a) Apply 'natural' alignment with padding, send it as is, convert it on the host app side
    b) Convert natural alignment to the packed one just before sending the structure to the host
    c) Apply packed one throughout the firmware.

    I can't tell which one is better for a) and b), though c) seems too inefficient.
    a) increases the required bandwidth of USB communication, but it is trivial for HID.

    Other than above packed structure, the problem lies in the difference of the byte order for the compilers of the firmware and host app.

    Tsuneo

Reply
  • You can send any buffer, including structure, using USB_WriteEP()

    typedef struct{
    unsigned char   Header;
    RTC_TIME TStamp;        // Timestamp Hs,Ms,Ss,mS.
    unsigned char IMU[18];
    }S_IMUPACKET;
    
    S_IMUPACKET InReport_of_struct;
    
    USB_WriteEP(0x81, &InReport_of_struct, sizeof(InReport_of_struct));
    

    Of course, you have to declare this size of input report in the report descriptor, if you are working on HID.

    #define INREPORT_SIZE    sizeof(InReport_of_struct)
    
    /* HID Report Descriptor */
    const BYTE HID_ReportDescriptor[] = {
      HID_UsagePageVendor( 0x00                                   ),
      HID_Usage(           0x01                                   ),
      HID_Collection(      HID_Application                        ),
        HID_LogicalMin(    0                                      ),
        HID_LogicalMaxS(   0xFF                                   ),
        HID_ReportSize(    8                                      ),  // bits
        HID_ReportCount(   INREPORT_SIZE                          ),  // bytes
        HID_Usage(         0x01                                   ),
        HID_Input(         HID_Data | HID_Variable | HID_Absolute ),
      HID_EndCollection,
    };
    





    The problem lies in the handling of the word alignment and padding of members of the structure.
    On the firmware side, there are three strategy.

    a) Apply 'natural' alignment with padding, send it as is, convert it on the host app side
    b) Convert natural alignment to the packed one just before sending the structure to the host
    c) Apply packed one throughout the firmware.

    I can't tell which one is better for a) and b), though c) seems too inefficient.
    a) increases the required bandwidth of USB communication, but it is trivial for HID.

    Other than above packed structure, the problem lies in the difference of the byte order for the compilers of the firmware and host app.

    Tsuneo

Children