利用STM32CubeMX来生成USB_HID_Mouse工程

 

 

硬件开发板:STM32F103C8

软件平台

 

好了现在开始利用STM32CubeMX来生成我们的工程

1、新建工程

选择MCU的型号

选择选择时钟

开启usb的模块

 

 选择USB的类

 

 配置时钟树(主要是设置usb的48Mhz)

设置工程路径和编译环境

打开工程

我们现在在main.c进行修改

/* USER CODE BEGIN Includes */
#include "usbd_hid.h"
/* USER CODE END Includes */
  /* USER CODE BEGIN 1 */
  
  // HID Mouse
  struct mouseHID_t {
      uint8_t buttons;
      int8_t x;
      int8_t y;
      int8_t wheel;
  };
  struct mouseHID_t mouseHID;
  
  mouseHID.buttons = 0;
  mouseHID.x = 10;
  mouseHID.y = 0;
  mouseHID.wheel = 0;
  
  /* USER CODE END 1 */
  /* USER CODE BEGIN 3 */
 
  // Send HID report
    mouseHID.x = 10;
    USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t*)&mouseHID, sizeof(struct mouseHID_t));
    HAL_Delay(1000);
  }
  /* USER CODE END 3 */

 编译下载后复位

在电脑设备管理中可以看到一个新的USB输入设备(到这部时候可以看到我们的STM32的USB枚举成功)

可以看到我们鼠标光标移动

 

如果出现

***JLink Error: Bad JTAG communication: Write to IR: Expected 0x1, got 0xF (TAP Command : 10) @ Off 0x5.

将在HAL_MspInit()代码中 __HAL_AFIO_REMAP_SWJ_DISABLE();给注释掉

 

为了回顾之前的浅析USB HID ReportDesc (HID报告描述符)

我们将对static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]进行分析

源码

__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]  __ALIGN_END =
{
  0x05,   0x01,
  0x09,   0x02,
  0xA1,   0x01,
  0x09,   0x01,
  
  0xA1,   0x00,
  0x05,   0x09,
  0x19,   0x01,
  0x29,   0x03,
  
  0x15,   0x00,
  0x25,   0x01,
  0x95,   0x03,
  0x75,   0x01,
  
  0x81,   0x02,
  0x95,   0x01,
  0x75,   0x05,
  0x81,   0x01,
  
  0x05,   0x01,
  0x09,   0x30,
  0x09,   0x31,
  0x09,   0x38,
  
  0x15,   0x81,
  0x25,   0x7F,
  0x75,   0x08,
  0x95,   0x03,
  
  0x81,   0x06,
  0xC0,   0x09,
  0x3c,   0x05,
  0xff,   0x09,
  
  0x01,   0x15,
  0x00,   0x25,
  0x01,   0x75,
  0x01,   0x95,
  
  0x02,   0xb1,
  0x22,   0x75,
  0x06,   0x95,
  0x01,   0xb1,
  
  0x01,   0xc0
}; 
View Code

 注释整理后

__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]  __ALIGN_END =
{
  0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
  0x09, 0x02,                    // USAGE (Mouse)
  0xa1, 0x01,                    // COLLECTION (Application)
  0x09, 0x01,                    //   USAGE (Pointer)  
  0xa1, 0x00,                    //   COLLECTION (Physical)
  0x05, 0x09,                    //     USAGE_PAGE (Button)
  0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
  0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)  
  0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
  0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
  0x95, 0x03,                    //     REPORT_COUNT (3)
  0x75, 0x01,                    //     REPORT_SIZE (1)  
  0x81, 0x02,                    //     INPUT (Data,Var,Abs)
  0x95, 0x01,                    //     REPORT_COUNT (1)
  0x75, 0x05,                    //     REPORT_SIZE (5)
  0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
  0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
  0x09, 0x30,                    //     USAGE (X)
  0x09, 0x31,                    //     USAGE (Y)
  0x09, 0x38,                    //     USAGE (Wheel)
  0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
  0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
  0x75, 0x08,                    //     REPORT_SIZE (8)
  0x95, 0x03,                    //     REPORT_COUNT (3)  
  0x81, 0x06,                    //     INPUT (Data,Var,Rel)
  0xC0,                          //   END_COLLECTION  
  0x09, 0x3c,                    //     USAGE (Motion Wakeup)  
  0x05, 0xff,                    //     USAGE_PAGE (0xff) //在HID Usage Tables 1.12中15 页中UASE PAGES表格为Reserved
  0x09, 0x01,                    //     USAGE (SMBBatteryMode)  
  0x15, 0x00,                    //     LOGICAL_MINIMUM (0)   
  0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
  0x75, 0x01,                    //     REPORT_SIZE (1)    
  0x95, 0x02,                    //     REPORT_COUNT (2)
  0xb1, 0x22,                    //     FEATURE(Data,Var,Abs,NPrf)
  0x75, 0x06,                    //     REPORT_SIZE (6)
  0x95, 0x01,                    //     REPORT_COUNT (1)
  0xb1, 0x01,                    //     FEATURE(Cnst,Ary,Abs)
  0xc0                           // END_COLLECTION
}; 

 现在各位同学请翻开《Universal Serial Bus Specification Revision 2.0 》<9.5 Descriptors >

我们参考Table 9-8. Standard Device Descriptor 将__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] 数据也按照这个表格填入数据

__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC]的源码如下:

/* USB Standard Device Descriptor */
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
  {
    0x12,                       /*bLength */
    USB_DESC_TYPE_DEVICE,       /*bDescriptorType*/
    0x00,                       /* bcdUSB */  
    0x02,
    0x00,                       /*bDeviceClass*/
    0x00,                       /*bDeviceSubClass*/
    0x00,                       /*bDeviceProtocol*/
    USB_MAX_EP0_SIZE,          /*bMaxPacketSize*/
    LOBYTE(USBD_VID),           /*idVendor*/
    HIBYTE(USBD_VID),           /*idVendor*/
    LOBYTE(USBD_PID_FS),           /*idVendor*/
    HIBYTE(USBD_PID_FS),           /*idVendor*/
    0x00,                       /*bcdDevice rel. 2.00*/
    0x02,
    USBD_IDX_MFC_STR,           /*Index of manufacturer  string*/
    USBD_IDX_PRODUCT_STR,       /*Index of product string*/
    USBD_IDX_SERIAL_STR,        /*Index of serial number string*/
    USBD_MAX_NUM_CONFIGURATION  /*bNumConfigurations*/
  } ; 
/* USB_DeviceDescriptor */
View Code

 这个分析起来有点麻烦

/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ]  __ALIGN_END =
{
  0x09, /* bLength: Configuration Descriptor size */
  USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
  USB_HID_CONFIG_DESC_SIZ,
  /* wTotalLength: Bytes returned */
  0x00,
  0x01,         /*bNumInterfaces: 1 interface*/
  0x01,         /*bConfigurationValue: Configuration value*/
  0x00,         /*iConfiguration: Index of string descriptor describing
  the configuration*/
  0xE0,         /*bmAttributes: bus powered and Support Remote Wake-up */
  0x32,         /*MaxPower 100 mA: this current is used for detecting Vbus*/
  
  /************** Descriptor of Joystick Mouse interface ****************/
  /* 09 */
  0x09,         /*bLength: Interface Descriptor size*/
  USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
  0x00,         /*bInterfaceNumber: Number of Interface*/
  0x00,         /*bAlternateSetting: Alternate setting*/
  0x01,         /*bNumEndpoints*/
  0x03,         /*bInterfaceClass: HID*/
  0x01,         /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
  0x02,         /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
  0,            /*iInterface: Index of string descriptor*/
  /******************** Descriptor of Joystick Mouse HID ********************/
  /* 18 */
  0x09,         /*bLength: HID Descriptor size*/
  HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
  0x11,         /*bcdHID: HID Class Spec release number*/
  0x01,
  0x00,         /*bCountryCode: Hardware target country*/
  0x01,         /*bNumDescriptors: Number of HID class descriptors to follow*/
  0x22,         /*bDescriptorType*/
  HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
  0x00,
  /******************** Descriptor of Mouse endpoint ********************/
  /* 27 */
  0x07,          /*bLength: Endpoint Descriptor size*/
  USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
  
  HID_EPIN_ADDR,     /*bEndpointAddress: Endpoint Address (IN)*/
  0x03,          /*bmAttributes: Interrupt endpoint*/
  HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */
  0x00,
  HID_FS_BINTERVAL,          /*bInterval: Polling Interval (10 ms)*/
  /* 34 */
} ;
View Code

 

Table 9-10. Standard Configuration Descriptor

 

 Table 9-12. Standard Interface Descriptor 

 

 这边需要参考<Device Class Definition for Human Interface Devices (HID) Version 1.11 > "6.2.1 HID Descriptor"

 

 Table 9-13. Standard Endpoint Descriptor

 

现在利用软件直接看这个上面的信息

 

 

Connection StatusDevice connected
Current Configuration1
SpeedFull (12 Mbit/s)
Device Address12
Number Of Open Pipes1
Device Descriptor STM32 Human interface
OffsetFieldSizeValueDescription
0bLength112h 
1bDescriptorType101hDevice
2bcdUSB20200hUSB Spec 2.0
4bDeviceClass100hClass info in Ifc Descriptors
5bDeviceSubClass100h 
6bDeviceProtocol100h 
7bMaxPacketSize0140h64 bytes
8idVendor20483hSGS Thomson Microelectronics
10idProduct2572Bh 
12bcdDevice20200h2.00
14iManufacturer101h"STMicroelectronics"
15iProduct102h"STM32 Human interface"
16iSerialNumber103h"00000000001A"
17bNumConfigurations101h 
Configuration Descriptor 1
OffsetFieldSizeValueDescription
0bLength109h 
1bDescriptorType102hConfiguration
2wTotalLength20022h 
4bNumInterfaces101h 
5bConfigurationValue101h 
6iConfiguration100h 
7bmAttributes1E0hSelf Powered, Remote Wakeup
 4..0: Reserved ...00000  
 5: Remote Wakeup ..1..... Yes
 6: Self Powered .1...... Yes
 7: Reserved (set to one)
(bus-powered for 1.0)
 1.......  
8bMaxPower132h100 mA
Interface Descriptor 0/0 HID, 1 Endpoint
OffsetFieldSizeValueDescription
0bLength109h 
1bDescriptorType104hInterface
2bInterfaceNumber100h 
3bAlternateSetting100h 
4bNumEndpoints101h 
5bInterfaceClass103hHID
6bInterfaceSubClass101hBoot Interface
7bInterfaceProtocol102hMouse
8iInterface100h 
HID Descriptor
OffsetFieldSizeValueDescription
0bLength109h 
1bDescriptorType121hHID
2bcdHID20111h1.11
4bCountryCode100h 
5bNumDescriptors101h 
6bDescriptorType122hReport
7wDescriptorLength2004Ah74 bytes
Endpoint Descriptor 81 1 In, Interrupt, 10 ms
OffsetFieldSizeValueDescription
0bLength107h 
1bDescriptorType105hEndpoint
2bEndpointAddress181h1 In
3bmAttributes103hInterrupt
 1..0: Transfer Type ......11 Interrupt
 7..2: Reserved 000000..  
4wMaxPacketSize20004h4 bytes
6bInterval10Ah10 ms
Interface 0 HID Report Descriptor Mouse

Item Tag (Value)Raw Data
Usage Page (Generic Desktop)05 01 
Usage (Mouse)09 02 
Collection (Application)A1 01 
    Usage (Pointer)09 01 
    Collection (Physical)A1 00 
        Usage Page (Button)05 09 
        Usage Minimum (Button 1)19 01 
        Usage Maximum (Button 3)29 03 
        Logical Minimum (0)15 00 
        Logical Maximum (1)25 01 
        Report Count (3)95 03 
        Report Size (1)75 01 
        Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit)81 02 
        Report Count (1)95 01 
        Report Size (5)75 05 
        Input (Cnst,Ary,Abs)81 01 
        Usage Page (Generic Desktop)05 01 
        Usage (X)09 30 
        Usage (Y)09 31 
        Usage (Wheel)09 38 
        Logical Minimum (-127)15 81 
        Logical Maximum (127)25 7F 
        Report Size (8)75 08 
        Report Count (3)95 03 
        Input (Data,Var,Rel,NWrp,Lin,Pref,NNul,Bit)81 06 
    End CollectionC0 
    Usage (Motion Wakeup)09 3C 
    Usage Page05 FF 
    Usage09 01 
    Logical Minimum (0)15 00 
    Logical Maximum (1)25 01 
    Report Size (1)75 01 
    Report Count (2)95 02 
    Feature (Data,Var,Abs,NWrp,Lin,NPrf,NNul,NVol,Bit)B1 22 
    Report Size (6)75 06 
    Report Count (1)95 01 
    Feature (Cnst,Ary,Abs,NWrp,Lin,Pref,NNul,NVol,Bit)B1 01 
End CollectionC0 

转载于:https://www.cnblogs.com/libra13179/p/6841436.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32F103C8 Serial(UART) to USB HID Keyboard Mouse Joystick 串口 转 USB键盘;鼠标;手柄 (1) 使用Composite Device 组合(复合)设备 (1.1) 1个Device -> 1个 Configuation -> 3个Interfance (Keyboard & Mouse & Joystick) (1.2) 支持BIOS模式中的操作(使用HID boot模式) 以便兼容在计算器中设定BIOS : (1.2.1) Keyboard Interfance -> HID (boot mode) -> 2个Endpoint(IN_0x81 & OUT_0x01) -> KeyboardReportDescriptor(不使用Report ID) (1.2.2) Mouse Interfance -> HID (boot mode) -> 1个Endpoint(IN_0x82) -> MouseReportDescriptor(不使用Report ID) (1.3) 支持Windows模式中的操作(使用Report ID) : (1.3.1) Multimedia Keyboard Interfance -> HID -> 1个Endpoint(IN_0x85) -> KeyboardReportDescriptor(使用Report ID) (1.3.1.1) Report ID (1) : HID Usage Page 0x0C, MediaKey (1.3.1.2) Report ID (2) : HID Usage Page 0x01, PowerControl (1.3.2) Mouse Interfance -> HID (boot mode) -> 1个Endpoint(IN_0x84) -> MouseReportDescriptor(使用Report ID) (1.3.2.1) Report ID (1) : 相对坐标 (-32768 ~ 32767) (1.3.2.2) Report ID (2) : 绝对坐标 (0 ~ 2048) (1.4) 支持反馈Keyboard_LED灯号: All Off; Num Lock; Caps Lock; Scroll Lock; Compose; Kana (1.5) Joystick Interfance -> HID -> 1个Endpoint(IN_0x83) -> JoyStickReportDescriptor (1.7) 支持GPIO命令, 可控制 12个GPIO 0/1 (hi/low)输出 (1.8) 支持Hardware或Software 插拔(Connect/Disconnect)命令 (1.9) 支持Software Reset命令 (2) 串口接收 命令 (2.1) UART协议: 115200, n, 8, 1 (2.2) 1帧发送字符串格式, 以 '{'开始, '}'结束 ','分隔 共10个10进制数字 例如: {1,2,3,4,5,6,7,8,9,10} (2.3) 第1位 区分 Keyboard(128) 或是 Mouse(64) 或是 Joystick(32) 或是 Control(32) 命令 Control_cmd = 0x10, Joystick_cmd = 0x20, Mouse_cmd = 0x40, MouseHold_cmd = 0x41, AbsMouse_cmd = 0x42, AbsMouseHold_cmd= 0x43, Keyboard_cmd = 0x80, KeyboardHold_cmd= 0x81, MediaKey_cmd = 0x82, MediaKeyHold_cmd= 0x83, Switch_cmd = 0xC0, SwitchHold_cmd = 0xC1 例如: {16, 0,0,0,0,0,0,0,0} --- 发送Control命令 {32, 0,0,0,0,0,0,0,0} --- 发送Joystick命令 {64, 0,0,0,0,0,0,0,0} --- 发送Mouse命令(相对坐标) {65, 0,0,0,0,0,0,0,0} --- 发送Mouse命令(相对坐标)(按住不放) {66, 0,0,0,0,0,0,0,0} --- 发送WinMouse命令(相对坐标/绝对坐标) {67, 0,0,0,0
STM32F103C8 Serial(UART) to USB HID Keyboard Mouse 串口 转 USB键盘鼠标 (1) 使用Composite Device 组合(复合)设备 (1.1) 1个Device -> 1个 Configuation -> 2个Interfance (Keyboard & Mouse) (1.2) Keyboard Interfance -> HID (boot mode) -> 2个Endpoint(IN_0x81 & OUT_0x01) -> KeyboardReportDescriptor(不使用Report ID) (1.3) Mouse Interfance -> HID (boot mode) -> 1个Endpoint(IN_0x82) -> MouseReportDescriptor(不使用Report ID) (1.4) 使用HID boot模式, 不使用Report ID, 以便兼容在 计算器设定BIOS模式 中的操作 (2) 串口接收 命令 (2.1) UART协议: 115200, n, 8, 1 (2.2) 1帧发送字符串格式, 以 '{'开始; '}'结束; ','分隔. 共9个10进制数字 例如: {1,2,3,4,5,6,7,8,9} (2.3) 第9位 区分 Keyboard(64) 或是 Mouse(128) 命令 例如: {0,0,0,0,0,0,0,0,64} --- 发送Keyboard命令 {0,0,0,0,0,0,0,0,128} --- 发送Keyboard命令 (3) 发送Keyboard键盘命令时 : 第1~8位 分别如下 (3.1) 第1位 : Key_Release = 0x00, Left_Control = 0x01, Left_Shift = 0x02, Left_Alt = 0x04, Left_GUI = 0x08, Right_Control = 0x10, Right_Shift = 0x20, Right_Alt = 0x40, Right_GUI = 0x80, 例如: {8,0,0,0,0,0,0,0,64} --- 发送 Win_Key键 {128,0,0,0,0,0,0,0,64} --- 发送 WinApp_Key键 {32,0,0,0,0,0,0,0,64} --- 发送 右Shift键 (3.2) 第2位 : 保留,不使用,一律填0 (3.3) 第3~8位 : 可以同时发送6个Keyboard按键 例如: {0,0,4,5,6,7,8,9,64} --- 发送 'abcdef'键 {2,0,4,5,6,7,8,9,64} --- 按住 左Shift 发送 'abcdef'键 => 'ABCDEF' {0,0,0,5,0,7,0,9,64} --- 发送 'bdf'键 (0表示 无按键) 按键码 可参阅: (HID Usage ID) http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf https://www.hiemalis.org/~keiji/PC/scancode-translate.pdf https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2 http://www.usb.org/developers/hidpage/Hut1_12v2.pdf (4) 发送Mouse鼠标命令时 : 第1~8位 分别如下 (4.1) 第1位 : Button_Release = 0x00, Left_Button = 0x01, Right_Button = 0x02, Mid_Button = 0x04, 例如: {1,0,0,0,0,0,0,0,128} --- 点击 左键 {2,0,0,0,0,0,0,0,128} --- 点击 右键 {4,0,0,0,0,0,0,0,128} --- 点击 中键 (4.2) 第2~4位 : 移动(X,Y), 滚轮(Wheel) X: -127~127:左右移动鼠标 Y: -127~127:上下移动鼠标 Wheel: -127~127:上下转动滚轮 例如: {0,20,-10,0,0,0,0,0,128} --- 鼠标 右移20,上移10 {0,0,0,-30,0,0,0,0,128} --- 滚轮-30 (4.2) 第5~8位 : 保留,不使用,一律填0

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值