From cfb3ae23f8108d3d2273b9972398c0f7cb7c2522 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Sat, 26 Nov 2022 15:25:27 +0800 Subject: [PATCH] update template --- demo/cdc_acm_template.c | 8 +- demo/mtp_template.c | 166 ++++++++++++++++++++++++++++++++++++++ demo/winusb1.0_template.c | 92 +++++++++++---------- 3 files changed, 223 insertions(+), 43 deletions(-) create mode 100644 demo/mtp_template.c diff --git a/demo/cdc_acm_template.c b/demo/cdc_acm_template.c index 2fa76058..754845ef 100644 --- a/demo/cdc_acm_template.c +++ b/demo/cdc_acm_template.c @@ -94,7 +94,7 @@ static const uint8_t cdc_descriptor[] = { }; USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048]; -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 }; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048]; volatile bool ep_tx_busy_flag = false; @@ -149,6 +149,11 @@ struct usbd_interface intf1; void cdc_acm_init(void) { + const uint8_t data[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 }; + + memcpy(&write_buffer[0], data, 10); + memset(&write_buffer[10], 'a', 2038); + usbd_desc_register(cdc_descriptor); usbd_add_interface(usbd_cdc_acm_init_intf(&intf0)); usbd_add_interface(usbd_cdc_acm_init_intf(&intf1)); @@ -171,7 +176,6 @@ void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr) void cdc_acm_data_send_with_dtr_test(void) { if (dtr_enable) { - memset(&write_buffer[10], 'a', 2038); ep_tx_busy_flag = true; usbd_ep_start_write(CDC_IN_EP, write_buffer, 2048); while (ep_tx_busy_flag) { diff --git a/demo/mtp_template.c b/demo/mtp_template.c new file mode 100644 index 00000000..20d3fb6d --- /dev/null +++ b/demo/mtp_template.c @@ -0,0 +1,166 @@ +#include "usbd_core.h" +#include "usbd_mtp.h" + +#define WCID_VENDOR_CODE 0x01 + +__ALIGN_BEGIN const uint8_t WCID_StringDescriptor_MSOS[18] __ALIGN_END = { + /////////////////////////////////////// + /// MS OS string descriptor + /////////////////////////////////////// + 0x12, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + /* MSFT100 */ + 'M', 0x00, 'S', 0x00, 'F', 0x00, 'T', 0x00, /* wcChar_7 */ + '1', 0x00, '0', 0x00, '0', 0x00, /* wcChar_7 */ + WCID_VENDOR_CODE, /* bVendorCode */ + 0x00, /* bReserved */ +}; + +__ALIGN_BEGIN const uint8_t WINUSB_WCIDDescriptor[40] __ALIGN_END = { + /////////////////////////////////////// + /// WCID descriptor + /////////////////////////////////////// + 0x28, 0x00, 0x00, 0x00, /* dwLength */ + 0x00, 0x01, /* bcdVersion */ + 0x04, 0x00, /* wIndex */ + 0x01, /* bCount */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_7 */ + + /////////////////////////////////////// + /// WCID function descriptor + /////////////////////////////////////// + 0x00, /* bFirstInterfaceNumber */ + 0x01, /* bReserved */ + /* MTP */ + 'M', 'T', 'P', 0x00, 0x00, 0x00, 0x00, 0x00, /* cCID_8 */ + /* */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* cSubCID_8 */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* bReserved_6 */ +}; + +struct usb_msosv1_descriptor msosv1_desc = { + .string = (uint8_t *)WCID_StringDescriptor_MSOS, + .string_len = 18, + .vendor_code = WCID_VENDOR_CODE, + .compat_id = (uint8_t *)WINUSB_WCIDDescriptor, + .compat_id_len = sizeof(WINUSB_WCIDDescriptor), + .comp_id_property = NULL, + .comp_id_property_len = 0, +}; + +/*!< endpoint address */ +#define CDC_IN_EP 0x81 +#define CDC_OUT_EP 0x02 +#define CDC_INT_EP 0x83 + +#define USBD_VID 0xFFFE +#define USBD_PID 0xFFFF +#define USBD_MAX_POWER 100 +#define USBD_LANGID_STRING 1033 + +/*!< config descriptor size */ +#define USB_CONFIG_SIZE (9 + MTP_DESCRIPTOR_LEN) + +const uint8_t mtp_descriptor[] = { + USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0201, 0x01), + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + MTP_DESCRIPTOR_INIT(0x00, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP, 2), + /////////////////////////////////////// + /// string0 descriptor + /////////////////////////////////////// + USB_LANGID_INIT(USBD_LANGID_STRING), + /////////////////////////////////////// + /// string1 descriptor + /////////////////////////////////////// + 0x14, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'C', 0x00, /* wcChar0 */ + 'h', 0x00, /* wcChar1 */ + 'e', 0x00, /* wcChar2 */ + 'r', 0x00, /* wcChar3 */ + 'r', 0x00, /* wcChar4 */ + 'y', 0x00, /* wcChar5 */ + 'U', 0x00, /* wcChar6 */ + 'S', 0x00, /* wcChar7 */ + 'B', 0x00, /* wcChar8 */ + /////////////////////////////////////// + /// string2 descriptor + /////////////////////////////////////// + 0x26, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'C', 0x00, /* wcChar0 */ + 'h', 0x00, /* wcChar1 */ + 'e', 0x00, /* wcChar2 */ + 'r', 0x00, /* wcChar3 */ + 'r', 0x00, /* wcChar4 */ + 'y', 0x00, /* wcChar5 */ + 'U', 0x00, /* wcChar6 */ + 'S', 0x00, /* wcChar7 */ + 'B', 0x00, /* wcChar8 */ + ' ', 0x00, /* wcChar9 */ + 'M', 0x00, /* wcChar10 */ + 'T', 0x00, /* wcChar11 */ + 'P', 0x00, /* wcChar12 */ + ' ', 0x00, /* wcChar13 */ + 'D', 0x00, /* wcChar14 */ + 'E', 0x00, /* wcChar15 */ + 'M', 0x00, /* wcChar16 */ + 'O', 0x00, /* wcChar17 */ + /////////////////////////////////////// + /// string3 descriptor + /////////////////////////////////////// + 0x16, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + '2', 0x00, /* wcChar0 */ + '0', 0x00, /* wcChar1 */ + '2', 0x00, /* wcChar2 */ + '1', 0x00, /* wcChar3 */ + '0', 0x00, /* wcChar4 */ + '3', 0x00, /* wcChar5 */ + '1', 0x00, /* wcChar6 */ + '0', 0x00, /* wcChar7 */ + '0', 0x00, /* wcChar8 */ + '0', 0x00, /* wcChar9 */ +#ifdef CONFIG_USB_HS + /////////////////////////////////////// + /// device qualifier descriptor + /////////////////////////////////////// + 0x0a, + USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x02, + 0x02, + 0x01, + 0x40, + 0x01, + 0x00, +#endif + 0x00 +}; + +const uint8_t bos_descriptor[] = { + 0x05, 0x0f, 0x16, 0x00, 0x02, + 0x07, 0x10, 0x02, 0x06, 0x00, 0x00, 0x00, + 0x0a, 0x10, 0x03, 0x00, 0x0f, 0x00, 0x01, 0x01, 0xf4, 0x01 +}; + +void usbd_configure_done_callback(void) +{ +} + +struct usbd_interface intf0; + +struct usb_bos_descriptor bos_desc = { + .string = bos_descriptor, + .string_len = 22 +}; + +void mtp_init(void) +{ + usbd_desc_register(mtp_descriptor); + usbd_msosv1_desc_register(&msosv1_desc); + usbd_bos_desc_register(&bos_desc); + usbd_add_interface(usbd_mtp_init_intf(&intf0, CDC_OUT_EP, CDC_IN_EP, CDC_INT_EP)); + usbd_initialize(); +} \ No newline at end of file diff --git a/demo/winusb1.0_template.c b/demo/winusb1.0_template.c index afe4bb72..c040a4d4 100644 --- a/demo/winusb1.0_template.c +++ b/demo/winusb1.0_template.c @@ -93,39 +93,18 @@ struct usb_msosv1_descriptor msosv1_desc = { #define USB_CONFIG_SIZE (9 + 9 + 7 + 7) +#ifdef CONFIG_USB_HS +#define WINUSB_EP_MPS 512 +#else +#define WINUSB_EP_MPS 64 +#endif + const uint8_t winusb_descriptor[] = { USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xff, 0xff, 0xff, USBD_VID, USBD_PID, 0x0001, 0x01), USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), - /////////////////////////////////////// - /// interface descriptor - /////////////////////////////////////// - 0x09, /* bLength */ - USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ - 0x00, /* bInterfaceNumber */ - 0x00, /* bAlternateSetting */ - 0x02, /* bNumEndpoints */ - 0xff, /* bInterfaceClass */ - 0x01, /* bInterfaceSubClass */ - 0x00, /* bInterfaceProtocol */ - 0x02, /* iInterface */ - /////////////////////////////////////// - /// endpoint descriptor - /////////////////////////////////////// - 0x07, /* bLength */ - USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ - WINUSB_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes */ - 0x00, 0x02, /* wMaxPacketSize */ - 0x00, /* bInterval */ - /////////////////////////////////////// - /// endpoint descriptor - /////////////////////////////////////// - 0x07, /* bLength */ - USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ - WINUSB_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes */ - 0x00, 0x02, /* wMaxPacketSize */ - 0x00, /* bInterval */ + USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xff, 0x01, 0x00, 0x02), + USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_IN_EP, 0x02, WINUSB_EP_MPS, 0x00), + USB_ENDPOINT_DESCRIPTOR_INIT(WINUSB_OUT_EP, 0x02, WINUSB_EP_MPS, 0x00), /////////////////////////////////////// /// string0 descriptor /////////////////////////////////////// @@ -147,10 +126,29 @@ const uint8_t winusb_descriptor[] = { /////////////////////////////////////// /// string2 descriptor /////////////////////////////////////// - 26, - 0x03, - 'C', 0, 'M', 0, 'S', 0, 'I', 0, 'S', 0, '-', 0, 'D', 0, 'A', 0, - 'P', 0, ' ', 0, 'v', 0, '2', 0, + 0x2C, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'C', 0x00, /* wcChar0 */ + 'h', 0x00, /* wcChar1 */ + 'e', 0x00, /* wcChar2 */ + 'r', 0x00, /* wcChar3 */ + 'r', 0x00, /* wcChar4 */ + 'y', 0x00, /* wcChar5 */ + 'U', 0x00, /* wcChar6 */ + 'S', 0x00, /* wcChar7 */ + 'B', 0x00, /* wcChar8 */ + ' ', 0x00, /* wcChar9 */ + 'W', 0x00, /* wcChar10 */ + 'I', 0x00, /* wcChar11 */ + 'N', 0x00, /* wcChar12 */ + 'U', 0x00, /* wcChar13 */ + 'S', 0x00, /* wcChar14 */ + 'B', 0x00, /* wcChar15 */ + ' ', 0x00, /* wcChar16 */ + 'D', 0x00, /* wcChar17 */ + 'E', 0x00, /* wcChar18 */ + 'M', 0x00, /* wcChar19 */ + 'O', 0x00, /* wcChar20 */ /////////////////////////////////////// /// string3 descriptor /////////////////////////////////////// @@ -184,13 +182,10 @@ const uint8_t winusb_descriptor[] = { 0x00 }; -#ifdef CONFIG_USB_HS -#define WINUSB_OUT_EP_MPS 512 -#else -#define WINUSB_OUT_EP_MPS 64 -#endif - USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048]; + +volatile bool ep_tx_busy_flag = false; void usbd_configure_done_callback(void) { @@ -200,10 +195,25 @@ void usbd_configure_done_callback(void) void usbd_winusb_out(uint8_t ep, uint32_t nbytes) { + USB_LOG_RAW("actual out len:%d\r\n", nbytes); + // for (int i = 0; i < 100; i++) { + // printf("%02x ", read_buffer[i]); + // } + // printf("\r\n"); + /* setup next out ep read transfer */ + usbd_ep_start_read(CDC_OUT_EP, read_buffer, 2048); } void usbd_winusb_in(uint8_t ep, uint32_t nbytes) { + USB_LOG_RAW("actual in len:%d\r\n", nbytes); + + if ((nbytes % CDC_MAX_MPS) == 0 && nbytes) { + /* send zlp */ + usbd_ep_start_write(CDC_IN_EP, NULL, 0); + } else { + ep_tx_busy_flag = false; + } } struct usbd_endpoint winusb_out_ep = { @@ -218,7 +228,7 @@ struct usbd_endpoint winusb_in_ep = { struct usbd_interface intf0; -void daplink_winusb_init(void) +void winusb_init(void) { usbd_desc_register(winusb_descriptor); usbd_msosv1_desc_register(&msosv1_desc);