你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 USB HID开发实例,实现USB双向通信!

[复制链接]
中国-大鱼儿 发布时间:2011-4-16 14:45
STM32 USB HID开发实例,实现USB双向通信。

在STM32 ARM平台上实现USB与PC端得通信(IC为STM32F10XX系列)。本文提供一个例程(已测试通过),不用了解任何USB协议(当然了解USB相关协议或描述表的意义是很必要的),在此例程上,稍作修改,即可开展你的项目或学习或进行产品开发。

在ST中我们可以获得了USB相关的一个HID例程,但是官方例子中只是用到2个端点。数据只收不发。

本例程中,用到了3个USB端点,实现PC上位机与下位机见双向通信。EP0为控制端点(必须的,这是因为系统默认端点0作为控制传输端点),EP1为INTERRUPT OUT端点(数据输出端,即PC向MCU发送数据段),EP2为INTERRUPT OUT端点(数据输入端,即MCU向PC发送数据)。

实现过程,我们需要修改一下HID的描述表,修改如下(有详细注释)

/* USB Configuration Descriptor */

/*   All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */

const u8 CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] =

  {

    0x09, /* bLength: Configuation Descriptor size */

    USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */

    CUSTOMHID_SIZ_CONFIG_DESC,

    /* wTotalLength: Bytes returned */

    0x00,

    0x01,         /* bNumInterfaces: 1 interface */

    0x01,         /* bConfigurationValue: Configuration value */

    0x00,         /* iConfiguration: Index of string descriptor describing

                                 the configuration*/

    0xC0,         /* bmAttributes: Bus powered */

                  /*Bus powered: 7th bit, Self Powered: 6th bit, Remote wakeup: 5th bit, reserved: 4..0 bits */

    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_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */

    0x00,         /* bInterfaceNumber: Number of Interface */

    0x00,         /* bAlternateSetting: Alternate setting */

    0x02,         /* bNumEndpoints */

    0x03,         /* bInterfaceClass: HID */

    0x00,         /* bInterfaceSubClass : 1=BOOT, 0=no boot */

    0x00,         /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */

    0,            /* iInterface: Index of string descriptor */

    /******************** Descriptor of Custom HID HID ********************/

    /* 18 */

    0x09,         /* bLength: HID Descriptor size */

    HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */

    0x10,         /* bcdHID: HID Class Spec release number */

    0x01,

    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_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */

  

    0x82,          /* bEndpointAddress: Endpoint Address (IN) */               

                   // bit 3...0 : the endpoint number

                   // bit 6...4 : reserved

                    // bit 7     : 0(OUT), 1(IN)

    0x03,          /* bmAttributes: Interrupt endpoint */

    0x40,//0x02,          /* wMaxPacketSize: 20 Bytes max */

    0x00,

    0x20,          /* bInterval: Polling Interval (32 ms) */

    /* 34 */

            

    0x07,      /* bLength: Endpoint Descriptor size */

    USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: */

                     /*    Endpoint descriptor type */

    0x01,      /* bEndpointAddress: */

                     /*    Endpoint Address (OUT) */

    0x03,      /* bmAttributes: Interrupt endpoint */

    0x40,//0x02,   /* wMaxPacketSize: 20 Bytes max  */

    0x00,

    0x10,      /* bInterval: Polling Interval (16 ms) */

    /* 41 */

  }; /* CustomHID_ConfigDescriptor */

关于如何理解HID 描述表,请参考USB HID协议1.1版本,相关资料可以在网络上搜索得到。

在此,现提供KEIL MDK 和 IAR EWARM 5.4版本的例子,欢迎下载。萝卜青菜,喜欢用MDK,就MDK,喜欢EWARM,就EWARM。同样这里有见有意思的事,在全编译的情况下,EWARM要比MDK编译速度要快一些。

相关测试结果如下:

通过呀呀USB_hid 或Bus Hound相关工具,先发一包数据给下位机,这样下位机每隔1s会发送一包数据。结果如图:

呀USB_hid可以在本博客上下载^_^: http://blog.ednchina.com/itspy


测试结果
收藏 3 评论11 发布时间:2011-4-16 14:45

举报

11个回答
轻舟 回答时间:2013-12-7 13:52:13

RE:STM32 USB HID开发实例,实现USB双向通信!

正用得着,下来看看
风云1999~ 回答时间:2014-4-2 16:46:12

回复:STM32 USB HID开发实例,实现USB双向通信!

楼主辛苦,正需要呢。。。。。。。。。
zydwh 回答时间:2014-4-9 19:52:31

RE:STM32 USB HID开发实例,实现USB双向通信!

学习学习学习学习学习
GaoFong 回答时间:2014-10-4 19:41:56

回复:STM32 USB HID开发实例,实现USB双向通信!

确实挺好的,觉得USB是个很好的接口,但STM32的教程中很少提及。
xiaoluo 回答时间:2015-4-30 17:27:38
真在学习USB,LZ,在哪下载?
qiu-368230 回答时间:2015-9-14 13:13:03
哪里下载呢
xujiantj 回答时间:2019-1-23 13:48:39
感谢楼主分享,下载看看
yang000333245 回答时间:2019-10-9 14:45:00
楼主,已经失效了
zhaoyunme0 回答时间:2020-1-4 14:51:33
没地方下载啊
jdzczz 回答时间:2020-3-2 11:15:06
需要这个,应该会有用!
斯坦隆平 回答时间:2020-3-23 16:54:02
研究看看

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版