STM32自定义USB设备开发详细流程讲解及全套资料源码下载(基于libusb)一、前言USB的用途就不多说了,下面的内容主要就是讲解如何利用ST提供的USB驱动库和libusb上位机驱动库实现一个USB数据传输功能,为了降低开发难度,我们仅仅讲解Bulk传输模式,当然这也是用得比较多的传输模式。二、开发流程1.完成STM32单片机端的USB程序;2.利用linusb自带的inf-wizard工具生成USB驱动;3.基于libusb编写USB通信程序;4.测试PC和单片机的数据通信。三、STM32程序编写1.完成描述符的修改,修改后的描述符如下(在usb_desc.c文件中)。配置描述符就包含了端点描述符,我们用了4个端点,两个BULK-OUT端点,两个BULK-IN端点。A.设备描述符010203040506070809101112131415161718192021constuint8_tCustomHID_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,/*bcdDevicerel.2.00*/0x02,1,/*Indexofstringdescriptordescribingmanufacturer*/2,/*Indexofstringdescriptordescribingproduct*/3,/*Indexofstringdescriptordescribingthedeviceserialnumber*/0x01/*bNumConfigurations*/};/*CustomHID_DeviceDescriptor*/B.配置描述符01020304050607080910constuint8_tCustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC]={0x09,/*bLength:ConfiguationDescriptorsize*/USB_CONFIGURATION_DESCRIPTOR_TYPE,/*bDescriptorType:Configuration*/CUSTOMHID_SIZ_CONFIG_DESC,/*wTotalLength:Bytesreturned*/0x00,0x01,/*bNumInterfaces:1interface*/0x01,/*bConfigurationValue:Configurationvalue*/0x00,/*iConfiguration:Indexofstringdescriptordescribingtheconfiguration*/11121314151617181920212223242526272829303132333435363738394041424344454647484950510xE0,/*bmAttributes:Buspowered*//*Buspowered:7thbit,SelfPowered:6thbit,Remotewakeup:5thbit,reserved:4..0bits*/0xFA,/*MaxPower500mA:thiscurrentisusedfordetectingVbus*//**************DescriptorofCustomHIDinterface****************//*09*/0x09,/*bLength:InterfaceDescriptorsize*/USB_INTERFACE_DESCRIPTOR_TYPE,/*bDescriptorType:Interfacedescriptortype*/0x00,/*bInterfaceNumber:NumberofInterface*/0x00,/*bAlternateSetting:Alternatesetting*/0x04,/*bNumEndpoints*/0xDC,/*bInterfaceClass:Classcode=0DCH*/0xA0,/*bInterfaceSubClass:Subclasscode=0A0H*/0xB0,/*nInterfaceProtocol:Protocolcode=0B0H*/0,/*iInterface:Indexofstringdescriptor*//********************endpointdescriptor********************//*18*/0x07,/*endpointdescriptorlength=07H*/USB_ENDPOINT_DESCRIPTOR_TYPE,/*endpointdescriptortype=05H*/0x81,/*endpoint1IN*/0x02,/*bulktransfer=02H*/0x40,0x00,/*endpointmaxpacketsize=0040H*/0x00,/*thevalueisinvalidwhenbulktransfer*/0x07,/*endpointdescriptorlength=07H*/USB_ENDPOINT_DESCRIPTOR_TYPE,/*endpointdescriptortype=05H*/0x01,/*endpoint1OUT*/0x02,/*bulktransfer=02H*/0x40,0x00,/*endpointmaxpacketsize=0040H*/0x00,/*thevalueisinvalidwhenbulktransfer*/0x07,/*endpointdescriptorlength=07H*/USB_ENDPOINT_DESCRIPTOR_TYPE,/*endpointdescriptortype=05H*/0x82,/*endpoint2IN*/0x02,/*bulktransfer=02H*/0x40,0x00,/*endpointmaxpacketsize=0040H*/0x00,/*thevalueisinvalidwhenbulktransfer*/0x07,/*endpointdescriptorlength=07H*/USB_ENDPOINT_DESCRIPTOR_TYPE,/*endpointdescriptortype=05H*/0x02,/*endpoint2OUT*/0x02,/*bulktransfer=02H*/0x40,0x00,/*endpointmaxpacketsize=0040H*/0x00,/*thevalueisinvalidwhenbulktransfer*/};/*CustomHID_ConfigDescriptor*/C.其他的描述符01020304050607/*USBStringDescriptors(optional)*/constuint8_tCustomHID_StringLangID[CUSTOMHID_SIZ_STRING_LANGID]={CUSTOMHID_SIZ_STRING_LANGID,USB_STRING_DESCRIPTOR_TYPE,0x09,0x040809101112131415161718192021222324252627};/*LangID=0x0409:U.S.English*/constuint8_tCustomHID_StringVendor[CUSTOMHID_SIZ_STRING_VENDOR]={CUSTOMHID_SIZ_STRING_VENDOR,/*SizeofVendorstring*/USB_STRING_DESCRIPTOR_TYPE,/*bDescriptorType*///Manufacturer:STMicroelectronics'M',0,'y',0,'U',0,'S',0,'B',0,'_',0,'H',0,'I',0,'D',0};constuint8_tCustomHID_StringProduct[CUSTOMHID_SIZ_STRING_PRODUCT]={CUSTOMHID_SIZ_STRING_PRODUCT,/*bLength*/USB_STRING_DESCRIPTOR_TYPE,/*bDescriptorType*/'B',0,'y',0,'',0,'e',0,'m',0,'b',0,'e',0,'d',0,'-',0,'n',0,'e',0,'t',0};uint8_tCustomHID_StringSerial[CUSTOMHID_SIZ_STRING_SERIAL]={CUSTOMHID_SIZ_STRING_SERIAL,/*bLength*/USB_STRING_DESCRIPTOR_TYPE,/*bDescriptorType*/'x',0,'x',0,'x',0,'x',0,'x',0,'x',0,'x',0};2.根据端点缓冲区大小配置端点缓冲区地址,配置信息如下(在usb_conf.h文件中):0102030405060708091011121314151617/*buffertablebaseaddress*/#defineBTABLE_ADDRESS(0x00)/*EP0*//*rx/txbufferbaseaddress*/#defineENDP0_RXADDR(0x18)#defineENDP0_TXADDR(0x58)/*EP1*//*txbufferbaseaddress*///地址为32位对其,位4的倍数,不能超过bMaxPacketSize//EP1#defineENDP1_RXADDR(0x98)#defineENDP1_TXADDR(0x98+64)////EP2#defineENDP2_RXADDR(0xA0+64+64)#defineENDP2_TXADDR(0xA0+64+64+64)3.初始化每个端点(在usb_prop.c文件中的CustomHID_Reset函数中)01020304050607080910/*InitializeEndpoint0*/SetEPType(ENDP0,EP_CONTROL);SetEPTxStatus(ENDP0,EP_TX_STALL);SetEPRxAddr(ENDP0,ENDP0_RXADDR);SetEPTxAddr(ENDP0,ENDP0_TXADDR);Clear_Status_Out(ENDP0);SetEPRxCount(ENDP0,Device_Property.MaxPacketSize);SetEPRxValid(ENDP0);/*InitializeEndpoint1*/SetEPType(ENDP1,EP_BULK);111213141516171819202122SetEPRxAddr(ENDP1,ENDP1_RXADDR);SetEPTxAddr(ENDP1,ENDP1_TXADDR);SetEPRxCount(ENDP1,EP_SIZE);SetEPRxStatus(ENDP1,EP_RX_VALID);SetEPTxStatus(ENDP1,EP_TX_NAK);/*InitializeEndpoint2*/