打印
[STM32F4]

USB开发是不是只要实现读取端点数据和写端点数据就可以?

[复制链接]
2478|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
kohillyang|  楼主 | 2015-9-14 11:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
学了一段时间USB,感觉迷迷糊糊的,在想我不需要实现复杂的功能,只要能保证FPG或者STM32A能正确的,快速地接收到数据就行了,是不是只需要一个通用的USB驱动(比如winusb),上层封装一下写端点的函数,在单片机(或者USB芯片)做一个读取端点数据的函数,就可以发送数据了,然后这时候中断端点和批量端点就没有太大区别了?
沙发
mmuuss586| | 2015-9-14 14:55 | 只看该作者
还需要握手的;

使用特权

评论回复
板凳
bbapple| | 2015-9-14 18:02 | 只看该作者
usb学起来挺麻烦的,涉及到很多协议,也不是很懂。。。

使用特权

评论回复
地板
kohillyang|  楼主 | 2015-9-14 22:13 | 只看该作者

那么,握手是什么时候进行的呢,网上查了下,握手是速度切换的时候进行的,这个速度切换是发生在什么时候呢?
谢谢您的解答

使用特权

评论回复
5
airwill| | 2015-9-14 22:42 | 只看该作者
实现读取端点数据和写端点数据只是最底层的. 接下来数据还要处理吧

使用特权

评论回复
6
kohillyang|  楼主 | 2015-9-15 17:21 | 只看该作者
airwill 发表于 2015-9-14 22:42
实现读取端点数据和写端点数据只是最底层的. 接下来数据还要处理吧

自己用libusb试了一下,发现真的可以,固件程序读取批量端点2的数据,将数据写入批量端点6
上位机代码如下
#include "../src/lusb0_usb.h"
#include <stdio.h>

// Enables this example to work with a device running the
// libusb-win32 PIC Benchmark Firmware.
//#define BENCHMARK_DEVICE

//////////////////////////////////////////////////////////////////////////////
// TEST SETUP (User configurable)

// Issues a Set configuration request
#define TEST_SET_CONFIGURATION

// Issues a claim interface request
#define TEST_CLAIM_INTERFACE
#define TEST_BULK_WRITE
// Attempts one bulk read.
#define TEST_BULK_READ

// Attempts one bulk write.
// #define TEST_BULK_WRITE

//////////////////////////////////////////////////////////////////////////////
// DEVICE SETUP (User configurable)

// Device vendor and product id.
#define MY_VID 0x04B4
#define MY_PID 0x1104

// Device configuration and interface id.
#define MY_CONFIG 1
#define MY_INTF 0

// Device endpoint(s)
#define EP_IN 0x86
#define EP_OUT 0x02

// Device of bytes to transfer.
#define BUF_SIZE 2048


usb_dev_handle *open_dev(void)
{
        struct usb_bus *bus;
        struct usb_device *dev;

        for (bus = usb_get_busses(); bus; bus = bus->next)
        {
                for (dev = bus->devices; dev; dev = dev->next)
                {
                        if (dev->descriptor.idVendor == MY_VID
                                && dev->descriptor.idProduct == MY_PID)
                        {
                                return usb_open(dev);
                        }
                }
        }
        return NULL;
}

int main(void)
{
        usb_dev_handle *dev = NULL; /* the device handle */
        unsigned char tmpwrite[BUF_SIZE] = "hello world\n";
        unsigned char tmpread[BUF_SIZE] = {0};
        for (int i = 0; i < BUF_SIZE;i++)
        {
                tmpread[i] = 0;
        }
        int ret;
        void* async_read_context = NULL;
        void* async_write_context = NULL;

        usb_init(); /* initialize the library */
        usb_find_busses(); /* find all busses */
        usb_find_devices(); /* find all connected devices */


        if (!(dev = open_dev()))
        {
                printf("error opening device: \n%s\n", usb_strerror());
                return 0;
        }
        else
        {
                printf("success: device %04X:%04X opened\n", MY_VID, MY_PID);
        }

#ifdef TEST_SET_CONFIGURATION
        if (usb_set_configuration(dev, MY_CONFIG) < 0)
        {
                printf("error setting config #%d: %s\n", MY_CONFIG, usb_strerror());
                usb_close(dev);
                return 0;
        }
        else
        {
                printf("success: set configuration #%d\n", MY_CONFIG);
        }
#endif

#ifdef TEST_CLAIM_INTERFACE
        if (usb_claim_interface(dev, 0) < 0)
        {
                printf("error claiming interface #%d:\n%s\n", MY_INTF, usb_strerror());
                usb_close(dev);
                return 0;
        }
        else
        {
                printf("success: claim_interface #%d\n", MY_INTF);
        }
#endif

#ifdef TEST_BULK_WRITE


#ifdef TEST_ASYNC
        // Running an async write test
        ret = transfer_bulk_async(dev, EP_OUT, tmp, sizeof(tmp), 5000);
#else
        // Running a sync write test
        ret = usb_bulk_write(dev, EP_OUT, (char *)tmpwrite, 2045, 5000);
#endif
        if (ret < 0)
        {
                printf("error writing:\n%s\n", usb_strerror());
        }
        else
        {
                printf("success: bulk write %d bytes\n", ret);
        }
#endif

#ifdef TEST_BULK_READ


        // Running a sync read test
        ret = usb_bulk_read(dev, EP_IN, (char *)tmpread, sizeof(tmpwrite), 4);
        if (ret < 0)
        {
                printf("error reading:\n%s\n", usb_strerror());
        }
        else
        {
                printf("success: bulk read %d bytes\n", ret);
        }
#endif

#ifdef TEST_CLAIM_INTERFACE
        usb_release_interface(dev, 0);
#endif

        if (dev)
        {
                usb_close(dev);
        }
        printf("Done.\n");
        printf((char *)tmpread);
        system("pause");

        return 0;
}

然后可以顺利从端点六读取数据

使用特权

评论回复
7
kohillyang|  楼主 | 2015-9-16 12:46 | 只看该作者
问题是速度好慢,只有400KB/s

使用特权

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

本版积分规则

3

主题

11

帖子

0

粉丝