Home > Software design >  STM32 USB Host HID Joystick
STM32 USB Host HID Joystick

Time:12-24

A USB HID Joystick is connected to my STM32F401-based board. I used last versions CubeMX, the USB_HOST library, and HAL. Everything works automatically with the mouse and keyboard, but not with the joystick. I made the following changes in the library based on USBlyzer data:

  1. usbh_conf.h
/*----------   -----------*/
#define USBH_MAX_NUM_ENDPOINTS      1U

/*----------   -----------*/
#define USBH_MAX_NUM_INTERFACES      1U
  1. usbh_core.c

I commented out all sections of the code with the creation of the IN pipe

  phost->gState = HOST_ENUMERATION;

  phost->Control.pipe_out = USBH_AllocPipe(phost, 0x00U);
  // phost->Control.pipe_in  = USBH_AllocPipe(phost, 0x80U);

  /* Open Control pipes */
  // USBH_OpenPipe(phost, phost->Control.pipe_in, 0x80U,
  //               phost->device.address, phost->device.speed,
  //               USBH_EP_CONTROL, (uint16_t)phost->Control.pipe_size);

  /* Open Control pipes */
  USBH_OpenPipe(phost, phost->Control.pipe_out, 0x00U,
                phost->device.address, phost->device.speed,
                USBH_EP_CONTROL, (uint16_t)phost->Control.pipe_size);
  1. usbh_hid.h

Add nonboot code for my joystick

/* Interface Descriptor field values for HID Boot Protocol */
#define HID_BOOT_CODE                                 0x01U
#define HID_NONE_BOOT_CODE                            0x00U
#define HID_KEYBRD_BOOT_CODE                          0x01U
#define HID_MOUSE_BOOT_CODE                           0x02U
  1. usbh_hid.c

Add

  else if (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceProtocol  == HID_NONE_BOOT_CODE)
  {
    USBH_UsrLog("Non boot device found!");
  }

Changed the protocol to non-boot

case HID_REQ_SET_PROTOCOL:
    /* set protocol */
    classReqStatus = USBH_HID_SetProtocol(phost, 1U);
    if (classReqStatus == USBH_OK)
    {
      HID_Handle->ctl_state = HID_REQ_IDLE;

My code goes as far as getting the USB_HID_GetHIDReportDescriptor in the USB_HID_ClassRequest function and gets it. But then it breaks down on the USB_HID_GetReport function in USB_HID_Process, where I always get the status USB_NOT_SUPPORTED. I need to get a report, my functions will process the data, but I can't get a report and really don't understand why.

Therefore, I need your help, maybe someone has already worked with similar HID devices and USB_HOST library.

Data from USBlyzer:

Connection Status Device connected 
Current Configuration 1 
Speed Full (12 Mbit/s) 
Device Address 6 
Number Of Open Pipes 1 

Device Descriptor VKB-Sim Space Gunfighter L 
Offset Field Size Value Description 
0 bLength 1 12h  
1 bDescriptorType 1 01h Device 
2 bcdUSB 2 0200h USB Spec 2.0 
4 bDeviceClass 1 00h Class info in Ifc Descriptors 
5 bDeviceSubClass 1 00h  
6 bDeviceProtocol 1 00h  
7 bMaxPacketSize0 1 08h 8 bytes 
8 idVendor 2 231Dh  
10 idProduct 2 0127h  
12 bcdDevice 2 1997h 19.97 
14 iManufacturer 1 01h ". Alex Oz 2012-2020" 
15 iProduct 1 02h " VKB-Sim Space Gunfighter L " 
16 iSerialNumber 1 00h  
17 bNumConfigurations 1 01h  

Configuration Descriptor 1 Bus Powered, 500 mA
Offset Field Size Value Description 
0 bLength 1 09h  
1 bDescriptorType 1 02h Configuration 
2 wTotalLength 2 0022h  
4 bNumInterfaces 1 01h  
5 bConfigurationValue 1 01h  
6 iConfiguration 1 00h  
7 bmAttributes 1 80h Bus Powered 
 4..0: Reserved  ...00000   
 5: Remote Wakeup  ..0.....  No 
 6: Self Powered  .0......  No, Bus Powered 
 7: Reserved (set to one)
(bus-powered for 1.0)  1.......   
8 bMaxPower 1 FAh 500 mA 

Interface Descriptor 0/0 HID, 1 Endpoint
Offset Field Size Value Description 
0 bLength 1 09h  
1 bDescriptorType 1 04h Interface 
2 bInterfaceNumber 1 00h  
3 bAlternateSetting 1 00h  
4 bNumEndpoints 1 01h  
5 bInterfaceClass 1 03h HID 
6 bInterfaceSubClass 1 00h  
7 bInterfaceProtocol 1 00h  
8 iInterface 1 00h  

HID Descriptor 
Offset Field Size Value Description 
0 bLength 1 09h  
1 bDescriptorType 1 21h HID 
2 bcdHID 2 0111h 1.11 
4 bCountryCode 1 00h  
5 bNumDescriptors 1 01h  
6 bDescriptorType 1 22h Report 
7 wDescriptorLength 2 012Dh 301 bytes 

Endpoint Descriptor 81 1 In, Interrupt, 1 ms
Offset Field Size Value Description 
0 bLength 1 07h  
1 bDescriptorType 1 05h Endpoint 
2 bEndpointAddress 1 81h 1 In 
3 bmAttributes 1 03h Interrupt 
 1..0: Transfer Type  ......11  Interrupt 
 7..2: Reserved  000000..   
4 wMaxPacketSize 2 0040h 64 bytes 
6 bInterval 1 01h 1 ms 

Interface 0 HID Report Descriptor Joystick
Item Tag (Value) Raw Data 
Usage Page (Generic Desktop) 05 01  
Usage (Joystick) 09 04  
Collection (Application) A1 01  
    Usage Page (Generic Desktop) 05 01  
    Report ID (1) 85 01  
    Usage Page (Generic Desktop) 05 01  
    Usage (X) 09 30  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (4095) 26 FF 0F  
    Physical Maximum (4095) 46 FF 0F  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Generic Desktop) 05 01  
    Usage (Y) 09 31  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (4095) 26 FF 0F  
    Physical Maximum (4095) 46 FF 0F  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Generic Desktop) 05 01  
    Usage (Rx) 09 33  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (2047) 26 FF 07  
    Physical Maximum (2047) 46 FF 07  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Generic Desktop) 05 01  
    Usage (Ry) 09 34  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (2047) 26 FF 07  
    Physical Maximum (2047) 46 FF 07  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Generic Desktop) 05 01  
    Usage (Rz) 09 35  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (2047) 26 FF 07  
    Physical Maximum (2047) 46 FF 07  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Undefined) 05 00  
    Usage 09 00  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (2047) 26 FF 07  
    Physical Maximum (2047) 46 FF 07  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Generic Desktop) 05 01  
    Usage (Z) 09 32  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (2047) 26 FF 07  
    Physical Maximum (2047) 46 FF 07  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Undefined) 05 00  
    Usage 09 00  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (2047) 26 FF 07  
    Physical Maximum (2047) 46 FF 07  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Button) 05 09  
    Usage Minimum (Button 1) 19 01  
    Usage Maximum (Button 128) 2A 80 00  
    Logical Minimum (0) 15 00  
    Logical Maximum (1) 25 01  
    Report Size (1) 75 01  
    Report Count (128) 96 80 00  
    Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02  
    Usage Page (Generic Desktop) 05 01  
    Usage (Hat Switch) 09 39  
    Logical Minimum (0) 15 00  
    Logical Maximum (7) 26 07 00  
    Physical Minimum (0) 35 00  
    Physical Maximum (360) 46 68 01  
    Unit (Eng Rot: Degree) 65 14  
    Unit Exponent (1) 55 01  
    Report Size (4) 75 04  
    Report Count (1) 95 01  
    Input (Data,Var,Abs,NWrp,Lin,Pref,Null,Bit) 81 42  
    Usage (Undefined) 09 00  
    Unit (None) 65 00  
    Unit Exponent (0) 55 00  
    Report Size (4) 75 04  
    Report Count (3) 95 03  
    Input (Cnst,Ary,Abs) 81 01  
    Usage Page (Generic Desktop) 05 01  
    Usage (Undefined) 09 00  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Input (Cnst,Ary,Abs) 81 01  
    Usage Page (Generic Desktop) 05 01  
    Usage (Undefined) 09 00  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Input (Cnst,Ary,Abs) 81 01  
    Usage Page (Generic Desktop) 05 01  
    Usage (Undefined) 09 00  
    Report Size (16) 75 10  
    Report Count (1) 95 01  
    Input (Cnst,Ary,Abs) 81 01  
    Usage Page (Generic Desktop) 05 01  
    Usage (Undefined) 09 00  
    Report Size (8) 75 08  
    Report Count (23) 95 17  
    Input (Cnst,Ary,Abs) 81 01  
    Report ID (11) 85 0B  
    Usage Page (Generic Desktop) 05 01  
    Usage (Undefined) 09 00  
    Report Size (8) 75 08  
    Report Count (63) 95 3F  
    Input (Cnst,Ary,Abs) 81 01  
    Report ID (8) 85 08  
    Usage Page (Generic Desktop) 05 01  
    Usage (Undefined) 09 00  
    Report Size (8) 75 08  
    Report Count (63) 95 3F  
    Input (Cnst,Ary,Abs) 81 01  
    Logical Minimum (0) 15 00  
    Logical Maximum (255) 26 FF 00  
    Physical Maximum (255) 46 FF 00  
    Report ID (88) 85 58  
    Report Size (8) 75 08  
    Report Count (63) 95 3F  
    Usage (Undefined) 09 00  
    Output (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) 91 02  
    Report ID (89) 85 59  
    Report Size (8) 75 08  
    Report Count (128) 95 80  
    Usage (Undefined) 09 00  
    Feature (Data,Var,Abs,NWrp,Lin,Pref,NNul,NVol,Bit) B1 02  
End Collection C0  

CodePudding user response:

i've fixed the bug by refer link with :http://enjointhing.cn/?p=325

  • Related