495005638

495005638

0个粉丝

22

问答

0

专栏

38

资料

495005638  发布于  2013-08-24 22:48:10
采纳率 0%
22个问答
4603

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

 
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编译速度要快一些。
我来回答
回答0个
时间排序
认可量排序
易百纳技术社区暂无数据
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区