STM32 基础系列教程 24 - USB_HID_key
前言
學習stm32 USB接口使用,學會用CUBE工具快速創建USB設備工程及調試,關于usb的相關知道請讀者提前準備并學習,當然如果不想深究其中原理的話,跟著本文來操作就可以實現基于USB的設備開發了。需要提示的是,stm32在使用usb接口功能是一般需要在DP引腳上上拉一個1.5K電阻到3.3V(部分MCU內部會上拉)。
?
示例詳解
基于硬件平臺: STM32F10C8T6最小系統板, MCU 的型號是?STM32F103c8t6, 使用stm32cubemx 工具自動產生的配置工程,使用KEIL5編譯代碼。
?
?
本示例所用的最小系統板原理圖:
?
?
?
?
在usbd_hid.c中做如下更改:
/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ]? __ALIGN_END =
{
?
#if 0
? 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 */
??????
#else
?????? /************** Descriptor of Configuation ****************/
? 0x09, /* bLength: Configuation 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 */ //160911
? /*Bus powered: 7th bit, Self Powered: 6th bit, Remote wakeup: 5th bit, reserved: 4..0 bits */
? 0xFA,???????? /* MaxPower 500 mA: this current is used for detecting Vbus */????????? //160911 以2mA為單位
//??????????? 0x32,?????? ??/* MaxPower 100 mA: this current is used for detecting Vbus */
//??? 0x96,???????? /* MaxPower 300 mA: this current is used for detecting Vbus */
?
? /************** Descriptor of Custom HID interface ****************/
? /* 09 */
? 0x09,???????? /* bLength: Interface Descriptor size */
? USB_DESC_TYPE_INTERFACE,/* bDescriptorType: Interface descriptor type = 4*/
? 0x00,???????? /* bInterfaceNumber: Number of Interface */
? 0x00,???????? /* bAlternateSetting: Alternate setting */
? 0x02,???????? /* 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 Custom HID ********************/
? /* 18 */
? 0x09,???????? /* bLength: HID Descriptor size */
? HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
? 0x10,0x01,??????????? /* bcdHID: HID Class Spec release number */ //160911 設備版本號BCD碼
? 0x00,???????? /* bCountryCode: Hardware target country */
? 0x01,???????? /* bNumDescriptors: Number of HID class descriptors to follow */
? 0x22,???????? /* bDescriptorType */
? CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
? 0x00,
?
? /******************** Descriptor of Custom HID endpoints ******************/
? /* 27 */
? 0x07,?????????????????? /* bLength: Endpoint Descriptor size */
? USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: = 5*/
? 0x82,??????????? /* bEndpointAddress: (IN) = 2*/
? // bit 3...0 : the endpoint number
? // bit 6...4 : reserved
? // bit 7???? : 0(OUT), 1(IN)
? 0x03,?????????????????? /* bmAttributes: Interrupt endpoint */ //160911 0-1bit定義了傳輸類型,00=控制,01=同步,10=批量,11=中斷事務
? //160911 00=Control, 01=Isochronous, 10=Bulk, 11=Interrupt Transaction
? 0x08,?????????????????? /* wMaxPacketSize: 64 40 Bytes max? */
? 0x00,?????????????????? //High 8 bit of wMaxPacketSize
? 0x01,?????????????????? /* bInterval: Polling Interval (1 ms) */? //160911 Used in 01,03 Transaction
? /* 34 */
? 0x07,?????????????????? /* bLength: Endpoint Descriptor size */
? USB_DESC_TYPE_ENDPOINT,?? /* bDescriptorType: = 5*/
? 0x01,??????????? /* bEndpointAddress: (OUT) = 1*/
? 0x03,?????????????????? /* bmAttributes: Interrupt endpoint */ //160911 0-1bit定義了傳輸類型,00=控制,01=同步,10=批量,11=中斷事務
? //160911 00=Control, 01=Isochronous, 10=Bulk, 11=Interrupt Transaction
??0x08,?????????????????? /* wMaxPacketSize: 64 40 Bytes max? */
? 0x00,?????????????????? //High 8 bit of wMaxPacketSize
? 0x01,?????????????????? /* bInterval: Polling Interval (1 ms) */? //160911 Used in 01,03 Transaction
? /* 41 */
?
#endif
?
} ;
加入
__ALIGN_BEGIN static uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
{
? 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
? 0x09, 0x06, // USAGE (Keyboard)
? 0xa1, 0x01, // COLLECTION (Application)
?
? // 輸入,第一個字節表示特殊功能鍵 LeftControl -- Right GUI
? 0x05, 0x07, //???? USAGE_PAGE (Keyboard/Keypad)
? 0x19, 0xe0, //???? USAGE_MINIMUM (Keyboard LeftControl)
? 0x29, 0xe7, //???? USAGE_MAXIMUM (Keyboard Right GUI)
? 0x15, 0x00, //???? LOGICAL_MINIMUM (0)
? 0x25, 0x01, //???? LOGICAL_MAXIMUM (1)
? 0x95, 0x08, //???? REPORT_COUNT (8)
? 0x75, 0x01, //???? REPORT_SIZE (1)
? 0x81, 0x02, //???? INPUT (Data,Var,Abs)
?
? // 輸入,第二個字節是常值0,給OEM使用
? 0x95, 0x01, //???? REPORT_COUNT (1)
? 0x75, 0x08, //???? REPORT_SIZE (8)
? 0x81, 0x03, //???? INPUT (Cnst,Var,Abs)
?
? // 輸入,第三到8個字節,用來表示最多同時6個按鍵按下
? 0x95, 0x06, //?? REPORT_COUNT (6)
? 0x75, 0x08, //?? REPORT_SIZE (8)
? 0x25, 0xFF, //?? LOGICAL_MAXIMUM (255)
? 0x19, 0x00, //?? USAGE_MINIMUM (Reserved (no event indicated))
? 0x29, 0x65, //?? USAGE_MAXIMUM (Keyboard Application)
? 0x81, 0x00, //???? INPUT (Data,Ary,Abs)
?
? // 輸出,第一個字節前兩位 分別表示兩個LED燈狀態
? 0x25, 0x01, //???? LOGICAL_MAXIMUM (1)
? 0x95, 0x02, //?? REPORT_COUNT (2)
? 0x75, 0x01, //?? REPORT_SIZE (1)
? 0x05, 0x08, //?? USAGE_PAGE (LEDs)
? 0x19, 0x01, //?? USAGE_MINIMUM (Num Lock)
? 0x29, 0x02, //?? USAGE_MAXIMUM (Caps Lock)
? 0x91, 0x02, //?? OUTPUT (Data,Var,Abs)
?
? // 輸出,第一個學節后六位用常值0來填充
? 0x95, 0x01, //?? REPORT_COUNT (1)
? 0x75, 0x06, //?? REPORT_SIZE (6)
? 0x91, 0x03, //?? OUTPUT (Cnst,Var,Abs)
?
? 0xc0??????? // END_COLLECTION
// The Feature report
}; /* CustomHID_ReportDescriptor */
?
main.c 中加入如下圖所示代碼,模擬鍵盤發送一些數據:
?
?
燒錄默認代碼,連上電腦可在電腦上看到對應多一個USB keyBoard設備:
?
?
?
OK,本期實驗完成!下期見!同時如果大家有什么疑問或是有想了解的其它內容,也歡迎大家留言!!最后喜歡這個公眾號的同學們記得加關注了,每天都會有技術干貨推出!!
?
?
文中資料下載,在公眾號里給十三發消息:
下載|STM32基礎系列教程24
?
關注十三公眾號
?
總結
以上是生活随笔為你收集整理的STM32 基础系列教程 24 - USB_HID_key的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STM32 基础系列教程 23 - US
- 下一篇: STM32 基础系列教程 25 - US