STM32实现一个HID键盘

欢迎加群交流,文章内不共享资源了,都是文件,加群找群文件

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)

猜你喜欢

转载自blog.csdn.net/Jun626/article/details/87803569