如何ESP32S3的USB实现winusb自定义设备方式

Moderator: ESP_Bob

chenkejin
Posts: 11
Joined: Sun Aug 14, 2022 3:38 pm

如何ESP32S3的USB实现winusb自定义设备方式

Postby chenkejin » Sun Sep 25, 2022 9:30 am

我正在考虑移植基于stm32的can2usb的固件(https://github.com/candle-usb/candleLight_fw)。该方案使用winusb方式现实,并且完全自定义usb的描述符。
我了解了esp32的tinyusb库,并使用了tusb_sample_descriptor_main的demo,但是这个demo并不完整,它只有简单示例如何配置DeviceDescriptor。
如何基于tinyusb实现自定义的usb通讯,并且符合winusb要求,以达到我的can2usb的目的?
以下为stm32的主要代码:
  1. /* CAN interface class callbacks structure */
  2. USBD_ClassTypeDef USBD_GS_CAN = {
  3.     USBD_GS_CAN_Start,
  4.     USBD_GS_CAN_DeInit,
  5.     USBD_GS_CAN_Setup,
  6.     NULL, // EP0_TxSent
  7.     USBD_GS_CAN_EP0_RxReady,
  8.     USBD_GS_CAN_DataIn,
  9.     USBD_GS_CAN_DataOut,
  10.     USBD_GS_CAN_SOF,
  11.     NULL, // IsoInComplete
  12.     NULL, // IsoOutComplete
  13.     USBD_GS_CAN_GetCfgDesc,
  14.     USBD_GS_CAN_GetCfgDesc,
  15.     USBD_GS_CAN_GetCfgDesc,
  16.     NULL, // GetDeviceQualifierDescriptor
  17.     USBD_GS_CAN_GetStrDesc // GetUsrStrDescriptor
  18. };
  19.  
  20. /* USB_DeviceDescriptor */
  21. static const uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] = {
  22.     0x12,                       /* bLength */
  23.     USB_DESC_TYPE_DEVICE,       /* bDescriptorType */
  24.     0x00,                       /* bcdUSB */
  25.     0x02,
  26.     0x00,                       /* bDeviceClass */
  27.     0x00,                       /* bDeviceSubClass */
  28.     0x00,                       /* bDeviceProtocol */
  29.     USB_MAX_EP0_SIZE,           /* bMaxPacketSize */
  30.     LOBYTE(USBD_VID),           /* idVendor */
  31.     HIBYTE(USBD_VID),
  32.     LOBYTE(USBD_PID_FS),        /* idProduct */
  33.     HIBYTE(USBD_PID_FS),
  34.     0x00,                       /* bcdDevice rel. 0.00 */
  35.     0x00,
  36.     USBD_IDX_MFC_STR,           /* Index of manufacturer string */
  37.     USBD_IDX_PRODUCT_STR,       /* Index of product string */
  38.     USBD_IDX_SERIAL_STR,        /* Index of serial number string */
  39.     USBD_MAX_NUM_CONFIGURATION  /* bNumConfigurations */
  40. } ;
  41.  
  42. /* Configuration Descriptor */
  43. static const uint8_t USBD_GS_CAN_CfgDesc[USB_CAN_CONFIG_DESC_SIZ] =
  44. {
  45.     /*---------------------------------------------------------------------------*/
  46.     /* Configuration Descriptor */
  47.     0x09,                             /* bLength */
  48.     USB_DESC_TYPE_CONFIGURATION,      /* bDescriptorType */
  49.     USB_CAN_CONFIG_DESC_SIZ,          /* wTotalLength */
  50.     0x00,
  51.     0x02,                             /* bNumInterfaces */
  52.     0x01,                             /* bConfigurationValue */
  53.     USBD_IDX_CONFIG_STR,              /* iConfiguration */
  54.     0x80,                             /* bmAttributes */
  55.     0x4B,                             /* MaxPower 150 mA */
  56.     /*---------------------------------------------------------------------------*/
  57.  
  58.     /*---------------------------------------------------------------------------*/
  59.     /* GS_USB Interface Descriptor */
  60.     0x09,                             /* bLength */
  61.     USB_DESC_TYPE_INTERFACE,          /* bDescriptorType */
  62.     0x00,                             /* bInterfaceNumber */
  63.     0x00,                             /* bAlternateSetting */
  64.     0x02,                             /* bNumEndpoints */
  65.     0xFF,                             /* bInterfaceClass: Vendor Specific*/
  66.     0xFF,                             /* bInterfaceSubClass: Vendor Specific */
  67.     0xFF,                             /* bInterfaceProtocol: Vendor Specific */
  68.     0x00,                             /* iInterface */
  69.     /*---------------------------------------------------------------------------*/
  70.  
  71.     /*---------------------------------------------------------------------------*/
  72.     /* EP1 descriptor */
  73.     0x07,                             /* bLength */
  74.     USB_DESC_TYPE_ENDPOINT,           /* bDescriptorType */
  75.     GSUSB_ENDPOINT_IN,                /* bEndpointAddress */
  76.     0x02,                             /* bmAttributes: bulk */
  77.     LOBYTE(CAN_DATA_MAX_PACKET_SIZE), /* wMaxPacketSize */
  78.     HIBYTE(CAN_DATA_MAX_PACKET_SIZE),
  79.     0x00,                             /* bInterval: */
  80.     /*---------------------------------------------------------------------------*/
  81.  
  82.     /*---------------------------------------------------------------------------*/
  83.     /* EP2 descriptor */
  84.     0x07,                             /* bLength */
  85.     USB_DESC_TYPE_ENDPOINT,           /* bDescriptorType */
  86.     GSUSB_ENDPOINT_OUT,               /* bEndpointAddress */
  87.     0x02,                             /* bmAttributes: bulk */
  88.     LOBYTE(CAN_DATA_MAX_PACKET_SIZE), /* wMaxPacketSize */
  89.     HIBYTE(CAN_DATA_MAX_PACKET_SIZE),
  90.     0x00,                             /* bInterval: */
  91.     /*---------------------------------------------------------------------------*/
  92.  
  93.     /*---------------------------------------------------------------------------*/
  94.     /* DFU Interface Descriptor */
  95.     /*---------------------------------------------------------------------------*/
  96.     0x09,                             /* bLength */
  97.     USB_DESC_TYPE_INTERFACE,          /* bDescriptorType */
  98.     DFU_INTERFACE_NUM,                /* bInterfaceNumber */
  99.     0x00,                             /* bAlternateSetting */
  100.     0x00,                             /* bNumEndpoints */
  101.     0xFE,                             /* bInterfaceClass: Vendor Specific*/
  102.     0x01,                             /* bInterfaceSubClass */
  103.     0x01,                             /* bInterfaceProtocol : Runtime mode */
  104.     DFU_INTERFACE_STR_INDEX,          /* iInterface */
  105.  
  106.     /*---------------------------------------------------------------------------*/
  107.     /* Run-Time DFU Functional Descriptor */
  108.     /*---------------------------------------------------------------------------*/
  109.     0x09,                             /* bLength */
  110.     0x21,                             /* bDescriptorType: DFU FUNCTIONAL */
  111.     0x0B,                             /* bmAttributes: detach, upload, download */
  112.     0xFF, 0x00,                       /* wDetachTimeOut */
  113.     0x00, 0x08,                       /* wTransferSize */
  114.     0x1a, 0x01,                       /* bcdDFUVersion: 1.1a */
  115.  
  116. };
  117.  
  118. /* Microsoft OS String Descriptor */
  119. static const uint8_t USBD_GS_CAN_WINUSB_STR[] =
  120. {
  121.     0x12,                    /* length */
  122.     0x03,                    /* descriptor type == string */
  123.     0x4D, 0x00, 0x53, 0x00,  /* signature: "MSFT100" */
  124.     0x46, 0x00, 0x54, 0x00,
  125.     0x31, 0x00, 0x30, 0x00,
  126.     0x30, 0x00,
  127.     USBD_GS_CAN_VENDOR_CODE, /* vendor code */
  128.     0x00                     /* padding */
  129. };
  130.  
  131. /*  Microsoft Compatible ID Feature Descriptor  */
  132. static const uint8_t USBD_MS_COMP_ID_FEATURE_DESC[] = {
  133.     0x40, 0x00, 0x00, 0x00, /* length */
  134.     0x00, 0x01,             /* version 1.0 */
  135.     0x04, 0x00,             /* descr index (0x0004) */
  136.     0x02,                   /* number of sections */
  137.     0x00, 0x00, 0x00, 0x00, /* reserved */
  138.     0x00, 0x00, 0x00,
  139.     0x00,                   /* interface number */
  140.     0x01,                   /* reserved */
  141.     0x57, 0x49, 0x4E, 0x55, /* compatible ID ("WINUSB\0\0") */
  142.     0x53, 0x42, 0x00, 0x00,
  143.     0x00, 0x00, 0x00, 0x00, /* sub-compatible ID */
  144.     0x00, 0x00, 0x00, 0x00,
  145.     0x00, 0x00, 0x00, 0x00, /* reserved */
  146.     0x00, 0x00,
  147.     0x01,                   /* interface number */
  148.     0x01,                   /* reserved */
  149.     0x57, 0x49, 0x4E, 0x55, /* compatible ID ("WINUSB\0\0") */
  150.     0x53, 0x42, 0x00, 0x00,
  151.     0x00, 0x00, 0x00, 0x00, /* sub-compatible ID */
  152.     0x00, 0x00, 0x00, 0x00,
  153.     0x00, 0x00, 0x00, 0x00, /* reserved */
  154.     0x00, 0x00
  155. };
  156.  
  157. /* Microsoft Extended Properties Feature Descriptor */
  158. static const uint8_t USBD_MS_EXT_PROP_FEATURE_DESC[] = {
  159.     0x92, 0x00, 0x00, 0x00, /* length */
  160.     0x00, 0x01,             /* version 1.0 */
  161.     0x05, 0x00,             /* descr index (0x0005) */
  162.     0x01, 0x00,             /* number of sections */
  163.     0x88, 0x00, 0x00, 0x00, /* property section size */
  164.     0x07, 0x00, 0x00, 0x00, /* property data type 7: Unicode REG_MULTI_SZ */
  165.     0x2a, 0x00,             /* property name length */
  166.  
  167.     0x44, 0x00, 0x65, 0x00, /* property name "DeviceInterfaceGUIDs" */
  168.     0x76, 0x00, 0x69, 0x00,
  169.     0x63, 0x00, 0x65, 0x00,
  170.     0x49, 0x00, 0x6e, 0x00,
  171.     0x74, 0x00, 0x65, 0x00,
  172.     0x72, 0x00, 0x66, 0x00,
  173.     0x61, 0x00, 0x63, 0x00,
  174.     0x65, 0x00, 0x47, 0x00,
  175.     0x55, 0x00, 0x49, 0x00,
  176.     0x44, 0x00, 0x73, 0x00,
  177.     0x00, 0x00,
  178.  
  179.     0x50, 0x00, 0x00, 0x00, /* property data length */
  180.  
  181.     0x7b, 0x00, 0x63, 0x00, /* property name: "{c15b4308-04d3-11e6-b3ea-6057189e6443}\0\0" */
  182.     0x31, 0x00, 0x35, 0x00,
  183.     0x62, 0x00, 0x34, 0x00,
  184.     0x33, 0x00, 0x30, 0x00,
  185.     0x38, 0x00, 0x2d, 0x00,
  186.     0x30, 0x00, 0x34, 0x00,
  187.     0x64, 0x00, 0x33, 0x00,
  188.     0x2d, 0x00, 0x31, 0x00,
  189.     0x31, 0x00, 0x65, 0x00,
  190.     0x36, 0x00, 0x2d, 0x00,
  191.     0x62, 0x00, 0x33, 0x00,
  192.     0x65, 0x00, 0x61, 0x00,
  193.     0x2d, 0x00, 0x36, 0x00,
  194.     0x30, 0x00, 0x35, 0x00,
  195.     0x37, 0x00, 0x31, 0x00,
  196.     0x38, 0x00, 0x39, 0x00,
  197.     0x65, 0x00, 0x36, 0x00,
  198.     0x34, 0x00, 0x34, 0x00,
  199.     0x33, 0x00, 0x7d, 0x00,
  200.     0x00, 0x00, 0x00, 0x00
  201. };

Who is online

Users browsing this forum: No registered users and 5 guests