[应用相关] 自定义USB设备开发详细流程讲解(基于libusb)

[复制链接]
4082|23
 楼主| gaonaiweng 发表于 2021-11-29 15:14 | 显示全部楼层 |阅读模式
USB的用途就不多说了,下面的内容主要就是讲解如何利用ST提供的USB驱动库和libusb上位机驱动库实现一个USB数据传输功能,为了降低开发难度,我们仅仅讲解Bulk传输模式,当然这也是用得比较多的传输模式。

开发流程
1,完成STM32单片机端的USB程序;
2,利用linusb自带的inf-wizard工具生成USB驱动;
3,基于libusb编写USB通信程序;
4,测试PC和单片机的数据通信;
小叶三千 发表于 2021-11-29 15:17 | 显示全部楼层
感谢分享,这个Bulk传输模式应该是需要驱动程序吧?可以无驱运行吗
 楼主| gaonaiweng 发表于 2021-11-29 15:17 | 显示全部楼层
STM32程序编写
1,完成描述符的修改,修改后的描述符如下(在usb_desc.c文件中)
设备描述符:
 楼主| gaonaiweng 发表于 2021-11-29 15:18 | 显示全部楼层
  1. const
  2.               uint8_t CustomHID_DeviceDescriptor[CUSTOMHID_SIZ_DEVICE_DESC] =
  3.             

  4.              {
  5.             

  6.                  
  7.              0x12,                       
  8.              /*bLength */
  9.             

  10.                  
  11.              USB_DEVICE_DESCRIPTOR_TYPE,
  12.              /*bDescriptorType*/
  13.             

  14.                  
  15.              0x00,                       
  16.              /*bcdUSB */
  17.             

  18.                  
  19.              0x02,
  20.             

  21.                  
  22.              0x00,                       
  23.              /*bDeviceClass*/
  24.             

  25.                  
  26.              0x00,                       
  27.              /*bDeviceSubClass*/
  28.             

  29.                  
  30.              0x00,                       
  31.              /*bDeviceProtocol*/
  32.             

  33.                  
  34.              0x40,                       
  35.              /*bMaxPacketSize40*/
  36.             

  37.                  
  38.              LOBYTE(USBD_VID),           
  39.              /*idVendor*/
  40.             

  41.                  
  42.              HIBYTE(USBD_VID),           
  43.              /*idVendor*/
  44.             

  45.                  
  46.              LOBYTE(USBD_PID),           
  47.              /*idVendor*/
  48.             

  49.                  
  50.              HIBYTE(USBD_PID),           
  51.              /*idVendor*/
  52.             

  53.                  
  54.              0x00,                       
  55.              /*bcdDevice rel. 2.00*/
  56.             

  57.                  
  58.              0x02,
  59.             

  60.                  
  61.              1,                          
  62.              /*Index of string descriptor describing manufacturer */
  63.             

  64.                  
  65.              2,                          
  66.              /*Index of string descriptor describing product*/
  67.             

  68.                  
  69.              3,                          
  70.              /*Index of string descriptor describing the device serial number */
  71.             

  72.                  
  73.              0x01                        
  74.              /*bNumConfigurations*/
  75.             

  76.              };
  77.              /* CustomHID_DeviceDescriptor */
 楼主| gaonaiweng 发表于 2021-11-29 15:19 | 显示全部楼层
配置描述符:
  1.   const
  2.               uint8_t CustomHID_ConfigDescriptor[CUSTOMHID_SIZ_CONFIG_DESC] =
  3.             

  4.              {
  5.             

  6.                  
  7.              0x09,
  8.              /* bLength: Configuation Descriptor size */
  9.             

  10.                  
  11.              USB_CONFIGURATION_DESCRIPTOR_TYPE,
  12.              /* bDescriptorType: Configuration */
  13.             

  14.                  
  15.              CUSTOMHID_SIZ_CONFIG_DESC,
  16.             

  17.                  
  18.              /* wTotalLength: Bytes returned */
  19.             

  20.                  
  21.              0x00,
  22.             

  23.                  
  24.              0x01,         
  25.              /* bNumInterfaces: 1 interface */
  26.             

  27.                  
  28.              0x01,         
  29.              /* bConfigurationValue: Configuration value */
  30.             

  31.                  
  32.              0x00,         
  33.              /* iConfiguration: Index of string descriptor describing
  34.             

  35.                                              
  36.              the configuration*/
  37.             

  38.                  
  39.              0xE0,         
  40.              /* bmAttributes: Bus powered */
  41.             

  42.                               
  43.              /*Bus powered: 7th bit, Self Powered: 6th bit, Remote wakeup: 5th bit, reserved: 4..0 bits */
  44.             

  45.                  
  46.              0xFA,         
  47.              /* MaxPower 500 mA: this current is used for detecting Vbus */
  48.             

  49.                  
  50.              /************** Descriptor of Custom HID interface ****************/
  51.             

  52.                  
  53.              /* 09 */
  54.             

  55.                  
  56.              0x09,         
  57.              /* bLength: Interface Descriptor size */
  58.             

  59.                  
  60.              USB_INTERFACE_DESCRIPTOR_TYPE,
  61.              /* bDescriptorType: Interface descriptor type */
  62.             

  63.                  
  64.              0x00,         
  65.              /* bInterfaceNumber: Number of Interface */
  66.             

  67.                  
  68.              0x00,         
  69.              /* bAlternateSetting: Alternate setting */
  70.             

  71.                  
  72.              0x04,         
  73.              /* bNumEndpoints */
  74.             

  75.                  
  76.              0xDC,         
  77.              /* bInterfaceClass: Class code = 0DCH */
  78.             

  79.                  
  80.              0xA0,         
  81.              /* bInterfaceSubClass : Subclass code = 0A0H */
  82.             

  83.                  
  84.              0xB0,         
  85.              /* nInterfaceProtocol : Protocol code = 0B0H */
  86.             

  87.                  
  88.              0,            
  89.              /* iInterface: Index of string descriptor */
  90.             

  91.                  
  92.              /******************** endpoint descriptor ********************/
  93.             

  94.                  
  95.              /* 18 */
  96.             

  97.                  
  98.              0x07,         
  99.              /* endpoint descriptor length = 07H */
  100.             

  101.                  
  102.              USB_ENDPOINT_DESCRIPTOR_TYPE,
  103.              /* endpoint descriptor type = 05H */
  104.             

  105.                  
  106.              0x81,         
  107.              /* endpoint 1 IN */
  108.             

  109.                  
  110.              0x02,                                       
  111.              /* bulk transfer = 02H */
  112.             

  113.                  
  114.              0x40,0x00,   
  115.              /* endpoint max packet size = 0040H */
  116.             

  117.                  
  118.              0x00,         
  119.              /* the value is invalid when bulk transfer */
  120.             

  121.                
  122.             

  123.                  
  124.              0x07,         
  125.              /* endpoint descriptor length = 07H */
  126.             

  127.                  
  128.              USB_ENDPOINT_DESCRIPTOR_TYPE,
  129.              /* endpoint descriptor type = 05H */
  130.             

  131.                  
  132.              0x01,         
  133.              /* endpoint 1 OUT */
  134.             

  135.                  
  136.              0x02,                                       
  137.              /* bulk transfer = 02H */
  138.             

  139.                  
  140.              0x40,0x00,   
  141.              /* endpoint max packet size = 0040H */
  142.             

  143.                  
  144.              0x00,         
  145.              /* the value is invalid when bulk transfer */
  146.             

  147.                               
  148.             

  149.                  
  150.              0x07,         
  151.              /* endpoint descriptor length = 07H */
  152.             

  153.                  
  154.              USB_ENDPOINT_DESCRIPTOR_TYPE,
  155.              /* endpoint descriptor type = 05H */
  156.             

  157.                  
  158.              0x82,         
  159.              /* endpoint 2 IN */
  160.             

  161.                  
  162.              0x02,                                       
  163.              /* bulk transfer = 02H */
  164.             

  165.                  
  166.              0x40,0x00,   
  167.              /* endpoint max packet size = 0040H */
  168.             

  169.                  
  170.              0x00,         
  171.              /* the value is invalid when bulk transfer */
  172.             

  173.                               
  174.             

  175.                  
  176.              0x07,         
  177.              /* endpoint descriptor length = 07H */
  178.             

  179.                  
  180.              USB_ENDPOINT_DESCRIPTOR_TYPE,
  181.              /* endpoint descriptor type = 05H */
  182.             

  183.                  
  184.              0x02,         
  185.              /* endpoint 2 OUT */
  186.             

  187.                  
  188.              0x02,                                       
  189.              /* bulk transfer = 02H */
  190.             

  191.                  
  192.              0x40,0x00,   
  193.              /* endpoint max packet size = 0040H */
  194.             

  195.                  
  196.              0x00,         
  197.              /* the value is invalid when bulk transfer */
  198.             

  199.              };
  200.              /* CustomHID_ConfigDescriptor */
  201.             
 楼主| gaonaiweng 发表于 2021-11-29 15:24 | 显示全部楼层
配置描述符就包含了端点描述符,我们用了4个端点,两个BULK-OUT端点,两个BULK-IN端点。

其他的描述符:
  1. /* USB String Descriptors (optional) */
  2.             

  3.              const
  4.               uint8_t CustomHID_StringLangID[CUSTOMHID_SIZ_STRING_LANGID] =
  5.             

  6.              {
  7.             

  8.                  
  9.              CUSTOMHID_SIZ_STRING_LANGID,
  10.             

  11.                  
  12.              USB_STRING_DESCRIPTOR_TYPE,
  13.             

  14.                  
  15.              0x09,
  16.             

  17.                  
  18.              0x04
  19.             

  20.              };
  21.              /* LangID = 0x0409: U.S. English */
  22.             

  23.                
  24.             

  25.              const
  26.               uint8_t CustomHID_StringVendor[CUSTOMHID_SIZ_STRING_VENDOR] =
  27.             

  28.              {
  29.             

  30.                  
  31.              CUSTOMHID_SIZ_STRING_VENDOR,
  32.              /* Size of Vendor string */
  33.             

  34.                  
  35.              USB_STRING_DESCRIPTOR_TYPE,  
  36.              /* bDescriptorType*/
  37.             

  38.                  
  39.              // Manufacturer: "STMicroelectronics"
  40.             

  41.                  
  42.              'M'
  43.              , 0,
  44.              'y'
  45.              , 0,
  46.              'U'
  47.              , 0,
  48.              'S'
  49.              , 0,
  50.              'B'
  51.              , 0,
  52.              '_'
  53.              , 0,
  54.              'H'
  55.              , 0,
  56.              'I'
  57.              ,0,
  58.              'D'
  59.              ,0
  60.             

  61.              };
  62.             

  63.                
  64.             

  65.              const
  66.               uint8_t CustomHID_StringProduct[CUSTOMHID_SIZ_STRING_PRODUCT] =
  67.             

  68.              {
  69.             

  70.                  
  71.              CUSTOMHID_SIZ_STRING_PRODUCT,         
  72.              /* bLength */
  73.             

  74.                  
  75.              USB_STRING_DESCRIPTOR_TYPE,        
  76.              /* bDescriptorType */
  77.             

  78.                  
  79.              'B'
  80.              , 0,
  81.              'y'
  82.              , 0,
  83.              ' '
  84.              , 0,
  85.              'e'
  86.              , 0,
  87.              'm'
  88.              , 0,
  89.              'b'
  90.              , 0,
  91.              'e'
  92.              ,0,
  93.              'd'
  94.              ,0,
  95.              '-'
  96.              ,0,
  97.              'n'
  98.              ,0,
  99.              'e'
  100.              ,0,
  101.              't'
  102.              ,0
  103.             

  104.              };
  105.             

  106.              uint8_t CustomHID_StringSerial[CUSTOMHID_SIZ_STRING_SERIAL] =
  107.             

  108.              {
  109.             

  110.                  
  111.              CUSTOMHID_SIZ_STRING_SERIAL,           
  112.              /* bLength */
  113.             

  114.                  
  115.              USB_STRING_DESCRIPTOR_TYPE,        
  116.              /* bDescriptorType */
  117.             

  118.                  
  119.              'x'
  120.              , 0,
  121.              'x'
  122.              , 0,
  123.              'x'
  124.              , 0,
  125.              'x'
  126.              , 0,
  127.              'x'
  128.              , 0,
  129.              'x'
  130.              , 0,
  131.              'x'
  132.              , 0
  133.             

  134.              };
  135.             
 楼主| gaonaiweng 发表于 2021-11-29 15:25 | 显示全部楼层
2,根据端点缓冲区大小配置端点缓冲区地址,配置信息如下(在usb_conf.h文件中):

  1.              /* buffer table base address */
  2.             

  3.              #define BTABLE_ADDRESS      (0x00)
  4.             

  5.                
  6.             

  7.              /* EP0  */
  8.             

  9.              /* rx/tx buffer base address */
  10.             

  11.              #define ENDP0_RXADDR        (0x18)
  12.             

  13.              #define ENDP0_TXADDR        (0x58)
  14.             

  15.                
  16.             

  17.              /* EP1  */
  18.             

  19.              /* tx buffer base address */
  20.             

  21.              //地址为32位对其,位4的倍数,不能超过 bMaxPacketSize
  22.             

  23.              //EP1
  24.             

  25.              #define ENDP1_RXADDR        (0x98)
  26.             

  27.              #define ENDP1_TXADDR        (0x98+64)
  28.             

  29.              EP2
  30.             

  31.              #define ENDP2_RXADDR        (0xA0+64+64)
  32.             

  33.              #define ENDP2_TXADDR        (0xA0+64+64+64)
  34.             
 楼主| gaonaiweng 发表于 2021-11-29 15:28 | 显示全部楼层
3,初始化每个端点(在usb_prop.c文件中的CustomHID_Reset函数中)
  1.   /* Initialize Endpoint 0 */
  2.             

  3.              SetEPType(ENDP0, EP_CONTROL);
  4.             

  5.              SetEPTxStatus(ENDP0, EP_TX_STALL);
  6.             

  7.              SetEPRxAddr(ENDP0, ENDP0_RXADDR);
  8.             

  9.              SetEPTxAddr(ENDP0, ENDP0_TXADDR);
  10.             

  11.              Clear_Status_Out(ENDP0);
  12.             

  13.              SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
  14.             

  15.              SetEPRxValid(ENDP0);
  16.             

  17.                
  18.             

  19.              /* Initialize Endpoint 1 */
  20.             

  21.                     
  22.              SetEPType(ENDP1, EP_BULK);
  23.             

  24.                     
  25.              SetEPRxAddr(ENDP1, ENDP1_RXADDR);
  26.             

  27.                     
  28.              SetEPTxAddr(ENDP1, ENDP1_TXADDR);
  29.             

  30.                     
  31.              SetEPRxCount(ENDP1, EP_SIZE);
  32.             

  33.                     
  34.              SetEPRxStatus(ENDP1, EP_RX_VALID);
  35.             

  36.               
  37.              SetEPTxStatus(ENDP1, EP_TX_NAK);
  38.             

  39.                
  40.             

  41.              /* Initialize Endpoint 2 */
  42.             

  43.                     
  44.              SetEPType(ENDP2, EP_BULK);
  45.             

  46.                     
  47.              SetEPRxAddr(ENDP2, ENDP2_RXADDR);
  48.             

  49.                     
  50.              SetEPTxAddr(ENDP2, ENDP2_TXADDR);
  51.             

  52.                     
  53.              SetEPRxCount(ENDP2, EP_SIZE);
  54.             

  55.                     
  56.              SetEPRxStatus(ENDP2, EP_RX_VALID);
  57.             

  58.                     
  59.              SetEPTxStatus(ENDP2, EP_TX_NAK);
  60.             
 楼主| gaonaiweng 发表于 2021-11-29 15:29 | 显示全部楼层
4,实现端点的回调函数(需要在usb_conf.h中注释掉对应的回调函数宏定义)
 楼主| gaonaiweng 发表于 2021-11-29 15:31 | 显示全部楼层
  1.        /*******************************************************************************
  2.             

  3.              * Function Name  : EP1_OUT_Callback.
  4.             

  5.              * Description    : EP1 OUT Callback Routine.
  6.             

  7.              * Input          : None.
  8.             

  9.              * Output         : None.
  10.             

  11.              * Return         : None.
  12.             

  13.              *******************************************************************************/
  14.             

  15.              void
  16.               EP1_OUT_Callback(
  17.              void
  18.              )
  19.             

  20.              {
  21.             

  22.                      
  23.              EP1_ReceivedCount = GetEPRxCount(ENDP1);
  24.             

  25.                      
  26.              PMAToUserBufferCopy(USB_Receive_Buffer, ENDP1_RXADDR, EP1_ReceivedCount);
  27.             

  28.                      
  29.              SetEPRxStatus(ENDP1, EP_RX_VALID);
  30.             

  31.              }
  32.             

  33.              /*******************************************************************************
  34.             

  35.              * Function Name  : EP2_OUT_Callback.
  36.             

  37.              * Description    : EP2 OUT Callback Routine.
  38.             

  39.              * Input          : None.
  40.             

  41.              * Output         : None.
  42.             

  43.              * Return         : None.
  44.             

  45.              *******************************************************************************/
  46.             

  47.              void
  48.               EP2_OUT_Callback(
  49.              void
  50.              )
  51.             

  52.              {
  53.             

  54.                      
  55.              EP2_ReceivedCount = GetEPRxCount(ENDP2);
  56.             

  57.                      
  58.              PMAToUserBufferCopy(USB_Receive_Buffer, ENDP2_RXADDR, EP2_ReceivedCount);
  59.             

  60.                      
  61.              SetEPRxStatus(ENDP2, EP_RX_VALID);
  62.             

  63.              }
 楼主| gaonaiweng 发表于 2021-11-29 15:33 | 显示全部楼层
5,完成主函数的测试程序

  1.              int
  2.               main(
  3.              void
  4.              )
  5.             

  6.              {
  7.             

  8.                      
  9.              uint8_t data[256];
  10.             

  11.                      
  12.              uint32_t i=0;
  13.             

  14.                      
  15.              Set_System();
  16.              //系统时钟初始化
  17.             

  18.                      
  19.              USART_Configuration();
  20.              //串口1初始化
  21.             

  22.                      
  23.              printf
  24.              (
  25.              "\x0c\0"
  26.              );
  27.              printf
  28.              (
  29.              "\x0c\0"
  30.              );
  31.              //超级终端清屏
  32.             

  33.                      
  34.              printf
  35.              (
  36.              "\033[1;40;32m"
  37.              );
  38.              //设置超级终端背景为黑色,字符为绿色
  39.             

  40.                      
  41.              printf
  42.              (
  43.              "\r\n*******************************************************************************"
  44.              );
  45.             

  46.                      
  47.              printf
  48.              (
  49.              "\r\n************************ Copyright 2009-2012, EmbedNet ************************"
  50.              );
  51.             

  52.                      
  53.              printf
  54.              (
  55.              "\r\n*************************** http://www.embed-net.com **************************"
  56.              );
  57.             

  58.                      
  59.              printf
  60.              (
  61.              "\r\n***************************** All Rights Reserved *****************************"
  62.              );
  63.             

  64.                      
  65.              printf
  66.              (
  67.              "\r\n*******************************************************************************"
  68.              );
  69.             

  70.                      
  71.              printf
  72.              (
  73.              "\r\n"
  74.              );
  75.             

  76.                
  77.             

  78.                      
  79.              USB_Interrupts_Config();
  80.             

  81.                      
  82.              Set_USBClock();
  83.             

  84.                      
  85.              USB_Init();
  86.             

  87.                
  88.             

  89.                      
  90.              while
  91.              (1)
  92.             

  93.                      
  94.              {
  95.             

  96.                              
  97.              if
  98.              (EP1_ReceivedCount > 0){
  99.             

  100.                                     
  101.              USB_GetData(ENDP1,data,EP1_ReceivedCount);
  102.             

  103.                                     
  104.              USB_SendData(ENDP1,data,EP1_ReceivedCount);
  105.             

  106.                                     
  107.              printf
  108.              (
  109.              "usb EP1 get data %d byte data\n\r"
  110.              ,EP1_ReceivedCount);
  111.             

  112.                                     
  113.              for
  114.              (i=0;i<EP1_ReceivedCount;i++){
  115.             

  116.                                              
  117.              printf
  118.              (
  119.              "0x%02X "
  120.              ,data[i]);
  121.             

  122.                                     
  123.              }
  124.             

  125.                                     
  126.              printf
  127.              (
  128.              "\n\r"
  129.              );
  130.             

  131.                                     
  132.              EP1_ReceivedCount=0;
  133.             

  134.                              
  135.              }
  136.             

  137.                              
  138.              if
  139.              (EP2_ReceivedCount > 0){
  140.             

  141.                                     
  142.              USB_GetData(ENDP2,data,EP2_ReceivedCount);
  143.             

  144.                                     
  145.              USB_SendData(ENDP2,data,EP2_ReceivedCount);
  146.             

  147.                                     
  148.              printf
  149.              (
  150.              "usb EP2 get data %d byte data\n\r"
  151.              ,EP2_ReceivedCount);
  152.             

  153.                                     
  154.              for
  155.              (i=0;i<EP2_ReceivedCount;i++){
  156.             

  157.                                              
  158.              printf
  159.              (
  160.              "0x%02X "
  161.              ,data[i]);
  162.             

  163.                                     
  164.              }
  165.             

  166.                                     
  167.              printf
  168.              (
  169.              "\n\r"
  170.              );
  171.             

  172.                                     
  173.              EP2_ReceivedCount=0;        
  174.             

  175.                              
  176.              }
  177.             

  178.                      
  179.              }
  180.             

  181.              }
 楼主| gaonaiweng 发表于 2021-11-29 15:34 | 显示全部楼层
到此,STM32的程序基本上编写完成,然后编译下载程序,如果一切顺利,系统会检测到一个新的设备并试图加载对应的驱动,由于我们还没做驱动程序,所以肯定会加载驱动失败。

驱动程序生成
下面我们就利用libusb自带的inf-wizard工具生成USB驱动程序,该工具可以到本**的附件下载,其具体过程如下:

 楼主| gaonaiweng 发表于 2021-11-29 15:36 | 显示全部楼层
运行该程序,出现下图对话框,点击“Next”;


出现下图对话框后选择我们需要生成驱动程序的设备;


这里可以写该Device Name,我们保持默认值,其他的都不需要修改;


点击Next后出现下图对话框,我们选择一个目录保存这个inf文件;


保存后的文件
 楼主| gaonaiweng 发表于 2021-11-29 15:37 | 显示全部楼层
若要立即安装驱动,可以点击下面对话框的红色框按钮;


Win7下可能会出现如下对话框,点击始终安装;


到此,USB驱动程序自动生成完毕,若安装了驱动,则在设备管理器里面会看到如下信息


基于libusb的上位机驱动程序编写
首先建立一个驱动程序工程,然后将libusb的库(附件有下载)添加到工程里面,编写以下几个函数
设备扫描函数,该函数用来找到插入电脑上的USB设备
 楼主| gaonaiweng 发表于 2021-11-29 15:39 | 显示全部楼层
  /**
            

               
             * @brief  扫描设备连接数
            

               
             * @param  NeedInit 是否需要初始化,第一次调用该函数需要初始化
            

               
             * @retval 识别到的指定设备个数
            

               
             */
            

             int
              __stdcall USBScanDev(
             int
              NeedInit)
            

             {
            

                     
             if
             (NeedInit){
            

                             
             usb_init();
             /* initialize the library */
            

                             
             usb_find_busses();
             /* find all busses */
            

                             
             usb_find_devices();
             /* find all connected devices */
            

                     
             }
            

                     
             return
              scan_dev(pBoard);
            

             }
            
 楼主| gaonaiweng 发表于 2021-11-29 15:39 | 显示全部楼层
打开设备
  1.   /**
  2.             

  3.                
  4.              * @brief  打开指定的USB设备
  5.             

  6.                
  7.              * @param  devNum        需要打开的设备号
  8.             

  9.                
  10.              * @retval 打开状态
  11.             

  12.                
  13.              */
  14.             

  15.              int
  16.               __stdcall USBOpenDev(
  17.              int
  18.               DevIndex)
  19.             

  20.              {
  21.             

  22.                      
  23.              pBoardHandle[DevIndex] = open_dev(DevIndex,pBoard);
  24.             

  25.                      
  26.              if
  27.              (pBoardHandle[DevIndex]==NULL){
  28.             

  29.                              
  30.              return
  31.               SEVERITY_ERROR;
  32.             

  33.                      
  34.              }
  35.              else
  36.              {
  37.             

  38.                              
  39.              return
  40.               SEVERITY_SUCCESS;
  41.             

  42.                      
  43.              }
  44.             

  45.              }
  46.             
 楼主| gaonaiweng 发表于 2021-11-29 15:41 | 显示全部楼层
关闭设备
  1.      /**
  2.             

  3.                
  4.              * @brief  关闭指定的USB设备
  5.             

  6.                
  7.              * @param  devNum        需要关闭的设备号
  8.             

  9.                
  10.              * @retval 打开状态
  11.             

  12.                
  13.              */
  14.             

  15.              int
  16.               __stdcall USBCloseDev(
  17.              int
  18.               DevIndex)
  19.             

  20.              {
  21.             

  22.                      
  23.              return
  24.               close_dev(DevIndex,pBoardHandle);
  25.             

  26.              }
  27.             
 楼主| gaonaiweng 发表于 2021-11-29 15:52 | 显示全部楼层
BULK端点写数据
 楼主| gaonaiweng 发表于 2021-11-29 15:54 | 显示全部楼层
  1.       /**
  2.             

  3.                
  4.              * @brief  USB Bulk端点写数据
  5.             

  6.                
  7.              * @param  nBoardID 设备号
  8.             

  9.                
  10.              * @param  pipenum 端点号
  11.             

  12.                
  13.              * @param  sendbuffer 发送数据缓冲区
  14.             

  15.                
  16.              * @param  len 发送数据字节数
  17.             

  18.                
  19.              * @param  waittime 超时时间
  20.             

  21.                
  22.              * @retval 成功发送的数据字节数
  23.             

  24.                
  25.              */
  26.             

  27.                
  28.             

  29.              int
  30.               __stdcall USBBulkWriteData(unsigned
  31.              int
  32.               nBoardID,
  33.              int
  34.               pipenum,
  35.              char
  36.               *sendbuffer,
  37.              int
  38.               len,
  39.              int
  40.               waittime)
  41.             

  42.              {
  43.             

  44.                      
  45.              int
  46.               ret=0;
  47.             

  48.                      
  49.              if
  50.              (pBoardHandle[nBoardID] == NULL){
  51.             

  52.                              
  53.              return
  54.               SEVERITY_ERROR;
  55.             

  56.                      
  57.              }
  58.             

  59.              #ifdef TEST_SET_CONFIGURATION
  60.             

  61.                  
  62.              if
  63.               (usb_set_configuration(pBoardHandle[nBoardID], MY_CONFIG) < 0)
  64.             

  65.                  
  66.              {
  67.             

  68.                      
  69.              usb_close(pBoardHandle[nBoardID]);
  70.             

  71.                      
  72.              return
  73.               SEVERITY_ERROR;
  74.             

  75.                  
  76.              }
  77.             

  78.              #endif
  79.             

  80.                
  81.             

  82.              #ifdef TEST_CLAIM_INTERFACE
  83.             

  84.                  
  85.              if
  86.               (usb_claim_interface(pBoardHandle[nBoardID], 0) < 0)
  87.             

  88.                  
  89.              {
  90.             

  91.                      
  92.              usb_close(pBoardHandle[nBoardID]);
  93.             

  94.                      
  95.              return
  96.               SEVERITY_ERROR;
  97.             

  98.                  
  99.              }
  100.             

  101.              #endif
  102.             

  103.                
  104.             

  105.              #if TEST_ASYNC
  106.             

  107.                  
  108.              // Running an async write test
  109.             

  110.                  
  111.              ret = transfer_bulk_async(dev, pipenum, sendbuffer, len, waittime);
  112.             

  113.              #else
  114.             

  115.                      
  116.              ret = usb_bulk_write(pBoardHandle[nBoardID], pipenum, sendbuffer, len, waittime);
  117.             

  118.                      
  119.              /*if((len%64) == 0){
  120.             

  121.                              
  122.              usb_bulk_write(pBoardHandle[nBoardID], pipenum, sendbuffer, 0, waittime);
  123.             

  124.                      
  125.              }*/
  126.             

  127.              #endif
  128.             

  129.              #ifdef TEST_CLAIM_INTERFACE
  130.             

  131.                  
  132.              usb_release_interface(pBoardHandle[nBoardID], 0);
  133.             

  134.              #endif
  135.             

  136.                  
  137.              return
  138.               ret;
  139.             

  140.              }
 楼主| gaonaiweng 发表于 2021-11-29 15:55 | 显示全部楼层
BULK端点读数据

  1.    /**
  2.             

  3.                
  4.              * @brief  USB Bulk读数据
  5.             

  6.                
  7.              * @param  nBoardID 设备号
  8.             

  9.                
  10.              * @param  pipenum 端点号
  11.             

  12.                
  13.              * @param  readbuffer 读取数据缓冲区
  14.             

  15.                
  16.              * @param  len 读取数据字节数
  17.             

  18.                
  19.              * @param  waittime 超时时间
  20.             

  21.                
  22.              * @retval 读到的数据字节数
  23.             

  24.                
  25.              */
  26.             

  27.              int
  28.               __stdcall USBBulkReadData(unsigned
  29.              int
  30.               nBoardID,
  31.              int
  32.               pipenum,
  33.              char
  34.               *readbuffer,
  35.              int
  36.               len,
  37.              int
  38.               waittime)
  39.             

  40.              {
  41.             

  42.                      
  43.              int
  44.               ret=0;
  45.             

  46.                      
  47.              if
  48.              (pBoardHandle[nBoardID] == NULL){
  49.             

  50.                              
  51.              return
  52.               SEVERITY_ERROR;
  53.             

  54.                      
  55.              }
  56.             

  57.              #ifdef TEST_SET_CONFIGURATION
  58.             

  59.                  
  60.              if
  61.               (usb_set_configuration(pBoardHandle[nBoardID], MY_CONFIG) < 0)
  62.             

  63.                  
  64.              {
  65.             

  66.                      
  67.              usb_close(pBoardHandle[nBoardID]);
  68.             

  69.                      
  70.              return
  71.               SEVERITY_ERROR;
  72.             

  73.                  
  74.              }
  75.             

  76.              #endif
  77.             

  78.                
  79.             

  80.              #ifdef TEST_CLAIM_INTERFACE
  81.             

  82.                  
  83.              if
  84.               (usb_claim_interface(pBoardHandle[nBoardID], 0) < 0)
  85.             

  86.                  
  87.              {
  88.             

  89.                      
  90.              usb_close(pBoardHandle[nBoardID]);
  91.             

  92.                      
  93.              return
  94.               SEVERITY_ERROR;
  95.             

  96.                  
  97.              }
  98.             

  99.              #endif
  100.             

  101.                
  102.             

  103.              #if TEST_ASYNC
  104.             

  105.                  
  106.              // Running an async read test
  107.             

  108.                  
  109.              ret = transfer_bulk_async(pGinkgoBoardHandle[nBoardID], pipenum, sendbuffer, len, waittime);
  110.             

  111.              #else
  112.             

  113.                      
  114.              ret = usb_bulk_read(pBoardHandle[nBoardID], pipenum, readbuffer, len, waittime);
  115.             

  116.              #endif
  117.             

  118.              #ifdef TEST_CLAIM_INTERFACE
  119.             

  120.                  
  121.              usb_release_interface(pBoardHandle[nBoardID], 0);
  122.             

  123.              #endif
  124.             

  125.                  
  126.              return
  127.               ret;
  128.             

  129.              }
  130.             
您需要登录后才可以回帖 登录 | 注册

本版积分规则

80

主题

812

帖子

3

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