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..
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
The fourth alternative is to manually write code to repack the struct into a raw array of bytes, according to a - by you - predefined byte order.
Agree Per, it ensures the portability/ reusability of the code.
Anyway, the bottom line is holding a precise description of each byte of the packet, on a document. The choice is at your own policy.
Thank You for the options
The last one works fine, but the others are very interesting too..
Thanks again ;)