如何ESP32S3的USB实现winusb自定义设备方式
Posted: 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的主要代码:
我了解了esp32的tinyusb库,并使用了tusb_sample_descriptor_main的demo,但是这个demo并不完整,它只有简单示例如何配置DeviceDescriptor。
如何基于tinyusb实现自定义的usb通讯,并且符合winusb要求,以达到我的can2usb的目的?
以下为stm32的主要代码:
- /* CAN interface class callbacks structure */
- USBD_ClassTypeDef USBD_GS_CAN = {
- USBD_GS_CAN_Start,
- USBD_GS_CAN_DeInit,
- USBD_GS_CAN_Setup,
- NULL, // EP0_TxSent
- USBD_GS_CAN_EP0_RxReady,
- USBD_GS_CAN_DataIn,
- USBD_GS_CAN_DataOut,
- USBD_GS_CAN_SOF,
- NULL, // IsoInComplete
- NULL, // IsoOutComplete
- USBD_GS_CAN_GetCfgDesc,
- USBD_GS_CAN_GetCfgDesc,
- USBD_GS_CAN_GetCfgDesc,
- NULL, // GetDeviceQualifierDescriptor
- USBD_GS_CAN_GetStrDesc // GetUsrStrDescriptor
- };
- /* USB_DeviceDescriptor */
- static const uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] = {
- 0x12, /* bLength */
- USB_DESC_TYPE_DEVICE, /* bDescriptorType */
- 0x00, /* bcdUSB */
- 0x02,
- 0x00, /* bDeviceClass */
- 0x00, /* bDeviceSubClass */
- 0x00, /* bDeviceProtocol */
- USB_MAX_EP0_SIZE, /* bMaxPacketSize */
- LOBYTE(USBD_VID), /* idVendor */
- HIBYTE(USBD_VID),
- LOBYTE(USBD_PID_FS), /* idProduct */
- HIBYTE(USBD_PID_FS),
- 0x00, /* bcdDevice rel. 0.00 */
- 0x00,
- USBD_IDX_MFC_STR, /* Index of manufacturer string */
- USBD_IDX_PRODUCT_STR, /* Index of product string */
- USBD_IDX_SERIAL_STR, /* Index of serial number string */
- USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */
- } ;
- /* Configuration Descriptor */
- static const uint8_t USBD_GS_CAN_CfgDesc[USB_CAN_CONFIG_DESC_SIZ] =
- {
- /*---------------------------------------------------------------------------*/
- /* Configuration Descriptor */
- 0x09, /* bLength */
- USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType */
- USB_CAN_CONFIG_DESC_SIZ, /* wTotalLength */
- 0x00,
- 0x02, /* bNumInterfaces */
- 0x01, /* bConfigurationValue */
- USBD_IDX_CONFIG_STR, /* iConfiguration */
- 0x80, /* bmAttributes */
- 0x4B, /* MaxPower 150 mA */
- /*---------------------------------------------------------------------------*/
- /*---------------------------------------------------------------------------*/
- /* GS_USB Interface Descriptor */
- 0x09, /* bLength */
- USB_DESC_TYPE_INTERFACE, /* bDescriptorType */
- 0x00, /* bInterfaceNumber */
- 0x00, /* bAlternateSetting */
- 0x02, /* bNumEndpoints */
- 0xFF, /* bInterfaceClass: Vendor Specific*/
- 0xFF, /* bInterfaceSubClass: Vendor Specific */
- 0xFF, /* bInterfaceProtocol: Vendor Specific */
- 0x00, /* iInterface */
- /*---------------------------------------------------------------------------*/
- /*---------------------------------------------------------------------------*/
- /* EP1 descriptor */
- 0x07, /* bLength */
- USB_DESC_TYPE_ENDPOINT, /* bDescriptorType */
- GSUSB_ENDPOINT_IN, /* bEndpointAddress */
- 0x02, /* bmAttributes: bulk */
- LOBYTE(CAN_DATA_MAX_PACKET_SIZE), /* wMaxPacketSize */
- HIBYTE(CAN_DATA_MAX_PACKET_SIZE),
- 0x00, /* bInterval: */
- /*---------------------------------------------------------------------------*/
- /*---------------------------------------------------------------------------*/
- /* EP2 descriptor */
- 0x07, /* bLength */
- USB_DESC_TYPE_ENDPOINT, /* bDescriptorType */
- GSUSB_ENDPOINT_OUT, /* bEndpointAddress */
- 0x02, /* bmAttributes: bulk */
- LOBYTE(CAN_DATA_MAX_PACKET_SIZE), /* wMaxPacketSize */
- HIBYTE(CAN_DATA_MAX_PACKET_SIZE),
- 0x00, /* bInterval: */
- /*---------------------------------------------------------------------------*/
- /*---------------------------------------------------------------------------*/
- /* DFU Interface Descriptor */
- /*---------------------------------------------------------------------------*/
- 0x09, /* bLength */
- USB_DESC_TYPE_INTERFACE, /* bDescriptorType */
- DFU_INTERFACE_NUM, /* bInterfaceNumber */
- 0x00, /* bAlternateSetting */
- 0x00, /* bNumEndpoints */
- 0xFE, /* bInterfaceClass: Vendor Specific*/
- 0x01, /* bInterfaceSubClass */
- 0x01, /* bInterfaceProtocol : Runtime mode */
- DFU_INTERFACE_STR_INDEX, /* iInterface */
- /*---------------------------------------------------------------------------*/
- /* Run-Time DFU Functional Descriptor */
- /*---------------------------------------------------------------------------*/
- 0x09, /* bLength */
- 0x21, /* bDescriptorType: DFU FUNCTIONAL */
- 0x0B, /* bmAttributes: detach, upload, download */
- 0xFF, 0x00, /* wDetachTimeOut */
- 0x00, 0x08, /* wTransferSize */
- 0x1a, 0x01, /* bcdDFUVersion: 1.1a */
- };
- /* Microsoft OS String Descriptor */
- static const uint8_t USBD_GS_CAN_WINUSB_STR[] =
- {
- 0x12, /* length */
- 0x03, /* descriptor type == string */
- 0x4D, 0x00, 0x53, 0x00, /* signature: "MSFT100" */
- 0x46, 0x00, 0x54, 0x00,
- 0x31, 0x00, 0x30, 0x00,
- 0x30, 0x00,
- USBD_GS_CAN_VENDOR_CODE, /* vendor code */
- 0x00 /* padding */
- };
- /* Microsoft Compatible ID Feature Descriptor */
- static const uint8_t USBD_MS_COMP_ID_FEATURE_DESC[] = {
- 0x40, 0x00, 0x00, 0x00, /* length */
- 0x00, 0x01, /* version 1.0 */
- 0x04, 0x00, /* descr index (0x0004) */
- 0x02, /* number of sections */
- 0x00, 0x00, 0x00, 0x00, /* reserved */
- 0x00, 0x00, 0x00,
- 0x00, /* interface number */
- 0x01, /* reserved */
- 0x57, 0x49, 0x4E, 0x55, /* compatible ID ("WINUSB\0\0") */
- 0x53, 0x42, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, /* sub-compatible ID */
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, /* reserved */
- 0x00, 0x00,
- 0x01, /* interface number */
- 0x01, /* reserved */
- 0x57, 0x49, 0x4E, 0x55, /* compatible ID ("WINUSB\0\0") */
- 0x53, 0x42, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, /* sub-compatible ID */
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, /* reserved */
- 0x00, 0x00
- };
- /* Microsoft Extended Properties Feature Descriptor */
- static const uint8_t USBD_MS_EXT_PROP_FEATURE_DESC[] = {
- 0x92, 0x00, 0x00, 0x00, /* length */
- 0x00, 0x01, /* version 1.0 */
- 0x05, 0x00, /* descr index (0x0005) */
- 0x01, 0x00, /* number of sections */
- 0x88, 0x00, 0x00, 0x00, /* property section size */
- 0x07, 0x00, 0x00, 0x00, /* property data type 7: Unicode REG_MULTI_SZ */
- 0x2a, 0x00, /* property name length */
- 0x44, 0x00, 0x65, 0x00, /* property name "DeviceInterfaceGUIDs" */
- 0x76, 0x00, 0x69, 0x00,
- 0x63, 0x00, 0x65, 0x00,
- 0x49, 0x00, 0x6e, 0x00,
- 0x74, 0x00, 0x65, 0x00,
- 0x72, 0x00, 0x66, 0x00,
- 0x61, 0x00, 0x63, 0x00,
- 0x65, 0x00, 0x47, 0x00,
- 0x55, 0x00, 0x49, 0x00,
- 0x44, 0x00, 0x73, 0x00,
- 0x00, 0x00,
- 0x50, 0x00, 0x00, 0x00, /* property data length */
- 0x7b, 0x00, 0x63, 0x00, /* property name: "{c15b4308-04d3-11e6-b3ea-6057189e6443}\0\0" */
- 0x31, 0x00, 0x35, 0x00,
- 0x62, 0x00, 0x34, 0x00,
- 0x33, 0x00, 0x30, 0x00,
- 0x38, 0x00, 0x2d, 0x00,
- 0x30, 0x00, 0x34, 0x00,
- 0x64, 0x00, 0x33, 0x00,
- 0x2d, 0x00, 0x31, 0x00,
- 0x31, 0x00, 0x65, 0x00,
- 0x36, 0x00, 0x2d, 0x00,
- 0x62, 0x00, 0x33, 0x00,
- 0x65, 0x00, 0x61, 0x00,
- 0x2d, 0x00, 0x36, 0x00,
- 0x30, 0x00, 0x35, 0x00,
- 0x37, 0x00, 0x31, 0x00,
- 0x38, 0x00, 0x39, 0x00,
- 0x65, 0x00, 0x36, 0x00,
- 0x34, 0x00, 0x34, 0x00,
- 0x33, 0x00, 0x7d, 0x00,
- 0x00, 0x00, 0x00, 0x00
- };