需要做一个USB数据传输,STM32F429作为USB设备,电脑为HOST。
PC端用的C#库LibUsbDotNet,STM32端用ST家自带的USB库。
已经实现了PC端向STM32发数据,但是STM32发回来的数据PC端收不到。
查了很多资料说,USB通信的架构就是HOST主动请求消息设备数据才能被获取,那么LibUsb是怎么实现实时读取数的呢?
定位信息1,设备描述符:
__ALIGN_BEGIN static uint8_t USBD_BULK_CfgDesc[USB_BULK_CONFIG_DESC_SIZ] __ALIGN_END
= { 0x09, /* bLength: Configuration Descriptor size */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
USB_BULK_CONFIG_DESC_SIZ,
/* 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 and Support Remote Wake-up */
0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
/************** Descriptor of user defined 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*/
0xFF, /*bInterfaceClass: none*/
0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0, /*iInterface: Index of string descriptor*/
/******************** Descriptor of user defined endpoint ********************/
/* 18 */
0x07, /*bLength: Endpoint Descriptor size*/
USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/
BULK_IN_EP, /*bEndpointAddress: Endpoint Address (IN)*/
0x02, /*bmAttributes: bulk endpoint*/
BULK_IN_PACKET, /*wMaxPacketSize: 4 Byte max */
0x00, 0x0A, /*bInterval: Polling Interval (10 ms)*/
/******************** Descriptor of user defined endpoint ********************/
/* 25 */
0x07, /*bLength: Endpoint Descriptor size*/
USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/
BULK_OUT_EP, /*bEndpointAddress: Endpoint Address (IN)*/
0x02, /*bmAttributes: bulk endpoint*/
BULK_OUT_PACKET, /*wMaxPacketSize: 4 Byte max */
0x00, 0x0A, /*bInterval: Polling Interval (10 ms)*/
/* 32 */
};
发送数据的方式:
void SendCardPullout(void)
{
StructUsbMessage msg = {0};
printf("Send card pull out\r\n");
msg.usbCmd = CMD_MCUCardPullout;
msg.dataEccSum = 0;
msg.validDataLength = 0;
memset(USB_Tx_Buffer, 0, MSC_MEDIA_PACKET);
memcpy(USB_Tx_Buffer, (char*)&msg, sizeof(msg));
DCD_EP_Tx(&USB_OTG_dev, BULK_IN_EP, USB_Tx_Buffer, MSC_MEDIA_PACKET);
UsbDelay(0xfff);
}
定位信息2,PC端发送数据的时候,Bus Hound收到如下数据:
Device Phase Data Description Cmd.Phase.Ofs(rep)
------ ----- ------------------------ ---------------- ------------------
33 CMD 00 00 00 00 00 00 TEST UNIT READY 1.1.0(31)
33 ok 1.2.0
31.0 CTL 80 06 00 01 00 00 12 00 GET DESCRIPTOR 32.1.0
31.0 IN 12 01 00 02 ff 00 00 40 .......[url=home.php?mod=space&uid=72445]@[/url] 32.2.0
83 04 20 f7 00 02 01 02 .. ..... 32.2.8
03 01 .. 32.2.16
31.0 CTL 80 06 00 02 00 00 09 00 GET DESCRIPTOR 33.1.0
31.0 IN 09 02 20 00 01 01 00 e0 .. ..... 33.2.0
32 2 33.2.8
31.0 CTL 80 06 00 02 00 00 20 00 GET DESCRIPTOR 34.1.0
31.0 IN 09 02 20 00 01 01 00 e0 .. ..... 34.2.0
32 09 04 00 00 02 ff 00 2....... 34.2.8
00 00 07 05 81 02 40 00 ......@. 34.2.16
0a 07 05 01 02 40 00 0a .....@.. 34.2.24
31.0 CTL 00 09 01 00 00 00 00 00 SET CONFIG 35.1.0
31.1 OUT 00 00 00 00 00 00 00 00 ........ 36.1.0
00 00 00 00 .... 36.1.8
|