STM32 USB主机+HID摇杆

Posted

技术标签:

【中文标题】STM32 USB主机+HID摇杆【英文标题】:STM32 USB Host + HID Joystick 【发布时间】:2022-01-23 18:48:07 【问题描述】:

USB HID 操纵杆连接到我的基于 STM32F401 的开发板。我使用了最新版本的 CubeMX、USB_HOST 库和 HAL。一切都可以使用鼠标和键盘自动运行,但不能使用操纵杆。我根据 USBlyzer 数据在库中做了如下改动:

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

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

我在创建 IN 管道时注释掉了代码的所有部分

  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);
    usbh_hid.h

为我的操纵杆添加非启动代码

/* 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
    usbh_hid.c

添加

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

将协议更改为非启动

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

我的代码在USB_HID_ClassRequest 函数中获取USB_HID_GetHIDReportDescriptor 并得到它。但随后它在USB_HID_Process 中的USB_HID_GetReport 函数上发生故障,我总是得到USB_NOT_SUPPORTED 的状态。我需要获取报告,我的函数会处理数据,但我无法获取报告并且真的不明白为什么。

因此,我需要你的帮助,也许有人已经使用过类似的 HID 设备和 USB_HOST 库。

来自 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  

【问题讨论】:

【参考方案1】:

我已经通过引用链接修复了这个错误:http://enjointhing.cn/?p=325

【讨论】:

是的,它对我有用!我的问题是我错误地为报告分配了缓冲区

以上是关于STM32 USB主机+HID摇杆的主要内容,如果未能解决你的问题,请参考以下文章

利用STM32CubeMX来生成USB_HID_Mouse工程添加ADC

62 stm32 usb自定义hid复合设备修改实验

62 stm32 usb自定义hid复合设备修改实验

62 stm32 usb自定义hid复合设备修改实验

STM32F7 发现 - USB FS 主机/设备模式检测

利用STM32CubeMX来生成USB_HID_Mouse工程