STM32 USB Host 鼠标和键盘驱动 -- 原创

STM32 USB 主机的鼠标和键盘驱动

本文基于 样例, 继续详细说明鼠标和键盘的驱动.
首先介绍鼠标, 当前鼠标的格式有很多有先发送按键的有先发送移动的, 我的鼠标是先发送按键的, 且是鼠标键盘公用一个USB接口, 然后键盘的特殊字符(例如调节音量, 播放控制等)通过鼠标发送, 当为鼠标报文时, 第一个字符为1, 当发送键盘特殊报文时, 第一个字符为0, 具体格式是:

  1. 鼠标报文
地址 说明
[0] 报文类型, 1为鼠标报文, 0为键盘特殊字符报文
[1] 按键状态, 包括0位左键, 1位右键,2位中键
[2] X轴移动值
[3] Y轴移动值
[4] 滚轮旋转值
  1. 键盘特殊报文
地址 说明
[0] 报文类型, 1为鼠标报文, 0为键盘特殊字符报文
[1] 键盘特殊字符
[2]
[3]
[4]

这种格式与usbh_hid_mouse.c中给的格式完全不同, 所以要对整个文件都要修改.

  1. usbh_hid_mouse.h

因为当前鼠标多了一个滚轮和中键, 所以要修改结构体HID_MOUSE_Info_TypeDef:

typedef struct _HID_MOUSE_Info
{
   
/* [ */
  uint8_t   type;
  uint8_t   buttons[3];
/* ] */
  int8_t    x;
  int8_t    y;
/* [ */
  int8_t    z;
  uint8_t   key;
/* ] */
}
HID_MOUSE_Info_TypeDef;
  1. usbh_hid_mouse.c

因为我的鼠标报文最大5个字节, 而默认是4个字节, 所以我把接收缓冲区改成了数组


/** @defgroup USBH_HID_MOUSE_Private_Variables
  * @{
  */
HID_MOUSE_Info_TypeDef    mouse_info;
uint32_t                  mouse_report_data[2];
uint32_t                  mouse_rx_report_buf[2]; // 这个buf在最新的USBH库中已经添加, 但不是数组, 之前是需要自己创建的, 防止接收与提取的buf冲突

然后是添加和修改按键和移动数据的顺序:

/* Structures defining how to access items in a HID mouse report */
/* Access message status. */
static const HID_Report_ItemTypedef prop_type = {
   
  (uint8_t *)(void *)mouse_report_data + 0, /*data*/
  8,     /*size*/
  0,     /*shift*/
  0,     /*count (only for array items)*/
  1,     /*signed?*/
  0,     /*min value read can return*/
  0xFF,  /*max value read can return*/
  0,     /*min vale device can report*/
  0xFF,  /*max value device can report*/
  1      /*resolution*/
};

/* Access the special key, when the message status is 3. */
static const HID_Report_ItemTypedef prop_key = {
   
  (uint8_t *)(void *)mouse_report_data + 1, /*data*/
  8,     /*size*/
  0,     /*shift*/
  0,     /*count (only for array items)*/
  1,     /*signed?*/
  0,     /*min value read can return*/
  0xFF,  /*max value read can return*/
  0,     /*min vale device can report*/
  0xFF,  /*max value device can report*/
  1      /*resolution*/
};

/* Access button 1 state. */
static const HID_Report_ItemTypedef prop_b1 = {
   
  (uint8_t *)(void *)mouse_report_data + 1, /*data*/
  1,     /*size*/
  0,     /*shift*/
  0,     /*count (only for array items)*/
  0,     /*signed?*/
  0,     /*min value read can return*/
  1,     /*max value read can return*/
  0,     /*min value device can report*/
  1,     /*max value device can report*/
  1      /*resolution*/
};

/* Access button 2 state. */
static const HID_Report_ItemTypedef prop_b2 = {
   
  (uint8_t *)(void *)mouse_report_data + 1, /*data*/
  1,     /*size*/
  1,     /*shift*/
  0,     /*count (only for array items)*/
  0,     /*signed?*/
  0,     /*min value read can return*/
  1,     /*max value read can return*/
  0,     /*min value device can report*/
  1,     /*max value device can report*/
  1      /*resolution*/
};

/* Access button 3 state. */
static const HID_Report_ItemTypedef prop_b3 = {
   
  (uint8_t *)(void *)mouse_report_data + 1, /*data*/
  1,     /*size*/
  2,     

你可能感兴趣的:(嵌入式,无线键盘鼠标,stm32,host,usb)