×
单片机 > 单片机程序设计 > 详情

基于stm32的自定义HID设备开发与上位机通讯实现

发布时间:2020-06-15 发布时间:
|

1.HID通信的实现

2.使用USB外设

3.使用HID设备

4.配置时钟


5.USB中断放低点



6.生成代码,替换配置描述符


/* 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*/

  0x01,         /*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 */

} ;

7.替换报告描述符


__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE]  __ALIGN_END =

{

0x05, 0x01,// USAGE_PAGE (Generic Desktop)

0x09, 0x06,// USAGE (Keyboard)

0xa1, 0x01,// COLLECTION (Application)

0x05, 0x07,// USAGE_PAGE (Keyboard)

0x19, 0xe0,// USAGE_MINIMUM (Keyboard LeftControl)

0x29, 0xe7,// USAGE_MAXIMUM (Keyboard Right GUI)

0x15, 0x00,// LOGICAL_MINIMUM (0)

0x25, 0x01,// LOGICAL_MAXIMUM (1)

0x75, 0x01,// REPORT_SIZE (1)

0x95, 0x08,// REPORT_COUNT (8)

0x81, 0x02,// INPUT (Data,Var,Abs)

0x95, 0x01,// REPORT_COUNT (1)

0x75, 0x08,// REPORT_SIZE (8)

0x81, 0x03,// INPUT (Cnst,Var,Abs)

0x95, 0x05,// REPORT_COUNT (5)

0x75, 0x01,// REPORT_SIZE (1)

0x05, 0x08,// USAGE_PAGE (LEDs)

0x19, 0x01,// USAGE_MINIMUM (Num Lock)

0x29, 0x05,// USAGE_MAXIMUM (Kana)

0x91, 0x02,// OUTPUT (Data,Var,Abs)

0x95, 0x01,// REPORT_COUNT (1)

0x75, 0x03,// REPORT_SIZE (3)

0x91, 0x03,// OUTPUT (Cnst,Var,Abs)

0x95, 0x06,// REPORT_COUNT (6)

0x75, 0x08,// REPORT_SIZE (8)

0x15, 0x00,// LOGICAL_MINIMUM (0)

0x25, 0xFF,// LOGICAL_MAXIMUM (255)

0x05, 0x07,// USAGE_PAGE (Keyboard)

0x19, 0x00,// USAGE_MINIMUM (Reserved (no event indicated))

0x29, 0x65,// USAGE_MAXIMUM (Keyboard Application)

0x81, 0x00,// INPUT (Data,Ary,Abs)

0xc0

}; 

8.修改宏定义


#define HID_EPIN_ADDR                 0x81

#define HID_EPIN_SIZE                 0x08

 

#define USB_HID_CONFIG_DESC_SIZ       34

#define USB_HID_DESC_SIZ              9

#define HID_MOUSE_REPORT_DESC_SIZE    63

 

#define HID_DESCRIPTOR_TYPE           0x21

#define HID_REPORT_DESC               0x22

 

#define HID_HS_BINTERVAL               0x07

#define HID_FS_BINTERVAL               0x0A

#define HID_POLLING_INTERVAL           0x0A

 

#define HID_REQ_SET_PROTOCOL          0x0B

#define HID_REQ_GET_PROTOCOL          0x03

 

#define HID_REQ_SET_IDLE              0x0A

#define HID_REQ_GET_IDLE              0x02

 

#define HID_REQ_SET_REPORT            0x09

#define HID_REQ_GET_REPORT            0x01

9.通过函数发送键值,自己写(USBD_HID_SendReport)



10.通过bushound监测上传数据



键盘发送给PC的数据每次8个字节:BYTE1 BYTE2 BYTE3 BYTE4 BYTE5 BYTE6 BYTE7 BYTE8。定义分别是:

BYTE1 --

       |--bit0:   Left Control    是否按下,按下为1 

       |--bit1:   Left Shift        是否按下,按下为1 

       |--bit2:   Left Alt           是否按下,按下为1 

       |--bit3:   Left GUI         是否按下,按下为1 

       |--bit4:   Right Control  是否按下,按下为1  

       |--bit5:   Right Shift      是否按下,按下为1 

       |--bit6:   Right Alt         是否按下,按下为1 

       |--bit7:   Right GUI        是否按下,按下为1 

BYTE2 -- 暂不清楚,有的地方说是保留位

BYTE3--BYTE8 -- 这六个为普通按键


第一列10进制键值,第二列16进制键值,第四列是按键

 

0 00 Reserved (no event indicated)9 N/A √ √ √ 4/101/104

1 01 Keyboard ErrorRollOver9 N/A √ √ √ 4/101/104

2 02 Keyboard POSTFail9 N/A √ √ √ 4/101/104

3 03 Keyboard ErrorUndefined9 N/A √ √ √ 4/101/104

4 04 Keyboard a and A4 31 √ √ √ 4/101/104

5 05 Keyboard b and B 50 √ √ √ 4/101/104

6 06 Keyboard c and C4 48 √ √ √ 4/101/104

7 07 Keyboard d and D 33 √ √ √ 4/101/104

8 08 Keyboard e and E 19 √ √ √ 4/101/104

9 09 Keyboard f and F 34 √ √ √ 4/101/104

10 0A Keyboard g and G 35 √ √ √ 4/101/104

11 0B Keyboard h and H 36 √ √ √ 4/101/104

12 0C Keyboard i and I 24 √ √ √ 4/101/104

13 0D Keyboard j and J 37 √ √ √ 4/101/104

14 0E Keyboard k and K 38 √ √ √ 4/101/104

15 0F Keyboard l and L 39 √ √ √ 4/101/104

16 10 Keyboard m and M4 52 √ √ √ 4/101/104

17 11 Keyboard n and N 51 √ √ √ 4/101/104

18 12 Keyboard o and O4 25 √ √ √ 4/101/104

19 13 Keyboard p and P4 26 √ √ √ 4/101/104

20 14 Keyboard q and Q4 17 √ √ √ 4/101/104

21 15 Keyboard r and R 20 √ √ √ 4/101/104

22 16 Keyboard s and S4 32 √ √ √ 4/101/104

23 17 Keyboard t and T 21 √ √ √ 4/101/104

24 18 Keyboard u and U 23 √ √ √ 4/101/104

25 19 Keyboard v and V 49 √ √ √ 4/101/104

26 1A Keyboard w and W4 18 √ √ √ 4/101/104

27 1B Keyboard x and X4 47 √ √ √ 4/101/104

28 1C Keyboard y and Y4 22 √ √ √ 4/101/104

29 1D Keyboard z and Z4 46 √ √ √ 4/101/104

30 1E Keyboard 1 and !4 2 √ √ √ 4/101/104

31 1F Keyboard 2 and @4 3 √ √ √ 4/101/104

32 20 Keyboard 3 and #4 4 √ √ √ 4/101/104

33 21 Keyboard 4 and $4 5 √ √ √ 4/101/104

34 22 Keyboard 5 and %4 6 √ √ √ 4/101/104

35 23 Keyboard 6 and ^4 7 √ √ √ 4/101/104

36 24 Keyboard 7 and &4 8 √ √ √ 4/101/104

37 25 Keyboard 8 and *4 9 √ √ √ 4/101/104

38 26 Keyboard 9 and (4 10 √ √ √ 4/101/104

39 27 Keyboard 0 and )4 11 √ √ √ 4/101/104

40 28 Keyboard Return (ENTER)5 43 √ √ √ 4/101/104

41 29 Keyboard ESCAPE 110 √ √ √ 4/101/104

42 2A Keyboard DELETE (Backspace)13 15 √ √ √ 4/101/104

43 2B Keyboard Tab 16 √ √ √ 4/101/104

44 2C Keyboard Spacebar 61 √ √ √ 4/101/104

45 2D Keyboard - and (underscore)4 12 √ √ √ 4/101/104

46 2E Keyboard = and +4 13 √ √ √ 4/101/104

47 2F Keyboard [ and {4 27 √ √ √ 4/101/104

48 30 Keyboard ] and }4 28 √ √ √ 4/101/104

49 31 Keyboard and | 29 √ √ √ 4/101/104

50 32 Keyboard Non-US # and ~2 42 √ √ √ 4/101/104

51 33 Keyboard ; and :4 40 √ √ √ 4/101/104

52 34 Keyboard ‘ and “4 41 √ √ √ 4/101/104

53 35 Keyboard Grave Accent and Tilde4 1 √ √ √ 4/101/104

54 36 Keyboard, and <4 53 √ √ √ 4/101/104

55 37 Keyboard . and >4 54 √ √ √ 4/101/104

56 38 Keyboard / and ?4 55 √ √ √ 4/101/104

57 39 Keyboard Caps Lock11 30 √ √ √ 4/101/104

58 3A Keyboard F1 112 √ √ √ 4/101/104

59 3B Keyboard F2 113 √ √ √ 4/101/104

60 3C Keyboard F3 114 √ √ √ 4/101/104

61 3D Keyboard F4 115 √ √ √ 4/101/104

62 3E Keyboard F5 116 √ √ √ 4/101/104

63 3F Keyboard F6 117 √ √ √ 4/101/104

64 40 Keyboard F7


关键字:stm32  HID设备  上位机通讯 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多