|
STM32 USB HID开发实例,实现USB双向通信。 $ j' b5 n% h: |, s! w, T 在STM32 ARM平台上实现USB与PC端得通信(IC为STM32F10XX系列)。本文提供一个例程(已测试通过),不用了解任何USB协议(当然了解USB相关协议或描述表的意义是很必要的),在此例程上,稍作修改,即可开展你的项目或学习或进行产品开发。 在ST中我们可以获得了USB相关的一个HID例程,但是官方例子中只是用到2个端点。数据只收不发。 , L+ Y1 ^! j2 y& @( E# W1 a % W. n$ v% M5 p2 s/ [ 本例程中,用到了3个USB端点,实现PC上位机与下位机见双向通信。EP0为控制端点(必须的,这是因为系统默认端点0作为控制传输端点),EP1为INTERRUPT OUT端点(数据输出端,即PC向MCU发送数据段),EP2为INTERRUPT OUT端点(数据输入端,即MCU向PC发送数据)。 $ P) }( R: t/ b6 U/ q: L: W 实现过程,我们需要修改一下HID的描述表,修改如下(有详细注释) /* USB Configuration Descriptor */ /* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ # }7 t+ _5 z' i. N' w/ o const u8 CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] = 7 F" f$ L+ @1 x { 0x09, /* bLength: Configuation Descriptor size */ 8 f: l: ^' W4 Y H 7 R6 f0 r6 p( E; M' k$ s* p' L$ i2 {7 ` USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */ : K7 r) T& S. `; b2 Q2 Y/ Z- G CUSTOMHID_SIZ_CONFIG_DESC, : J3 M0 h/ P) H; Q3 J& a ( v, p9 h$ f. J( @0 y) Z# I /* wTotalLength: Bytes returned */ 5 L4 ], Z! w- }" Z 0x00, $ g, b0 p4 {3 w8 x& ] 0x01, /* bNumInterfaces: 1 interface */ 1 R7 }8 G0 ]% l5 u4 D& ^9 b! X 0x01, /* bConfigurationValue: Configuration value */ + L) E- Y% Q7 A" [! l! A: j' C; p) \ 0x00, /* iConfiguration: Index of string descriptor describing ' `; U9 r4 G2 G1 c, Y; B9 L the configuration*/ ( l" Z. a# W2 | l7 @* m, p4 O+ W' F8 ` 0xC0, /* bmAttributes: Bus powered */ /*Bus powered: 7th bit, Self Powered: 6th bit, Remote wakeup: 5th bit, reserved: 4..0 bits */ # E% e9 A* B2 W4 f5 X+ x B+ Z* v" e2 s3 b) @6 X7 X 0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */ // 0x96, /* MaxPower 300 mA: this current is used for detecting Vbus */ 5 I2 a- r3 t. H. x# o; W+ T# M$ H /************** Descriptor of Custom HID interface ****************/ % u. \* H! E* ~! q /* 09 */ 6 q& ^" H8 V4 |3 D1 [8 { 0x09, /* bLength: Interface Descriptor size */ USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */ ) w6 l6 R- ~4 O/ b 0x00, /* bInterfaceNumber: Number of Interface */ 7 U/ i* \! Y* p& L2 r 0x00, /* bAlternateSetting: Alternate setting */ 2 X0 @" s! f* G( Y: Z- A* A- { 9 z; L) t8 p8 x: c' ^ 0x02, /* bNumEndpoints */ % B* R; A: c9 T' y: A3 K+ D 0x03, /* bInterfaceClass: HID */ ' b: V; ^8 v7 e; p4 a : V8 b! P7 X' V: a' M- u 0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ * Z! T6 j# L9 @8 A 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ $ B, m( _3 a7 { z) G* D! O1 z' w3 k 0, /* iInterface: Index of string descriptor */ " _+ G' t6 `, `2 @+ X6 v- X5 m5 | /******************** Descriptor of Custom HID HID ********************/ 9 R. I/ K& @9 T2 |2 T /* 18 */ 0x09, /* bLength: HID Descriptor size */ HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ . _& K6 \. B- H0 v8 j% w V. K 0x10, /* bcdHID: HID Class Spec release number */ 7 w: d+ H- D3 K+ {. B 0x01, 0x00, /* bCountryCode: Hardware target country */ ! }' b& S7 `/ ?4 [0 ?! ` 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ 3 F+ k0 q( ~; l: z8 s$ I 0x22, /* bDescriptorType */ - N8 e7 m: h$ L! K o 1 n% a, Y* P) s* `2 v CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */ : U- C' X8 L& m7 Z0 \ 0x00, $ h" ~% h( r; Q8 m& H /******************** Descriptor of Custom HID endpoints ******************/ ! E/ ?8 T; Q: _" r. a " D8 {) U! ]1 m+ ? /* 27 */ 0x07, /* bLength: Endpoint Descriptor size */ 6 m& x6 g8 g8 A7 ?& I( [4 Y 5 g$ H- ]) v+ f/ K# M& S( u! c USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */ ( d( |2 F" }: A7 E / c& X/ \8 G$ S6 c9 L4 l ! l8 U& v) b' ], i; t+ q 0x82, /* bEndpointAddress: Endpoint Address (IN) */ 0 N2 w/ m, X0 b0 G$ S! Q + V, B( Q3 A) S4 \& j0 n; } // bit 3...0 : the endpoint number 8 N2 ?% w* q% J6 J" f/ Q6 {/ I // bit 6...4 : reserved 0 n, }! r7 t; u' a* @. x // bit 7 : 0(OUT), 1(IN) / V6 G1 E7 Q \) f$ Y8 `' q& @ 8 ?9 b' l& D' z$ x, U 0x03, /* bmAttributes: Interrupt endpoint */ 9 q& Y: m7 [' J, ?+ D 0x40,//0x02, /* wMaxPacketSize: 20 Bytes max */ 7 G: q# y X' q( E8 | - B' o8 m3 \5 Z# T/ g 0x00, 0x20, /* bInterval: Polling Interval (32 ms) */ % V& }# E) K* G& v4 ?7 N7 B /* 34 */ ! P, H1 n9 |( y+ D$ [7 Z1 y. ` - m( _4 i1 L; |3 ] 9 C# ]2 a" A0 } 0x07, /* bLength: Endpoint Descriptor size */ 2 r! f5 }- _( L+ P , o0 J4 q! K$ b! a" l: K# r USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */ 0 l( c4 Y3 m& b5 j. t! d /* Endpoint descriptor type */ # Y$ _: |0 N8 A3 d; h, C& Z5 f 0x01, /* bEndpointAddress: */ $ L, b. S% ~4 [ /* Endpoint Address (OUT) */ 3 E. s+ \$ [1 G8 e 0x03, /* bmAttributes: Interrupt endpoint */ + _* S# q7 z p 0x40,//0x02, /* wMaxPacketSize: 20 Bytes max */ : | X! d$ j5 ^/ H3 w3 C' R 0x00, 0 l& _3 v; c6 \2 ] 0x10, /* bInterval: Polling Interval (16 ms) */ * S- M0 ?; P! K2 W, v /* 41 */ }; /* CustomHID_ConfigDescriptor */ 6 _8 [0 V2 V% S4 X6 g 关于如何理解HID 描述表,请参考USB HID协议1.1版本,相关资料可以在网络上搜索得到。 ; b5 V6 \* |- N2 C; ^: w 在此,现提供KEIL MDK 和 IAR EWARM 5.4版本的例子,欢迎下载。萝卜青菜,喜欢用MDK,就MDK,喜欢EWARM,就EWARM。同样这里有见有意思的事,在全编译的情况下,EWARM要比MDK编译速度要快一些。 相关测试结果如下: : M# Q5 W) G+ P2 R- M1 p6 S / U' i! B- p" Z5 v4 A! l3 ] 通过呀呀USB_hid 或Bus Hound相关工具,先发一包数据给下位机,这样下位机每隔1s会发送一包数据。结果如图: ) ~' J* L4 p* L& f + a5 s+ X/ h8 P4 r7 Z2 _. B 呀USB_hid可以在本博客上下载^_^: http://blog.ednchina.com/itspy 8 {( g# Q7 ^+ p! T: p 2 t* N- u9 K2 I" ]测试结果 |
最全USB HID开发资料,悉心整理一个月,亲自测试
【源码】STLINK-V3MINI 高速USB仿真器,成功改刷【高速CMSIS-DAP】
STM32 USB CDC 虚拟多串口
实战经验 | 选择USBX模块生成USB CDC ACM无PD的项目
STM32 USB HID键盘例程
刘氓兔的杂谈【001】-片上USB 高速PHY
【经验分享】在进行 USB CDC 类开发时,无法发送 64整数倍的数据
在线直播|无需编写任何代码即可在STM32上实现USB-C Power Delivery
圈圈发布USB图书第二版有感,以及分享一些我学习USB过程...
USB Audio设计与实现
微信公众号
手机版
RE:STM32 USB HID开发实例,实现USB双向通信!
回复:STM32 USB HID开发实例,实现USB双向通信!
RE:STM32 USB HID开发实例,实现USB双向通信!
回复:STM32 USB HID开发实例,实现USB双向通信!