NXP

libusb开发

2019-07-12 12:24发布

前言
USB的用途就不多说了,下面的内容主要就是讲解如何利用ST提供的USB驱动库和libusb上位机驱动库实现一个USB数据传输功能,为了降低开发难度,我们仅仅讲解Bulk传输模式,当然这也是用得比较多的传输模式。

开发流程
1,完成STM32单片机端的USB程序;
2,利用linusb自带的inf-wizard工具生成USB驱动;
3,基于libusb编写USB通信程序;
4,测试PC和单片机的数据通信;

STM32程序编写
1,完成描述符的修改,修改后的描述符如下(在usb_desc.c文件中)
设备描述符:
[C] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 const uint8_t CustomHID_DeviceDescriptor[CUSTOMHID_SIZ_DEVICE_DESC] = {     0x12,                       /*bLength */     USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/     0x00,                       /*bcdUSB */     0x02,     0x00,                       /*bDeviceClass*/     0x00,                       /*bDeviceSubClass*/     0x00,                       /*bDeviceProtocol*/     0x40,                       /*bMaxPacketSize40*/     LOBYTE(USBD_VID),           /*idVendor*/     HIBYTE(USBD_VID),           /*idVendor*/     LOBYTE(USBD_PID),           /*idVendor*/     HIBYTE(USBD_PID),           /*idVendor*/     0x00,                       /*bcdDevice rel. 2.00*/     0x02,     1,                          /*Index of string descriptor describing manufacturer */     2,                          /*Index of string descriptor describing product*/     3,                          /*Index of string descriptor describing the device serial number */     0x01                        /*bNumConfigurations*/ }; /* CustomHID_DeviceDescriptor */

配置描述符:
[C] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 const uint8_t 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*/     0xE0,         /* bmAttributes: Bus powered */                   /*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 */     /************** Descriptor of Custom HID interface ****************/     /* 09 */     0x09,         /* bLength: Interface Descriptor size */     USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */     0x00,