历史上的今天
今天是:2024年09月17日(星期二)
2018年09月17日 | 使用stm32F10XX芯片开发的USB HID 双向通信
2018-09-17 来源:eefocus
在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版本,相关资料可以在网络上搜索得到。
史海拾趣
|
从CD/DVD播放器和电脑制冷风扇到工业机械以及包含混合动力汽车、著名的Segway电动滑板车和许多其他电动滑板车、电力驱动的自行车(或称为“电动自行车”)在内的电动交通工具,无刷直流电动机正在各种领域中取代整流式电动机。由于它们越来越普及, ...… 查看全部问答> |
|
$(\'swf_U8t\').innerHTML=AC_FL_RunContent(\'width\', \'550\', \'height\', \'400\', \'allowNetworking\', \'internal\', \'allowScriptAccess\', \'never\', \'src\', encodeURI(\'http://player.youku.com/player.php/sid/30388392/v.swf\'), ...… 查看全部问答> |
|
在写一个模块时,发现一个函数的参数是一个结构体A。结构体A中有很多成员,包括结构体B,C,D,枚举成员E,F,整形G.而结构体B,C,D又包含一些结构体.请问,现在想给结构体A初始化,应该怎么写?有没有类似memset之类的函数,可以不必考虑那么多. ...… 查看全部问答> |
|
大家好! 小弟最近在搞wince5.0的battary驱动,利用DS2786来检测电池状态,现在我不用该芯片来检测,直接就在BatteryPDDInitialize()里用 sps.ACLineStatus = AC_LI ...… 查看全部问答> |
|
以前做好的一个VISTA的NDIS filter,现在编译出来用osrloader总是加载不起来。错误原因就是“连到系统上的设备没有发挥作用”。试了下把WDK6000自带的filter编译出来也是同样的结果,应该不是代码的问题。出现这个的原因是什么呢… 查看全部问答> |
|
【学习Sitara™ AM335x】- Beaglebone环境的搭建方法 Basic Requirements ARM Cross CompilerUbuntu (11.04 (Natty Narwhal) minimum): sudo apt-get install gcc-arm-linux-gnueabiLinaro: https://launchpad.net/linaro-toolchain-binaries复制代码BootloaderDas U-Boot – the Universal Boot Load ...… 查看全部问答> |




