cancel
Showing results for 
Search instead for 
Did you mean: 

HID USB Data to the STM32F4 DISCOVERY

vadimgouskov
Associate II
Posted on March 11, 2016 at 12:15

Dear people on the ST Forum.

I am currently working on configuring the STM32F4 DISCO board as a HID device that must send an receive data from a host (PC). I've managed to configure it as a HID mouse and altered the descriptors and reports to send 64byte packets to the PC.I get the correct data from the device, but when I try to send data to the device (YAT terminal) I get an undefinable error and the terminal closes.

Here's what I added to the USB FS project generated in CubeMx so far to try to accomplish this. :

Added an EPOUT descriptor 

 

and changed the wTotallength accordingly in the Conf.Desc. and checked the Descriptors and report descriptors in USBlyzer.

....

  /* 34 */

  //////////////////////EPOUT Descriptor ///////////////////////

  0x07,          /*bLength: Endpoint Descriptor size*/

  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/

  HID_EPOUT_ADDR,    /*bEndpointAddress: Endpoint Address (OUT)*/ // VADIM

  0x03,          /*bmAttributes: Interrupt endpoint*/

  HID_EPOUT_SIZE, /*wMaxPacketSize: 64 Byte max */

  0x00,

  0x0A,          /*bInterval: Polling Interval (10 ms)*/

} ; 

Changed the report Descriptors:

<code>

__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]  __ALIGN_END =

{

  0x06,   0xAB, 0xFF, // Report Descriptor Raw Hid

  0x0A,   0x00, 0x02,

  0xA1,   0x01,

  0x75,   0x08,

  

  0x15,   0x00,

  0x26,   0xFF, 0x00,

  0x95,   0x40,

  0x09,   0x01,

  

  0x81,   0x02,

  0x95,   0x40,

  0x09,   0x02,

  

  0x91,   0x02,

  0xC0

}; </code>

Added a Data out function in the HID callback structure

<code>

static uint8_t  USBD_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);

.....

USBD_ClassTypeDef  USBD_HID = 

{

  USBD_HID_Init,

  USBD_HID_DeInit,

  USBD_HID_Setup,

  NULL, /*EP0_TxSent*/  

  NULL, /*EP0_RxReady*/

  USBD_HID_DataIn, /*DataIn*/

  USBD_HID_DataOut, /*DataOut*/ // <<<<<<

  NULL, /*SOF */

  NULL,

  NULL,      

  USBD_HID_GetCfgDesc,

  USBD_HID_GetCfgDesc, 

  USBD_HID_GetCfgDesc,

  USBD_HID_GetDeviceQualifierDesc,

}; </code>

Declared the USBD_HID_DataOut  function

 

but to my knowledge this function is never entered...

<code>

static uint8_t  USBD_HID_DataOut (USBD_HandleTypeDef *pdev,

                              uint8_t epnum)

{

//printf(''Received \r\n'') ;

  USBD_LL_PrepareReceive(pdev, //VADIM

              HID_EPOUT_ADDR,

             /* (uint8_t*)*/HID_Rx_Buffer,

              HID_EPOUT_SIZE

                          ) ;

  return USBD_OK;

} </code>

And in USBD_HID_INIT opened EPOUT

<code>

static uint8_t  USBD_HID_Init (USBD_HandleTypeDef *pdev, 

                               uint8_t cfgidx)

{

  uint8_t ret = 0;

  

  /* Open EP IN */

  USBD_LL_OpenEP(pdev,

                 HID_EPIN_ADDR,

                 USBD_EP_TYPE_INTR,

                 HID_EPIN_SIZE);  

  /* Open EP OUT */

  USBD_LL_OpenEP(pdev, <<<<<<<<<<<<<<<<

       HID_EPOUT_ADDR,

                 USBD_EP_TYPE_INTR,

                 HID_EPOUT_SIZE);

  

  USBD_LL_PrepareReceive(pdev, <<<<<<<<<<<< 

              HID_EPOUT_ADDR,

              (uint8_t*)HID_Rx_Buffer,

              HID_EPOUT_SIZE

                          ) ;

....

</code>

Does somebody have more experience with HID on the STM32F4 ? For know I can't figure this out..

I followed the approach in this thread for USB HID on the STM32F105/107

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FSTM32F105107%20HID%20USB%20device%20SET_REPORT%2C%20GET_REPORT&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F...

Thanks in Advance,

Vadim , Student at Thomas More - De Nayer

#hid #hid #hid-usb-report-out #!stm32f4-!epout-!hid-!usb #usb #usb #usb #!stm32 #hid
4 REPLIES 4
tsuneo
Senior
Posted on March 12, 2016 at 18:28

Almost fine, as long as the code described on your post.

> Added an EPOUT descriptor and changed the wTotallength accordingly in the Conf.Desc.

Did you change these descriptor fields, too?

Interface descriptor - bNumEndpoints field: 1 --> 2

HID descriptor - wDescriptorLength: --> 0x001C

Tsuneo

vadimgouskov
Associate II
Posted on March 13, 2016 at 19:55

Hi Tsuneo,

Thanks for the reply

Thats's right , I changed :

-HID_MOUSE_REPORT_DESC_SIZE to 0x1C

-numEndpoints to 2

-wTotallength to 41

I checked the desciptors in USBlyzer and they all seem to work out.

I read in UM1021 p.39

''The USBD_HID_SendReport can be used by the application to send HID reports, the HID driver, in this release, handles only IN traffic.''

This worries me a bit as I'm not very comfortable editing lower level functions to make the library handle OUT traffic

In UM1734 on p.18 it says that :''The library passes all the non-standard requests to the class-specific code with the callback

pdev->pClass->Setup

(pdev,

req) function.''

And sould copy the setup request info to the struct usb_setup_req

(usbd_def.h). but when I try to send anything from YAT terminal it doesn't enter this function.Also 

HAL_PCD_IRQhandler is called around 119 times during enumeration and twice every time I call a sendreport. But the IRQ handler is never called when I try to send information from YAT to to the device. Not even as an ''Invalid interrupt''.

monitoring the USB traffic with USBcap in Wireshark it seems to send the packet to the correct device and the correct endpoint..

Did I miss out on something ? It must be possible to receive data from the host in the HID class..

Kind Regards,

Vadim , Student at Thomas More - De Nayer

connectjagadeep
Associate II
Posted on March 15, 2016 at 13:05

Hi,

   1)  First of all you should check is it possible to received data if you configured HID as mouse device. Usually mouse device will have data flow from device to host. If you want to have bidirectional data transfer I recommend you to have custom HID mode. 

  2)  It is important to call  USBD_LL_PrepareReceive function in USBD_CUSTOM_HID_DataOut callback. Which allows host to send data to the device. 

Let me know if this helps. 

Jagadeep.

vadimgouskov
Associate II
Posted on March 15, 2016 at 17:40

Hi Ramjagadeep,

What I did before was start from a HID mouse project and changing the descriptors and report to configure it as a custom HID that could only send data to the terminal. After that trying to add an endpoint and USBD_HID_DataOut callback function, which didn't work out .. 

As you suggested I tried this approach in a new Costum HID project and it worked ! I can now send and receive data to/from my USB device through YAT terminal.

Strange as I modified the Mouse project the same way as Tsuneo and used the same descriptors as in the working project.  But why didn't the OTG_FS_IRQHandler even get called as I sent something. It is the hardware that triggers this right ? Maybe the hw wasn't enabled to trigger an interrupt at an incoming report..

I will look into this further when I made more progress with my project.

Thanks for the tip! I was struggling with this for a while now.