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

[复制链接]
2811|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 | 显示全部楼层

那么,握手是什么时候进行的呢,网上查了下,握手是速度切换的时候进行的,这个速度切换是发生在什么时候呢?
谢谢您的解答
airwill 发表于 2015-9-14 22:42 | 显示全部楼层
实现读取端点数据和写端点数据只是最底层的. 接下来数据还要处理吧
 楼主| kohillyang 发表于 2015-9-15 17:21 | 显示全部楼层
airwill 发表于 2015-9-14 22:42
实现读取端点数据和写端点数据只是最底层的. 接下来数据还要处理吧

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

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

  6. //////////////////////////////////////////////////////////////////////////////
  7. // TEST SETUP (User configurable)

  8. // Issues a Set configuration request
  9. #define TEST_SET_CONFIGURATION

  10. // Issues a claim interface request
  11. #define TEST_CLAIM_INTERFACE
  12. #define TEST_BULK_WRITE
  13. // Attempts one bulk read.
  14. #define TEST_BULK_READ

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

  17. //////////////////////////////////////////////////////////////////////////////
  18. // DEVICE SETUP (User configurable)

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

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

  25. // Device endpoint(s)
  26. #define EP_IN 0x86
  27. #define EP_OUT 0x02

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


  30. usb_dev_handle *open_dev(void)
  31. {
  32.         struct usb_bus *bus;
  33.         struct usb_device *dev;

  34.         for (bus = usb_get_busses(); bus; bus = bus->next)
  35.         {
  36.                 for (dev = bus->devices; dev; dev = dev->next)
  37.                 {
  38.                         if (dev->descriptor.idVendor == MY_VID
  39.                                 && dev->descriptor.idProduct == MY_PID)
  40.                         {
  41.                                 return usb_open(dev);
  42.                         }
  43.                 }
  44.         }
  45.         return NULL;
  46. }

  47. int main(void)
  48. {
  49.         usb_dev_handle *dev = NULL; /* the device handle */
  50.         unsigned char tmpwrite[BUF_SIZE] = "hello world\n";
  51.         unsigned char tmpread[BUF_SIZE] = {0};
  52.         for (int i = 0; i < BUF_SIZE;i++)
  53.         {
  54.                 tmpread[i] = 0;
  55.         }
  56.         int ret;
  57.         void* async_read_context = NULL;
  58.         void* async_write_context = NULL;

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


  62.         if (!(dev = open_dev()))
  63.         {
  64.                 printf("error opening device: \n%s\n", usb_strerror());
  65.                 return 0;
  66.         }
  67.         else
  68.         {
  69.                 printf("success: device %04X:%04X opened\n", MY_VID, MY_PID);
  70.         }

  71. #ifdef TEST_SET_CONFIGURATION
  72.         if (usb_set_configuration(dev, MY_CONFIG) < 0)
  73.         {
  74.                 printf("error setting config #%d: %s\n", MY_CONFIG, usb_strerror());
  75.                 usb_close(dev);
  76.                 return 0;
  77.         }
  78.         else
  79.         {
  80.                 printf("success: set configuration #%d\n", MY_CONFIG);
  81.         }
  82. #endif

  83. #ifdef TEST_CLAIM_INTERFACE
  84.         if (usb_claim_interface(dev, 0) < 0)
  85.         {
  86.                 printf("error claiming interface #%d:\n%s\n", MY_INTF, usb_strerror());
  87.                 usb_close(dev);
  88.                 return 0;
  89.         }
  90.         else
  91.         {
  92.                 printf("success: claim_interface #%d\n", MY_INTF);
  93.         }
  94. #endif

  95. #ifdef TEST_BULK_WRITE


  96. #ifdef TEST_ASYNC
  97.         // Running an async write test
  98.         ret = transfer_bulk_async(dev, EP_OUT, tmp, sizeof(tmp), 5000);
  99. #else
  100.         // Running a sync write test
  101.         ret = usb_bulk_write(dev, EP_OUT, (char *)tmpwrite, 2045, 5000);
  102. #endif
  103.         if (ret < 0)
  104.         {
  105.                 printf("error writing:\n%s\n", usb_strerror());
  106.         }
  107.         else
  108.         {
  109.                 printf("success: bulk write %d bytes\n", ret);
  110.         }
  111. #endif

  112. #ifdef TEST_BULK_READ


  113.         // Running a sync read test
  114.         ret = usb_bulk_read(dev, EP_IN, (char *)tmpread, sizeof(tmpwrite), 4);
  115.         if (ret < 0)
  116.         {
  117.                 printf("error reading:\n%s\n", usb_strerror());
  118.         }
  119.         else
  120.         {
  121.                 printf("success: bulk read %d bytes\n", ret);
  122.         }
  123. #endif

  124. #ifdef TEST_CLAIM_INTERFACE
  125.         usb_release_interface(dev, 0);
  126. #endif

  127.         if (dev)
  128.         {
  129.                 usb_close(dev);
  130.         }
  131.         printf("Done.\n");
  132.         printf((char *)tmpread);
  133.         system("pause");

  134.         return 0;
  135. }

然后可以顺利从端点六读取数据
 楼主| kohillyang 发表于 2015-9-16 12:46 | 显示全部楼层
问题是速度好慢,只有400KB/s
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

11

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部