AnsweredAssumed Answered

Modifying USB Generic HID Example Code for Custom Report Descriptors

Question asked by Nick Guzzardo on Apr 17, 2019
Latest reply on May 7, 2019 by jeremyzhou

*** After all the discussion below and others I created a summary post here ***

 

I have successfully used the generic HID example code to echo data sent from the PC to my target processor back to the PC with modifications to high-speed and packet size of 1024-bytes.

 

https://community.nxp.com/thread/484970

 

I went on to add my own tasks under FreeRTOS as well as adjust the task priorities for the purpose of justifying the use of USB HID for our instrument's communication interface.  With that complete, I'm on to the task of customizing the reports via the report descriptor and data structures.  After much debate the only significant change we want is to change the report size from 8-bits to 32-bits.  To remain within the 1kB packet size, this also requires the report count to change from 1024 to 256 as well as the logical min and max.  With the help of the report descriptor tutorial and the tool for generating the hex for the report descriptor, I made the following changes:

 

Report Descriptor 8-bit reports and 1024 report count

USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceHidGenericReportDescriptor[] = {
   0x05U, 0x81U,          /* Usage Page (Vendor defined)*/
   0x09U, 0x82U,          /* Usage (Vendor defined) */
   0xA1U, 0x01U,          /* Collection (Application) */
   0x09U, 0x83U,          /* Usage (Vendor defined) */
   0x15U, 0x80U,          /* Logical Minimum (-128) */
   0x25U, 0x7FU,          /* Logical Maximum (127) */
   0x75U, 0x08U,          /* Report Size (8U) */
   0x96U, 0x00U, 0x04U,   /* Report Count (1024U) */
   0x81U, 0x02U,          /* Input(Data, Variable, Absolute) */
   0x09U, 0x84U,          /* Usage (Vendor defined) */
   0x15U, 0x80U,          /* Logical Minimum (-128) */
   0x25U, 0x7FU,          /* Logical Maximum (127) */
   0x75U, 0x08U,          /* Report Size (8U) */
   0x96U, 0x00U, 0x04U,   /* Report Count (1024U) */
  0x91U, 0x02U,           /* Output(Data, Variable, Absolute) */

   0xC0U, /* End collection */
};

 

Report Descriptor 32-bit reports and 256 report count

USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceHidGenericReportDescriptor[] = {
   0x05U, 0x81U,                      /* Usage Page (Vendor defined)*/
   0x09U, 0x82U,                      /* Usage (Vendor defined) */
   0xA1U, 0x01U,                      /* Collection (Application) */
   0x09U, 0x83U,                      /* Usage (Vendor defined) */
   0x17U, 0x00U, 0x00U, 0x00U, 0x80U, /* Logical Minimum (-2,147,483,648) */
   0x27U, 0xFFU, 0xFFU, 0xFFU, 0x7FU, /* Logical Maximum (2,147,483,647) */
   0x75U, 0x20U,                      /* Report Size (32U) */
   0x96U, 0x00U, 0x01U,               /* Report Count (256U) */
   0x81U, 0x02U,                      /* Input(Data, Variable, Absolute) */
   0x09U, 0x84U,                      /* Usage (Vendor defined) */
   0x17U, 0x00U, 0x00U, 0x00U, 0x80U, /* Logical Minimum (-2,147,483,648) */
   0x27U, 0xFFU, 0xFFU, 0xFFU, 0x7FU, /* Logical Maximum (2,147,483,647) */
   0x75U, 0x20U,                      /* Report Size (32U) */
   0x96U, 0x00U, 0x01U,               /* Report Count (256U) */
   0x91U, 0x02U,                      /* Output(Data, Variable, Absolute) */
   0xC0U, /* End collection */
};

 

Where I am struggling now is finding documentation on how to associate the structure defined in the report descriptor with a structure for using the data in my application.

 

As I see it, there are two arrays acting as data buffers.

 

USB_DMA_NONINIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE) static uint32_t s_GenericBuffer0[USB_HID_GENERIC_IN_BUFFER_LENGTH >> 2];
USB_DMA_NONINIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE) static uint32_t s_GenericBuffer1[USB_HID_GENERIC_IN_BUFFER_LENGTH >> 2];

 

The value of "USB_HID_GENERIC_IN_BUFFER_LENGTH" is equal to the size of the USB packet in bytes which is 1024, but the buffers have been declared as uint32 so the array size was divided by 4.  When the buffer is used in the application, the buffers are cast as unint8 which matches the report size in the report descriptor.

 

In my specific case, I believe it is effectively done as long as I don't re-cast the buffers since it's already in 32-bit elements.  But what if I had several reports defined in various sizes?  Would I simply transfer the entire packet to an array of n-bytes and then cast it to whatever structure I deem appropriate?

 

If that is the case, then what is the point of creating the report descriptor at all?  Wouldn't it be just as effective and far more simple to just add the number of bits I need, make that the report size with a report count of 1?

Outcomes