打印
[数据传输]

libusbDotnet能发数据给设备无法接收设备的数据

[复制链接]
1605|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
blandodo|  楼主 | 2017-9-21 11:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
需要做一个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        


相关帖子

沙发
blandodo|  楼主 | 2017-9-21 15:03 | 只看该作者
        while (1) {
                Key_RefreshState(&Key_Home); //刷新按键状态

                if (Key_Home.State == KEY_DOWN && Key_Home.StateChange == CHANGED) {
                        SendQRInfo();
                        printf(("Key down\n\r"));
                } else if (Key_Home.State == KEY_UP && Key_Home.StateChange == CHANGED) {
                        SendCardPullout();
                        printf(("Key up\n\r"));
                }

                HandleUsbBuffer();
                Delay(0xffff);  // 这里延时增加了就可以了
                LED3_TOGGLE;
        }


这里的HandleUsbBuffer就是发了一堆数据,为什么main函数里的循环会影响到USB的收发?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

5

帖子

0

粉丝