From d1841810bd5b77294dd415c91e8fc26f3ffd3f0b Mon Sep 17 00:00:00 2001 From: HalfSweet <60973476+HalfSweet@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:57:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E7=BB=93=E6=9E=84=20(#26)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dap_main.c | 612 ++++++-------------- dap_main.h | 118 +++- projects/HSLink-Pro/src/CMakeLists.txt | 9 +- projects/HSLink-Pro/src/HID_COMM/hid_comm.c | 77 +++ projects/HSLink-Pro/src/HID_COMM/hid_comm.h | 95 +++ projects/HSLink-Pro/src/main.c | 4 +- projects/HSLink-Pro/src/usb_config.h | 2 +- projects/HSLink-Pro/src/usb_configuration.c | 88 +++ projects/HSLink-Pro/src/usb_configuration.h | 9 + 9 files changed, 564 insertions(+), 450 deletions(-) create mode 100644 projects/HSLink-Pro/src/HID_COMM/hid_comm.c create mode 100644 projects/HSLink-Pro/src/HID_COMM/hid_comm.h create mode 100644 projects/HSLink-Pro/src/usb_configuration.c create mode 100644 projects/HSLink-Pro/src/usb_configuration.h diff --git a/dap_main.c b/dap_main.c index e0aad1c..54e1833 100644 --- a/dap_main.c +++ b/dap_main.c @@ -2,337 +2,172 @@ #include "DAP_config.h" #include "DAP.h" -#ifdef CONFIG_USE_HID_CONFIG -#include "usbd_hid.h" -#endif - -#define DAP_IN_EP 0x81 -#define DAP_OUT_EP 0x02 - -#define CDC_IN_EP 0x83 -#define CDC_OUT_EP 0x04 -#define CDC_INT_EP 0x85 - -#define MSC_IN_EP 0x86 -#define MSC_OUT_EP 0x07 - -#ifdef CONFIG_USE_HID_CONFIG -#define HID_IN_EP 0x88 -#define HID_OUT_EP 0x09 - -#ifdef CONFIG_USB_HS -#define HID_PACKET_SIZE 1024 -#else -#define HID_PACKET_SIZE 64 -#endif - -#endif - -#define USBD_VID 0x0D28 -#define USBD_PID 0x0204 -#define USBD_MAX_POWER 500 -#define USBD_LANGID_STRING 1033 - -#define CMSIS_DAP_INTERFACE_SIZE (9 + 7 + 7) - -#ifdef CONFIG_CHERRYDAP_USE_MSC -#define CONFIG_MSC_DESCRIPTOR_LEN CDC_ACM_DESCRIPTOR_LEN -#define CONFIG_MSC_INTF_NUM 1 -#define MSC_INTF_NUM (0x02 + 1) -#else -#define CONFIG_MSC_DESCRIPTOR_LEN 0 -#define CONFIG_MSC_INTF_NUM 0 -#define MSC_INTF_NUM (0x02) -#endif - -#ifdef CONFIG_USE_HID_CONFIG -#define CONFIG_HID_DESCRIPTOR_LEN (9 + 9 + 7 + 7) -#define CONFIG_HID_INTF_NUM 1 -#define HID_CUSTOM_REPORT_DESC_SIZE 38 -#define HIDRAW_INTERVAL 4 -#define HID_INTF_NUM (MSC_INTF_NUM + 1) - -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HID_PACKET_SIZE]; -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[HID_PACKET_SIZE]; -#else -#define CONFIG_HID_DESCRIPTOR_LEN 0 -#define CONFIG_HID_INTF_NUM 0 -#define HID_INTF_NUM (MSC_INTF_NUM) -#endif - -#define USB_CONFIG_SIZE (9 + CMSIS_DAP_INTERFACE_SIZE + CDC_ACM_DESCRIPTOR_LEN + CONFIG_MSC_DESCRIPTOR_LEN + CONFIG_HID_DESCRIPTOR_LEN) -#define INTF_NUM (2 + 1 + CONFIG_MSC_INTF_NUM + CONFIG_HID_INTF_NUM) - -#ifdef CONFIG_USB_HS -#if DAP_PACKET_SIZE != 512 -#error "DAP_PACKET_SIZE must be 512 in hs" -#endif -#else -#if DAP_PACKET_SIZE != 64 -#error "DAP_PACKET_SIZE must be 64 in fs" -#endif -#endif - -#define USBD_WINUSB_VENDOR_CODE 0x20 - -#define USBD_WEBUSB_ENABLE 0 -#define USBD_BULK_ENABLE 1 -#define USBD_WINUSB_ENABLE 1 - -/* WinUSB Microsoft OS 2.0 descriptor sizes */ -#define WINUSB_DESCRIPTOR_SET_HEADER_SIZE 10 -#define WINUSB_FUNCTION_SUBSET_HEADER_SIZE 8 -#define WINUSB_FEATURE_COMPATIBLE_ID_SIZE 20 - -#define FUNCTION_SUBSET_LEN 160 -#define DEVICE_INTERFACE_GUIDS_FEATURE_LEN 132 - -#define USBD_WINUSB_DESC_SET_LEN (WINUSB_DESCRIPTOR_SET_HEADER_SIZE + USBD_WEBUSB_ENABLE * FUNCTION_SUBSET_LEN + USBD_BULK_ENABLE * FUNCTION_SUBSET_LEN) +#define USB_CONFIG_SIZE (9 + CMSIS_DAP_INTERFACE_SIZE + CDC_ACM_DESCRIPTOR_LEN + CONFIG_MSC_DESCRIPTOR_LEN) +#define INTF_NUM (2 + 1 + CONFIG_MSC_INTF_NUM) __ALIGN_BEGIN const uint8_t USBD_WinUSBDescriptorSetDescriptor[] = { - WBVAL(WINUSB_DESCRIPTOR_SET_HEADER_SIZE), /* wLength */ - WBVAL(WINUSB_SET_HEADER_DESCRIPTOR_TYPE), /* wDescriptorType */ - 0x00, 0x00, 0x03, 0x06, /* >= Win 8.1 */ /* dwWindowsVersion*/ - WBVAL(USBD_WINUSB_DESC_SET_LEN), /* wDescriptorSetTotalLength */ + WBVAL(WINUSB_DESCRIPTOR_SET_HEADER_SIZE), /* wLength */ + WBVAL(WINUSB_SET_HEADER_DESCRIPTOR_TYPE), /* wDescriptorType */ + 0x00, 0x00, 0x03, 0x06, /* >= Win 8.1 */ /* dwWindowsVersion*/ + WBVAL(USBD_WINUSB_DESC_SET_LEN), /* wDescriptorSetTotalLength */ #if (USBD_WEBUSB_ENABLE) - WBVAL(WINUSB_FUNCTION_SUBSET_HEADER_SIZE), // wLength - WBVAL(WINUSB_SUBSET_HEADER_FUNCTION_TYPE), // wDescriptorType - 0, // bFirstInterface USBD_WINUSB_IF_NUM - 0, // bReserved - WBVAL(FUNCTION_SUBSET_LEN), // wSubsetLength - WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_SIZE), // wLength - WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_TYPE), // wDescriptorType - 'W', 'I', 'N', 'U', 'S', 'B', 0, 0, // CompatibleId - 0, 0, 0, 0, 0, 0, 0, 0, // SubCompatibleId - WBVAL(DEVICE_INTERFACE_GUIDS_FEATURE_LEN), // wLength - WBVAL(WINUSB_FEATURE_REG_PROPERTY_TYPE), // wDescriptorType - WBVAL(WINUSB_PROP_DATA_TYPE_REG_MULTI_SZ), // wPropertyDataType - WBVAL(42), // wPropertyNameLength - 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, - 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0, - 'G', 0, 'U', 0, 'I', 0, 'D', 0, 's', 0, 0, 0, - WBVAL(80), // wPropertyDataLength - '{', 0, - '9', 0, '2', 0, 'C', 0, 'E', 0, '6', 0, '4', 0, '6', 0, '2', 0, '-', 0, - '9', 0, 'C', 0, '7', 0, '7', 0, '-', 0, - '4', 0, '6', 0, 'F', 0, 'E', 0, '-', 0, - '9', 0, '3', 0, '3', 0, 'B', 0, '-', - 0, '3', 0, '1', 0, 'C', 0, 'B', 0, '9', 0, 'C', 0, '5', 0, 'A', 0, 'A', 0, '3', 0, 'B', 0, '9', 0, - '}', 0, 0, 0, 0, 0 + WBVAL(WINUSB_FUNCTION_SUBSET_HEADER_SIZE), // wLength + WBVAL(WINUSB_SUBSET_HEADER_FUNCTION_TYPE), // wDescriptorType + 0, // bFirstInterface USBD_WINUSB_IF_NUM + 0, // bReserved + WBVAL(FUNCTION_SUBSET_LEN), // wSubsetLength + WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_SIZE), // wLength + WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_TYPE), // wDescriptorType + 'W', 'I', 'N', 'U', 'S', 'B', 0, 0, // CompatibleId + 0, 0, 0, 0, 0, 0, 0, 0, // SubCompatibleId + WBVAL(DEVICE_INTERFACE_GUIDS_FEATURE_LEN), // wLength + WBVAL(WINUSB_FEATURE_REG_PROPERTY_TYPE), // wDescriptorType + WBVAL(WINUSB_PROP_DATA_TYPE_REG_MULTI_SZ), // wPropertyDataType + WBVAL(42), // wPropertyNameLength + 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, + 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0, + 'G', 0, 'U', 0, 'I', 0, 'D', 0, 's', 0, 0, 0, + WBVAL(80), // wPropertyDataLength + '{', 0, + '9', 0, '2', 0, 'C', 0, 'E', 0, '6', 0, '4', 0, '6', 0, '2', 0, '-', 0, + '9', 0, 'C', 0, '7', 0, '7', 0, '-', 0, + '4', 0, '6', 0, 'F', 0, 'E', 0, '-', 0, + '9', 0, '3', 0, '3', 0, 'B', 0, '-', + 0, '3', 0, '1', 0, 'C', 0, 'B', 0, '9', 0, 'C', 0, '5', 0, 'A', 0, 'A', 0, '3', 0, 'B', 0, '9', 0, + '}', 0, 0, 0, 0, 0 #endif #if USBD_BULK_ENABLE - WBVAL(WINUSB_FUNCTION_SUBSET_HEADER_SIZE), /* wLength */ - WBVAL(WINUSB_SUBSET_HEADER_FUNCTION_TYPE), /* wDescriptorType */ - 0, /* bFirstInterface USBD_BULK_IF_NUM*/ - 0, /* bReserved */ - WBVAL(FUNCTION_SUBSET_LEN), /* wSubsetLength */ - WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_SIZE), /* wLength */ - WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_TYPE), /* wDescriptorType */ - 'W', 'I', 'N', 'U', 'S', 'B', 0, 0, /* CompatibleId*/ - 0, 0, 0, 0, 0, 0, 0, 0, /* SubCompatibleId*/ - WBVAL(DEVICE_INTERFACE_GUIDS_FEATURE_LEN), /* wLength */ - WBVAL(WINUSB_FEATURE_REG_PROPERTY_TYPE), /* wDescriptorType */ - WBVAL(WINUSB_PROP_DATA_TYPE_REG_MULTI_SZ), /* wPropertyDataType */ - WBVAL(42), /* wPropertyNameLength */ - 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, - 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0, - 'G', 0, 'U', 0, 'I', 0, 'D', 0, 's', 0, 0, 0, - WBVAL(80), /* wPropertyDataLength */ - '{', 0, - 'C', 0, 'D', 0, 'B', 0, '3', 0, 'B', 0, '5', 0, 'A', 0, 'D', 0, '-', 0, - '2', 0, '9', 0, '3', 0, 'B', 0, '-', 0, - '4', 0, '6', 0, '6', 0, '3', 0, '-', 0, - 'A', 0, 'A', 0, '3', 0, '6', 0, '-', - 0, '1', 0, 'A', 0, 'A', 0, 'E', 0, '4', 0, '6', 0, '4', 0, '6', 0, '3', 0, '7', 0, '7', 0, '6', 0, - '}', 0, 0, 0, 0, 0 + WBVAL(WINUSB_FUNCTION_SUBSET_HEADER_SIZE), /* wLength */ + WBVAL(WINUSB_SUBSET_HEADER_FUNCTION_TYPE), /* wDescriptorType */ + 0, /* bFirstInterface USBD_BULK_IF_NUM*/ + 0, /* bReserved */ + WBVAL(FUNCTION_SUBSET_LEN), /* wSubsetLength */ + WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_SIZE), /* wLength */ + WBVAL(WINUSB_FEATURE_COMPATIBLE_ID_TYPE), /* wDescriptorType */ + 'W', 'I', 'N', 'U', 'S', 'B', 0, 0, /* CompatibleId*/ + 0, 0, 0, 0, 0, 0, 0, 0, /* SubCompatibleId*/ + WBVAL(DEVICE_INTERFACE_GUIDS_FEATURE_LEN), /* wLength */ + WBVAL(WINUSB_FEATURE_REG_PROPERTY_TYPE), /* wDescriptorType */ + WBVAL(WINUSB_PROP_DATA_TYPE_REG_MULTI_SZ), /* wPropertyDataType */ + WBVAL(42), /* wPropertyNameLength */ + 'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, + 'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0, + 'G', 0, 'U', 0, 'I', 0, 'D', 0, 's', 0, 0, 0, + WBVAL(80), /* wPropertyDataLength */ + '{', 0, + 'C', 0, 'D', 0, 'B', 0, '3', 0, 'B', 0, '5', 0, 'A', 0, 'D', 0, '-', 0, + '2', 0, '9', 0, '3', 0, 'B', 0, '-', 0, + '4', 0, '6', 0, '6', 0, '3', 0, '-', 0, + 'A', 0, 'A', 0, '3', 0, '6', 0, '-', + 0, '1', 0, 'A', 0, 'A', 0, 'E', 0, '4', 0, '6', 0, '4', 0, '6', 0, '3', 0, '7', 0, '7', 0, '6', 0, + '}', 0, 0, 0, 0, 0 #endif }; -#define USBD_NUM_DEV_CAPABILITIES (USBD_WEBUSB_ENABLE + USBD_WINUSB_ENABLE) - -#define USBD_WEBUSB_DESC_LEN 24 -#define USBD_WINUSB_DESC_LEN 28 - -#define USBD_BOS_WTOTALLENGTH (0x05 + \ - USBD_WEBUSB_DESC_LEN * USBD_WEBUSB_ENABLE + \ - USBD_WINUSB_DESC_LEN * USBD_WINUSB_ENABLE) - __ALIGN_BEGIN const uint8_t USBD_BinaryObjectStoreDescriptor[] = { - 0x05, /* bLength */ - 0x0f, /* bDescriptorType */ - WBVAL(USBD_BOS_WTOTALLENGTH), /* wTotalLength */ - USBD_NUM_DEV_CAPABILITIES, /* bNumDeviceCaps */ + 0x05, /* bLength */ + 0x0f, /* bDescriptorType */ + WBVAL(USBD_BOS_WTOTALLENGTH), /* wTotalLength */ + USBD_NUM_DEV_CAPABILITIES, /* bNumDeviceCaps */ #if (USBD_WEBUSB_ENABLE) - USBD_WEBUSB_DESC_LEN, /* bLength */ - 0x10, /* bDescriptorType */ - USB_DEVICE_CAPABILITY_PLATFORM, /* bDevCapabilityType */ - 0x00, /* bReserved */ - 0x38, 0xB6, 0x08, 0x34, /* PlatformCapabilityUUID */ - 0xA9, 0x09, 0xA0, 0x47, - 0x8B, 0xFD, 0xA0, 0x76, - 0x88, 0x15, 0xB6, 0x65, - WBVAL(0x0100), /* 1.00 */ /* bcdVersion */ - USBD_WINUSB_VENDOR_CODE, /* bVendorCode */ - 0, /* iLandingPage */ + USBD_WEBUSB_DESC_LEN, /* bLength */ + 0x10, /* bDescriptorType */ + USB_DEVICE_CAPABILITY_PLATFORM, /* bDevCapabilityType */ + 0x00, /* bReserved */ + 0x38, 0xB6, 0x08, 0x34, /* PlatformCapabilityUUID */ + 0xA9, 0x09, 0xA0, 0x47, + 0x8B, 0xFD, 0xA0, 0x76, + 0x88, 0x15, 0xB6, 0x65, + WBVAL(0x0100), /* 1.00 */ /* bcdVersion */ + USBD_WINUSB_VENDOR_CODE, /* bVendorCode */ + 0, /* iLandingPage */ #endif #if (USBD_WINUSB_ENABLE) - USBD_WINUSB_DESC_LEN, /* bLength */ - 0x10, /* bDescriptorType */ - USB_DEVICE_CAPABILITY_PLATFORM, /* bDevCapabilityType */ - 0x00, /* bReserved */ - 0xDF, 0x60, 0xDD, 0xD8, /* PlatformCapabilityUUID */ - 0x89, 0x45, 0xC7, 0x4C, - 0x9C, 0xD2, 0x65, 0x9D, - 0x9E, 0x64, 0x8A, 0x9F, - 0x00, 0x00, 0x03, 0x06, /* >= Win 8.1 */ /* dwWindowsVersion*/ - WBVAL(USBD_WINUSB_DESC_SET_LEN), /* wDescriptorSetTotalLength */ - USBD_WINUSB_VENDOR_CODE, /* bVendorCode */ - 0, /* bAltEnumCode */ + USBD_WINUSB_DESC_LEN, /* bLength */ + 0x10, /* bDescriptorType */ + USB_DEVICE_CAPABILITY_PLATFORM, /* bDevCapabilityType */ + 0x00, /* bReserved */ + 0xDF, 0x60, 0xDD, 0xD8, /* PlatformCapabilityUUID */ + 0x89, 0x45, 0xC7, 0x4C, + 0x9C, 0xD2, 0x65, 0x9D, + 0x9E, 0x64, 0x8A, 0x9F, + 0x00, 0x00, 0x03, 0x06, /* >= Win 8.1 */ /* dwWindowsVersion*/ + WBVAL(USBD_WINUSB_DESC_SET_LEN), /* wDescriptorSetTotalLength */ + USBD_WINUSB_VENDOR_CODE, /* bVendorCode */ + 0, /* bAltEnumCode */ #endif }; static const uint8_t device_descriptor[] = { - USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01), + USB_DEVICE_DESCRIPTOR_INIT(USB_2_1, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01), }; static const uint8_t config_descriptor[] = { - USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), - /* Interface 0 */ - USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xFF, 0x00, 0x00, 0x02), - /* Endpoint OUT 2 */ - USB_ENDPOINT_DESCRIPTOR_INIT(DAP_OUT_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), - /* Endpoint IN 1 */ - USB_ENDPOINT_DESCRIPTOR_INIT(DAP_IN_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), - CDC_ACM_DESCRIPTOR_INIT(0x01, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, DAP_PACKET_SIZE, 0x00), + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + /* Interface 0 */ + USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xFF, 0x00, 0x00, 0x02), + /* Endpoint OUT 2 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_OUT_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + /* Endpoint IN 1 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_IN_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + CDC_ACM_DESCRIPTOR_INIT(0x01, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, DAP_PACKET_SIZE, 0x00), #ifdef CONFIG_CHERRYDAP_USE_MSC - MSC_DESCRIPTOR_INIT(MSC_INTF_NUM, MSC_OUT_EP, MSC_IN_EP, DAP_PACKET_SIZE, 0x00), -#endif -#ifdef CONFIG_USE_HID_CONFIG - /************** Descriptor of Custom interface *****************/ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ - HID_INTF_NUM, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints */ - 0x03, /* bInterfaceClass: HID */ - 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ - 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ - 0, /* iInterface: Index of string descriptor */ - /******************** Descriptor of Custom HID ********************/ - 0x09, /* bLength: HID Descriptor size */ - HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */ - 0x11, /* bcdHID: HID Class Spec release number */ - 0x01, - 0x00, /* bCountryCode: Hardware target country */ - 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ - 0x22, /* bDescriptorType */ - HID_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */ - 0x00, - /******************** Descriptor of Custom in endpoint ********************/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ - HID_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */ - 0x03, /* bmAttributes: Interrupt endpoint */ - WBVAL(HID_PACKET_SIZE), /* wMaxPacketSize: 4 Byte max */ - HIDRAW_INTERVAL, /* bInterval: Polling Interval */ - /******************** Descriptor of Custom out endpoint ********************/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ - HID_OUT_EP, /* bEndpointAddress: Endpoint Address (IN) */ - 0x03, /* bmAttributes: Interrupt endpoint */ - WBVAL(HID_PACKET_SIZE), /* wMaxPacketSize: 4 Byte max */ - HIDRAW_INTERVAL, /* bInterval: Polling Interval */ + MSC_DESCRIPTOR_INIT(MSC_INTF_NUM, MSC_OUT_EP, MSC_IN_EP, DAP_PACKET_SIZE, 0x00), #endif }; static const uint8_t other_speed_config_descriptor[] = { - USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), - /* Interface 0 */ - USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xFF, 0x00, 0x00, 0x02), - /* Endpoint OUT 2 */ - USB_ENDPOINT_DESCRIPTOR_INIT(DAP_OUT_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), - /* Endpoint IN 1 */ - USB_ENDPOINT_DESCRIPTOR_INIT(DAP_IN_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), - CDC_ACM_DESCRIPTOR_INIT(0x01, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, DAP_PACKET_SIZE, 0x00), + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + /* Interface 0 */ + USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xFF, 0x00, 0x00, 0x02), + /* Endpoint OUT 2 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_OUT_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + /* Endpoint IN 1 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_IN_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + CDC_ACM_DESCRIPTOR_INIT(0x01, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, DAP_PACKET_SIZE, 0x00), #ifdef CONFIG_CHERRYDAP_USE_MSC - MSC_DESCRIPTOR_INIT(MSC_INTF_NUM, MSC_OUT_EP, MSC_IN_EP, DAP_PACKET_SIZE, 0x00), -#endif -#ifdef CONFIG_USE_HID_CONFIG - /************** Descriptor of Custom interface *****************/ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ - HID_INTF_NUM, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints */ - 0x03, /* bInterfaceClass: HID */ - 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ - 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ - 0, /* iInterface: Index of string descriptor */ - /******************** Descriptor of Custom HID ********************/ - 0x09, /* bLength: HID Descriptor size */ - HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */ - 0x11, /* bcdHID: HID Class Spec release number */ - 0x01, - 0x00, /* bCountryCode: Hardware target country */ - 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ - 0x22, /* bDescriptorType */ - HID_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */ - 0x00, - /******************** Descriptor of Custom in endpoint ********************/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ - HID_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */ - 0x03, /* bmAttributes: Interrupt endpoint */ - WBVAL(HID_PACKET_SIZE), /* wMaxPacketSize: 4 Byte max */ - HIDRAW_INTERVAL, /* bInterval: Polling Interval */ - /******************** Descriptor of Custom out endpoint ********************/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ - HID_OUT_EP, /* bEndpointAddress: Endpoint Address (IN) */ - 0x03, /* bmAttributes: Interrupt endpoint */ - WBVAL(HID_PACKET_SIZE), /* wMaxPacketSize: 4 Byte max */ - HIDRAW_INTERVAL, /* bInterval: Polling Interval */ + MSC_DESCRIPTOR_INIT(MSC_INTF_NUM, MSC_OUT_EP, MSC_IN_EP, DAP_PACKET_SIZE, 0x00), #endif }; char *string_descriptors[] = { - (char[]){ 0x09, 0x04 }, /* Langid */ - "CherryUSB", /* Manufacturer */ - "CherryUSB CMSIS-DAP", /* Product */ - "00000000000000000123456789ABCDEF", /* Serial Number */ + (char[]) {0x09, 0x04}, /* Langid */ + "CherryUSB", /* Manufacturer */ + "CherryUSB CMSIS-DAP", /* Product */ + "00000000000000000123456789ABCDEF", /* Serial Number */ }; static const uint8_t device_quality_descriptor[] = { - USB_DEVICE_QUALIFIER_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, 0x01), + USB_DEVICE_QUALIFIER_DESCRIPTOR_INIT(USB_2_1, 0x00, 0x00, 0x00, 0x01), }; -static const uint8_t *device_descriptor_callback(uint8_t speed) +__WEAK const uint8_t *device_descriptor_callback(uint8_t speed) { - (void)speed; + (void) speed; return device_descriptor; } -static const uint8_t *config_descriptor_callback(uint8_t speed) +__WEAK const uint8_t *config_descriptor_callback(uint8_t speed) { - (void)speed; + (void) speed; return config_descriptor; } -static const uint8_t *device_quality_descriptor_callback(uint8_t speed) +__WEAK const uint8_t *device_quality_descriptor_callback(uint8_t speed) { - (void)speed; + (void) speed; return device_quality_descriptor; } -static const uint8_t *other_speed_config_descriptor_callback(uint8_t speed) +__WEAK const uint8_t *other_speed_config_descriptor_callback(uint8_t speed) { - (void)speed; + (void) speed; return other_speed_config_descriptor; } -static const char *string_descriptor_callback(uint8_t speed, uint8_t index) +__WEAK const char *string_descriptor_callback(uint8_t speed, uint8_t index) { - (void)speed; + (void) speed; if (index >= (sizeof(string_descriptors) / sizeof(char *))) { return NULL; @@ -340,44 +175,17 @@ static const char *string_descriptor_callback(uint8_t speed, uint8_t index) return string_descriptors[index]; } -#ifdef CONFIG_USE_HID_CONFIG -/*!< custom hid report descriptor */ -static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = { - /* USER CODE BEGIN 0 */ - 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */ - 0x09, 0x01, /* USAGE (Vendor Usage 1) */ - 0xa1, 0x01, /* COLLECTION (Application) */ - 0x85, 0x02, /* REPORT ID (0x02) */ - 0x09, 0x02, /* USAGE (Vendor Usage 1) */ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ - 0x25, 0xff, /*LOGICAL_MAXIMUM (255) */ - 0x75, 0x08, /* REPORT_SIZE (8) */ - 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */ - 0x81, 0x02, /* INPUT (Data,Var,Abs) */ - /* <___________________________________________________> */ - 0x85, 0x01, /* REPORT ID (0x01) */ - 0x09, 0x03, /* USAGE (Vendor Usage 1) */ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ - 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */ - 0x75, 0x08, /* REPORT_SIZE (8) */ - 0x96, 0xff, 0x03, /* REPORT_COUNT (63) */ - 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ - /* USER CODE END 0 */ - 0xC0 /* END_COLLECTION */ -}; -#endif +static volatile uint16_t USB_RequestIndexI = 0; // Request Index In +static volatile uint16_t USB_RequestIndexO = 0; // Request Index Out +static volatile uint16_t USB_RequestCountI = 0; // Request Count In +static volatile uint16_t USB_RequestCountO = 0; // Request Count Out +static volatile uint8_t USB_RequestIdle = 1; // Request Idle Flag -static volatile uint16_t USB_RequestIndexI; // Request Index In -static volatile uint16_t USB_RequestIndexO; // Request Index Out -static volatile uint16_t USB_RequestCountI; // Request Count In -static volatile uint16_t USB_RequestCountO; // Request Count Out -static volatile uint8_t USB_RequestIdle; // Request Idle Flag - -static volatile uint16_t USB_ResponseIndexI; // Response Index In -static volatile uint16_t USB_ResponseIndexO; // Response Index Out -static volatile uint16_t USB_ResponseCountI; // Response Count In -static volatile uint16_t USB_ResponseCountO; // Response Count Out -static volatile uint8_t USB_ResponseIdle; // Response Idle Flag +static volatile uint16_t USB_ResponseIndexI = 0; // Response Index In +static volatile uint16_t USB_ResponseIndexO = 0; // Response Index Out +static volatile uint16_t USB_ResponseCountI = 0; // Response Count In +static volatile uint16_t USB_ResponseCountO = 0; // Response Count Out +static volatile uint8_t USB_ResponseIdle = 1; // Response Idle Flag static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t USB_Request[DAP_PACKET_COUNT][DAP_PACKET_SIZE]; // Request Buffer static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t USB_Response[DAP_PACKET_COUNT][DAP_PACKET_SIZE]; // Response Buffer @@ -387,12 +195,9 @@ volatile struct cdc_line_coding g_cdc_lincoding; volatile uint8_t config_uart = 0; volatile uint8_t config_uart_transfer = 0; -#define CONFIG_UARTRX_RINGBUF_SIZE (8 * 1024) -#define CONFIG_USBRX_RINGBUF_SIZE (8 * 1024) - -static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t uartrx_ringbuffer[CONFIG_UARTRX_RINGBUF_SIZE]; -static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usbrx_ringbuffer[CONFIG_USBRX_RINGBUF_SIZE]; -static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usb_tmpbuffer[DAP_PACKET_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t uartrx_ringbuffer[CONFIG_UARTRX_RINGBUF_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usbrx_ringbuffer[CONFIG_USBRX_RINGBUF_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usb_tmpbuffer[DAP_PACKET_SIZE]; static volatile uint8_t usbrx_idle_flag = 0; static volatile uint8_t usbtx_idle_flag = 0; @@ -403,7 +208,7 @@ USB_NOCACHE_RAM_SECTION chry_ringbuffer_t g_usbrx; void usbd_event_handler(uint8_t busid, uint8_t event) { - (void)busid; + (void) busid; switch (event) { case USBD_EVENT_RESET: usbrx_idle_flag = 0; @@ -426,10 +231,6 @@ void usbd_event_handler(uint8_t busid, uint8_t event) usbd_ep_start_read(0, DAP_OUT_EP, USB_Request[0], DAP_PACKET_SIZE); usbd_ep_start_read(0, CDC_OUT_EP, usb_tmpbuffer, DAP_PACKET_SIZE); -#ifdef CONFIG_USE_HID_CONFIG - usbd_ep_start_read(0, HID_OUT_EP, read_buffer, HID_PACKET_SIZE); -#endif - break; case USBD_EVENT_SET_REMOTE_WAKEUP: break; @@ -443,7 +244,7 @@ void usbd_event_handler(uint8_t busid, uint8_t event) void dap_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) { - (void)busid; + (void) busid; if (USB_Request[USB_RequestIndexI][0] == ID_DAP_TransferAbort) { DAP_TransferAbort = 1U; } else { @@ -455,7 +256,7 @@ void dap_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) } // Start reception of next request packet - if ((uint16_t)(USB_RequestCountI - USB_RequestCountO) != DAP_PACKET_COUNT) { + if ((uint16_t) (USB_RequestCountI - USB_RequestCountO) != DAP_PACKET_COUNT) { usbd_ep_start_read(0, DAP_OUT_EP, USB_Request[USB_RequestIndexI], DAP_PACKET_SIZE); } else { USB_RequestIdle = 1U; @@ -464,7 +265,7 @@ void dap_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) void dap_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) { - (void)busid; + (void) busid; if (USB_ResponseCountI != USB_ResponseCountO) { // Load data from response buffer to be sent back usbd_ep_start_write(0, DAP_IN_EP, USB_Response[USB_ResponseIndexO], USB_RespSize[USB_ResponseIndexO]); @@ -480,7 +281,7 @@ void dap_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes) { - (void)busid; + (void) busid; chry_ringbuffer_write(&g_usbrx, usb_tmpbuffer, nbytes); if (chry_ringbuffer_get_free(&g_usbrx) >= DAP_PACKET_SIZE) { usbd_ep_start_read(0, CDC_OUT_EP, usb_tmpbuffer, DAP_PACKET_SIZE); @@ -491,7 +292,7 @@ void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes) void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes) { - (void)busid; + (void) busid; uint32_t size; uint8_t *buffer; @@ -509,54 +310,25 @@ void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes) } } -static struct usbd_endpoint dap_out_ep = { - .ep_addr = DAP_OUT_EP, - .ep_cb = dap_out_callback -}; - -static struct usbd_endpoint dap_in_ep = { - .ep_addr = DAP_IN_EP, - .ep_cb = dap_in_callback -}; - -static struct usbd_endpoint cdc_out_ep = { - .ep_addr = CDC_OUT_EP, - .ep_cb = usbd_cdc_acm_bulk_out +struct usbd_endpoint dap_out_ep = { + .ep_addr = DAP_OUT_EP, + .ep_cb = dap_out_callback }; -static struct usbd_endpoint cdc_in_ep = { - .ep_addr = CDC_IN_EP, - .ep_cb = usbd_cdc_acm_bulk_in +struct usbd_endpoint dap_in_ep = { + .ep_addr = DAP_IN_EP, + .ep_cb = dap_in_callback }; -#ifdef CONFIG_USE_HID_CONFIG - -__WEAK void usbd_hid_custom_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) -{ - (void)busid; - USB_LOG_RAW("actual in len:%d\r\n", nbytes); - // custom_state = HID_STATE_IDLE; -} - -__WEAK void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) -{ - (void)busid; - USB_LOG_RAW("actual out len:%d\r\n", nbytes); - usbd_ep_start_read(0, ep, read_buffer, HID_PACKET_SIZE); - read_buffer[0] = 0x02; /* IN: report id */ - usbd_ep_start_write(0, HID_IN_EP, read_buffer, nbytes); -} - -static struct usbd_endpoint hid_custom_in_ep = { - .ep_cb = usbd_hid_custom_in_callback, - .ep_addr = HID_IN_EP +struct usbd_endpoint cdc_out_ep = { + .ep_addr = CDC_OUT_EP, + .ep_cb = usbd_cdc_acm_bulk_out }; -static struct usbd_endpoint hid_custom_out_ep = { - .ep_cb = usbd_hid_custom_out_callback, - .ep_addr = HID_OUT_EP +struct usbd_endpoint cdc_in_ep = { + .ep_addr = CDC_IN_EP, + .ep_cb = usbd_cdc_acm_bulk_in }; -#endif struct usbd_interface dap_intf; struct usbd_interface intf1; @@ -564,79 +336,27 @@ struct usbd_interface intf2; struct usbd_interface intf3; struct usbd_interface hid_intf; -static void chry_dap_state_init(void) -{ - // Initialize variables - USB_RequestIndexI = 0U; - USB_RequestIndexO = 0U; - USB_RequestCountI = 0U; - USB_RequestCountO = 0U; - USB_RequestIdle = 1U; - USB_ResponseIndexI = 0U; - USB_ResponseIndexO = 0U; - USB_ResponseCountI = 0U; - USB_ResponseCountO = 0U; - USB_ResponseIdle = 1U; -} - struct usb_msosv2_descriptor msosv2_desc = { - .vendor_code = USBD_WINUSB_VENDOR_CODE, - .compat_id = USBD_WinUSBDescriptorSetDescriptor, - .compat_id_len = USBD_WINUSB_DESC_SET_LEN, + .vendor_code = USBD_WINUSB_VENDOR_CODE, + .compat_id = USBD_WinUSBDescriptorSetDescriptor, + .compat_id_len = USBD_WINUSB_DESC_SET_LEN, }; struct usb_bos_descriptor bos_desc = { - .string = USBD_BinaryObjectStoreDescriptor, - .string_len = USBD_BOS_WTOTALLENGTH + .string = USBD_BinaryObjectStoreDescriptor, + .string_len = USBD_BOS_WTOTALLENGTH }; const struct usb_descriptor cmsisdap_descriptor = { - .device_descriptor_callback = device_descriptor_callback, - .config_descriptor_callback = config_descriptor_callback, - .device_quality_descriptor_callback = device_quality_descriptor_callback, - .other_speed_descriptor_callback = other_speed_config_descriptor_callback, - .string_descriptor_callback = string_descriptor_callback, - .bos_descriptor = &bos_desc, - .msosv2_descriptor = &msosv2_desc, + .device_descriptor_callback = device_descriptor_callback, + .config_descriptor_callback = config_descriptor_callback, + .device_quality_descriptor_callback = device_quality_descriptor_callback, + .other_speed_descriptor_callback = other_speed_config_descriptor_callback, + .string_descriptor_callback = string_descriptor_callback, + .bos_descriptor = &bos_desc, + .msosv2_descriptor = &msosv2_desc, }; -void chry_dap_init(uint8_t busid, uint32_t reg_base) -{ - chry_ringbuffer_init(&g_uartrx, uartrx_ringbuffer, CONFIG_UARTRX_RINGBUF_SIZE); - chry_ringbuffer_init(&g_usbrx, usbrx_ringbuffer, CONFIG_USBRX_RINGBUF_SIZE); - - DAP_Setup(); - - chry_dap_state_init(); - - usbd_desc_register(0, &cmsisdap_descriptor); - // usbd_bos_desc_register(0, &bos_desc); - // usbd_msosv2_desc_register(0, &msosv2_desc); - - /*!< winusb */ - usbd_add_interface(0, &dap_intf); - usbd_add_endpoint(0, &dap_out_ep); - usbd_add_endpoint(0, &dap_in_ep); - - /*!< cdc acm */ - usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf1)); - usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf2)); - usbd_add_endpoint(0, &cdc_out_ep); - usbd_add_endpoint(0, &cdc_in_ep); - -#ifdef CONFIG_USE_HID_CONFIG - /*!< hid */ - usbd_add_interface(0, usbd_hid_init_intf(0, &hid_intf, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE)); - usbd_add_endpoint(0, &hid_custom_in_ep); - usbd_add_endpoint(0, &hid_custom_out_ep); -#endif - -#ifdef CONFIG_CHERRYDAP_USE_MSC - usbd_add_interface(0, usbd_msc_init_intf(0, &intf3, MSC_OUT_EP, MSC_IN_EP)); -#endif - usbd_initialize(busid, reg_base, usbd_event_handler); -} - void chry_dap_handle(void) { uint32_t n; @@ -661,7 +381,7 @@ void chry_dap_handle(void) // Execute DAP Command (process request and prepare response) USB_RespSize[USB_ResponseIndexI] = - (uint16_t)DAP_ExecuteCommand(USB_Request[USB_RequestIndexO], USB_Response[USB_ResponseIndexI]); + (uint16_t) DAP_ExecuteCommand(USB_Request[USB_RequestIndexO], USB_Response[USB_ResponseIndexI]); // Update Request Index and Count USB_RequestIndexO++; @@ -671,7 +391,7 @@ void chry_dap_handle(void) USB_RequestCountO++; if (USB_RequestIdle) { - if ((uint16_t)(USB_RequestCountI - USB_RequestCountO) != DAP_PACKET_COUNT) { + if ((uint16_t) (USB_RequestCountI - USB_RequestCountO) != DAP_PACKET_COUNT) { USB_RequestIdle = 0U; usbd_ep_start_read(0, DAP_OUT_EP, USB_Request[USB_RequestIndexI], DAP_PACKET_SIZE); } @@ -701,9 +421,9 @@ void chry_dap_handle(void) void usbd_cdc_acm_set_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding) { - (void)busid; - if (memcmp(line_coding, (uint8_t *)&g_cdc_lincoding, sizeof(struct cdc_line_coding)) != 0) { - memcpy((uint8_t *)&g_cdc_lincoding, line_coding, sizeof(struct cdc_line_coding)); + (void) busid; + if (memcmp(line_coding, (uint8_t *) &g_cdc_lincoding, sizeof(struct cdc_line_coding)) != 0) { + memcpy((uint8_t *) &g_cdc_lincoding, line_coding, sizeof(struct cdc_line_coding)); config_uart = 1; config_uart_transfer = 0; } @@ -711,8 +431,8 @@ void usbd_cdc_acm_set_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_c void usbd_cdc_acm_get_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding) { - (void)busid; - memcpy(line_coding, (uint8_t *)&g_cdc_lincoding, sizeof(struct cdc_line_coding)); + (void) busid; + memcpy(line_coding, (uint8_t *) &g_cdc_lincoding, sizeof(struct cdc_line_coding)); } void chry_dap_usb2uart_handle(void) @@ -724,7 +444,7 @@ void chry_dap_usb2uart_handle(void) /* disable irq here */ config_uart = 0; /* config uart here */ - chry_dap_usb2uart_uart_config_callback((struct cdc_line_coding *)&g_cdc_lincoding); + chry_dap_usb2uart_uart_config_callback((struct cdc_line_coding *) &g_cdc_lincoding); usbtx_idle_flag = 1; uarttx_idle_flag = 1; config_uart_transfer = 1; diff --git a/dap_main.h b/dap_main.h index cb14914..73aeece 100644 --- a/dap_main.h +++ b/dap_main.h @@ -5,16 +5,132 @@ #include "usbd_cdc.h" #include "usbd_msc.h" #include "chry_ringbuffer.h" +#include "DAP_config.h" +#include "DAP.h" + +#define DAP_IN_EP 0x81 +#define DAP_OUT_EP 0x02 + +#define CDC_IN_EP 0x83 +#define CDC_OUT_EP 0x04 +#define CDC_INT_EP 0x85 + +#define MSC_IN_EP 0x86 +#define MSC_OUT_EP 0x07 + +#define USBD_VID 0x0D28 +#define USBD_PID 0x0204 +#define USBD_MAX_POWER 500 +#define USBD_LANGID_STRING 1033 + +#define CMSIS_DAP_INTERFACE_SIZE (9 + 7 + 7) + +#ifdef CONFIG_CHERRYDAP_USE_MSC +#define CONFIG_MSC_DESCRIPTOR_LEN CDC_ACM_DESCRIPTOR_LEN +#define CONFIG_MSC_INTF_NUM 1 +#define MSC_INTF_NUM (0x02 + 1) +#else +#define CONFIG_MSC_DESCRIPTOR_LEN 0 +#define CONFIG_MSC_INTF_NUM 0 +#define MSC_INTF_NUM (0x02) +#endif + +#ifdef CONFIG_USB_HS +#if DAP_PACKET_SIZE != 512 +#error "DAP_PACKET_SIZE must be 512 in hs" +#endif +#else +#if DAP_PACKET_SIZE != 64 +#error "DAP_PACKET_SIZE must be 64 in fs" +#endif +#endif + +#define USBD_WINUSB_VENDOR_CODE 0x20 + +#define USBD_WEBUSB_ENABLE 0 +#define USBD_BULK_ENABLE 1 +#define USBD_WINUSB_ENABLE 1 + +/* WinUSB Microsoft OS 2.0 descriptor sizes */ +#define WINUSB_DESCRIPTOR_SET_HEADER_SIZE 10 +#define WINUSB_FUNCTION_SUBSET_HEADER_SIZE 8 +#define WINUSB_FEATURE_COMPATIBLE_ID_SIZE 20 + +#define FUNCTION_SUBSET_LEN 160 +#define DEVICE_INTERFACE_GUIDS_FEATURE_LEN 132 + +#define USBD_WINUSB_DESC_SET_LEN (WINUSB_DESCRIPTOR_SET_HEADER_SIZE + USBD_WEBUSB_ENABLE * FUNCTION_SUBSET_LEN + USBD_BULK_ENABLE * FUNCTION_SUBSET_LEN) + +#define USBD_NUM_DEV_CAPABILITIES (USBD_WEBUSB_ENABLE + USBD_WINUSB_ENABLE) + +#define USBD_WEBUSB_DESC_LEN 24 +#define USBD_WINUSB_DESC_LEN 28 + +#define USBD_BOS_WTOTALLENGTH (0x05 + \ + USBD_WEBUSB_DESC_LEN * USBD_WEBUSB_ENABLE + \ + USBD_WINUSB_DESC_LEN * USBD_WINUSB_ENABLE) + +#define CONFIG_UARTRX_RINGBUF_SIZE (8 * 1024) +#define CONFIG_USBRX_RINGBUF_SIZE (8 * 1024) + +extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t uartrx_ringbuffer[CONFIG_UARTRX_RINGBUF_SIZE]; +extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usbrx_ringbuffer[CONFIG_USBRX_RINGBUF_SIZE]; +extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usb_tmpbuffer[DAP_PACKET_SIZE]; + +extern const struct usb_descriptor cmsisdap_descriptor; +extern __ALIGN_BEGIN const uint8_t USBD_WinUSBDescriptorSetDescriptor[]; +extern __ALIGN_BEGIN const uint8_t USBD_BinaryObjectStoreDescriptor[]; +extern char *string_descriptors[]; + +extern struct usbd_interface dap_intf; +extern struct usbd_interface intf1; +extern struct usbd_interface intf2; +extern struct usbd_interface intf3; +extern struct usbd_interface hid_intf; + +extern struct usbd_endpoint dap_out_ep; +extern struct usbd_endpoint dap_in_ep; +extern struct usbd_endpoint cdc_out_ep; +extern struct usbd_endpoint cdc_in_ep; extern chry_ringbuffer_t g_uartrx; extern chry_ringbuffer_t g_usbrx; -void chry_dap_init(uint8_t busid, uint32_t base); +__STATIC_INLINE void chry_dap_init(uint8_t busid, uint32_t reg_base) +{ + chry_ringbuffer_init(&g_uartrx, uartrx_ringbuffer, CONFIG_UARTRX_RINGBUF_SIZE); + chry_ringbuffer_init(&g_usbrx, usbrx_ringbuffer, CONFIG_USBRX_RINGBUF_SIZE); + + DAP_Setup(); + + usbd_desc_register(0, &cmsisdap_descriptor); + + /*!< winusb */ + usbd_add_interface(0, &dap_intf); + usbd_add_endpoint(0, &dap_out_ep); + usbd_add_endpoint(0, &dap_in_ep); + + /*!< cdc acm */ + usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf1)); + usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf2)); + usbd_add_endpoint(0, &cdc_out_ep); + usbd_add_endpoint(0, &cdc_in_ep); + +#ifdef CONFIG_CHERRYDAP_USE_MSC + usbd_add_interface(0, usbd_msc_init_intf(0, &intf3, MSC_OUT_EP, MSC_IN_EP)); +#endif + extern void usbd_event_handler(uint8_t busid, uint8_t event); + usbd_initialize(busid, reg_base, usbd_event_handler); +} + void chry_dap_handle(void); + void chry_dap_usb2uart_handle(void); void chry_dap_usb2uart_uart_config_callback(struct cdc_line_coding *line_coding); + void chry_dap_usb2uart_uart_send_bydma(uint8_t *data, uint16_t len); + void chry_dap_usb2uart_uart_send_complete(uint32_t size); #endif \ No newline at end of file diff --git a/projects/HSLink-Pro/src/CMakeLists.txt b/projects/HSLink-Pro/src/CMakeLists.txt index 5983e49..bfb969a 100644 --- a/projects/HSLink-Pro/src/CMakeLists.txt +++ b/projects/HSLink-Pro/src/CMakeLists.txt @@ -73,7 +73,6 @@ sdk_app_src(JTAG_DP/JTAG_DP.c) sdk_app_src(JTAG_DP/JTAG_DP_SPI.c) sdk_app_src(JTAG_DP/JTAG_DP_IO.c) - sdk_inc(.) sdk_inc(../../..) sdk_inc(../../../CherryRB) @@ -104,6 +103,14 @@ sdk_app_inc(USB2UART) sdk_compile_definitions(-DPRODUCT_STRING="HSLink Pro") sdk_compile_definitions(-DCONFIG_USE_HID_CONFIG=1) sdk_app_src(../common/HSLink_Pro_expansion.c) + +file(GLOB_RECURSE HID_COMM_SRC + "HID_COMM/*.c" +) +sdk_app_src(${HID_COMM_SRC}) +sdk_app_inc(HID_COMM) +sdk_app_src(usb_configuration.c) + sdk_compile_options("-g") # 执行完之后转换为uf2 diff --git a/projects/HSLink-Pro/src/HID_COMM/hid_comm.c b/projects/HSLink-Pro/src/HID_COMM/hid_comm.c new file mode 100644 index 0000000..bd82936 --- /dev/null +++ b/projects/HSLink-Pro/src/HID_COMM/hid_comm.c @@ -0,0 +1,77 @@ +// +// Created by HalfSweet on 24-9-25. +// + +#include "hid_comm.h" +#include "usbd_core.h" + +#include "dap_main.h" + +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HID_PACKET_SIZE]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[HID_PACKET_SIZE]; + +#ifdef CONFIG_USE_HID_CONFIG +/*!< custom hid report descriptor */ +const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = { + /* USER CODE BEGIN 0 */ + 0x06, 0x00, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */ + 0x09, 0x01, /* USAGE (Vendor Usage 1) */ + 0xa1, 0x01, /* COLLECTION (Application) */ + 0x85, 0x02, /* REPORT ID (0x02) */ + 0x09, 0x02, /* USAGE (Vendor Usage 1) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0xff, /*LOGICAL_MAXIMUM (255) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x96, 0xff, 0x03, /* REPORT_COUNT (1023) */ + 0x81, 0x02, /* INPUT (Data,Var,Abs) */ + /* <___________________________________________________> */ + 0x85, 0x01, /* REPORT ID (0x01) */ + 0x09, 0x03, /* USAGE (Vendor Usage 1) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x96, 0xff, 0x03, /* REPORT_COUNT (1023) */ + 0x91, 0x02, /* OUTPUT (Data,Var,Abs) */ + + /* <___________________________________________________> */ + 0x85, 0x03, /* REPORT ID (0x03) */ + 0x09, 0x04, /* USAGE (Vendor Usage 1) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0xff, /* LOGICAL_MAXIMUM (255) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x96, 0xff, 0x03, /* REPORT_COUNT (1023) */ + 0xb1, 0x02, /* FEATURE (Data,Var,Abs) */ + /* USER CODE END 0 */ + 0xC0 /* END_COLLECTION */ +}; +#endif + +#ifdef CONFIG_USE_HID_CONFIG + +__WEAK void usbd_hid_custom_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) +{ + (void) busid; + USB_LOG_RAW("actual in len:%d\r\n", nbytes); + // custom_state = HID_STATE_IDLE; +} + +__WEAK void usbd_hid_custom_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) +{ + (void) busid; + USB_LOG_RAW("actual out len:%d\r\n", nbytes); + usbd_ep_start_read(0, ep, read_buffer, HID_PACKET_SIZE); + read_buffer[0] = 0x02; /* IN: report id */ + usbd_ep_start_write(0, HID_IN_EP, read_buffer, nbytes); +} + +struct usbd_endpoint hid_custom_in_ep = { + .ep_cb = usbd_hid_custom_in_callback, + .ep_addr = HID_IN_EP +}; + +struct usbd_endpoint hid_custom_out_ep = { + .ep_cb = usbd_hid_custom_out_callback, + .ep_addr = HID_OUT_EP +}; +#endif + diff --git a/projects/HSLink-Pro/src/HID_COMM/hid_comm.h b/projects/HSLink-Pro/src/HID_COMM/hid_comm.h new file mode 100644 index 0000000..0fb506c --- /dev/null +++ b/projects/HSLink-Pro/src/HID_COMM/hid_comm.h @@ -0,0 +1,95 @@ +// +// Created by HalfSweet on 24-9-25. +// + +#ifndef HSLINK_PRO_HID_COMM_H +#define HSLINK_PRO_HID_COMM_H + +#include +#include "usbd_core.h" +#include "usbd_hid.h" + +#ifdef CONFIG_USE_HID_CONFIG + +#define HID_IN_EP 0x88 +#define HID_OUT_EP 0x09 + +#ifdef CONFIG_USB_HS +#define HID_PACKET_SIZE 1024 +#else +#define HID_PACKET_SIZE 64 +#endif + +#endif + +#ifdef CONFIG_USE_HID_CONFIG +#define HID_IN_EP 0x88 +#define HID_OUT_EP 0x09 + +#ifdef CONFIG_USB_HS +#define HID_PACKET_SIZE 1024 +#else +#define HID_PACKET_SIZE 64 +#endif + +#endif + +#ifdef CONFIG_USE_HID_CONFIG +#define CONFIG_HID_DESCRIPTOR_LEN (9 + 9 + 7 + 7) +#define CONFIG_HID_INTF_NUM 1 +#define HID_CUSTOM_REPORT_DESC_SIZE 53 +#define HIDRAW_INTERVAL 4 +#define HID_INTF_NUM (MSC_INTF_NUM + 1) + +#else +#define CONFIG_HID_DESCRIPTOR_LEN 0 +#define CONFIG_HID_INTF_NUM 0 +#define HID_INTF_NUM (MSC_INTF_NUM) +#endif + +#ifdef CONFIG_USE_HID_CONFIG +#define HID_DESC() \ + /************** Descriptor of Custom interface *****************/ \ + 0x09, /* bLength: Interface Descriptor size */ \ + USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ \ + HID_INTF_NUM, /* bInterfaceNumber: Number of Interface */ \ + 0x00, /* bAlternateSetting: Alternate setting */ \ + 0x02, /* bNumEndpoints */ \ + 0x03, /* bInterfaceClass: HID */ \ + 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ \ + 0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ \ + 0, /* iInterface: Index of string descriptor */ \ + /******************** Descriptor of Custom HID ********************/ \ + 0x09, /* bLength: HID Descriptor size */ \ + HID_DESCRIPTOR_TYPE_HID, /* bDescriptorType: HID */ \ + 0x11, /* bcdHID: HID Class Spec release number */ \ + 0x01, \ + 0x00, /* bCountryCode: Hardware target country */ \ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ \ + 0x22, /* bDescriptorType */ \ + HID_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */ \ + 0x00, \ + /******************** Descriptor of Custom in endpoint ********************/ \ + 0x07, /* bLength: Endpoint Descriptor size */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ \ + HID_IN_EP, /* bEndpointAddress: Endpoint Address (IN) */ \ + 0x03, /* bmAttributes: Interrupt endpoint */ \ + WBVAL(HID_PACKET_SIZE), /* wMaxPacketSize: 4 Byte max */ \ + HIDRAW_INTERVAL, /* bInterval: Polling Interval */ \ + /******************** Descriptor of Custom out endpoint ********************/ \ + 0x07, /* bLength: Endpoint Descriptor size */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType: */ \ + HID_OUT_EP, /* bEndpointAddress: Endpoint Address (IN) */ \ + 0x03, /* bmAttributes: Interrupt endpoint */ \ + WBVAL(HID_PACKET_SIZE), /* wMaxPacketSize: 4 Byte max */ \ + HIDRAW_INTERVAL, /* bInterval: Polling Interval */ +#endif + +#ifdef CONFIG_USE_HID_CONFIG +extern struct usbd_endpoint hid_custom_in_ep; +extern struct usbd_endpoint hid_custom_out_ep; + +extern const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE]; +#endif + +#endif //HSLINK_PRO_HID_COMM_H diff --git a/projects/HSLink-Pro/src/main.c b/projects/HSLink-Pro/src/main.c index 2a94d19..70d1eed 100644 --- a/projects/HSLink-Pro/src/main.c +++ b/projects/HSLink-Pro/src/main.c @@ -7,6 +7,7 @@ #include "WS2812.h" #include "projects/HSLink-Pro/common/HSLink_Pro_expansion.h" #include "usb2uart.h" +#include "usb_configuration.h" char serial_number[32]; @@ -56,7 +57,8 @@ int main(void) intc_set_irq_priority(CONFIG_HPM_USBD_IRQn, 5); uartx_preinit(); - chry_dap_init(0, CONFIG_HPM_USBD_BASE); + USB_Configuration(); + while (1) { chry_dap_handle(); chry_dap_usb2uart_handle(); diff --git a/projects/HSLink-Pro/src/usb_config.h b/projects/HSLink-Pro/src/usb_config.h index 919f709..b43ea5a 100644 --- a/projects/HSLink-Pro/src/usb_config.h +++ b/projects/HSLink-Pro/src/usb_config.h @@ -19,7 +19,7 @@ #define usb_free(ptr) free(ptr) #ifndef CONFIG_USB_DBG_LEVEL -#define CONFIG_USB_DBG_LEVEL USB_DBG_INFO +#define CONFIG_USB_DBG_LEVEL USB_DBG_LOG #endif diff --git a/projects/HSLink-Pro/src/usb_configuration.c b/projects/HSLink-Pro/src/usb_configuration.c new file mode 100644 index 0000000..9355064 --- /dev/null +++ b/projects/HSLink-Pro/src/usb_configuration.c @@ -0,0 +1,88 @@ +#include "usb_configuration.h" +#include "chry_ringbuffer.h" +#include "usbd_core.h" +#include "dap_main.h" +#include "hid_comm.h" + +#define USB_CONFIG_SIZE (9 + CMSIS_DAP_INTERFACE_SIZE + CDC_ACM_DESCRIPTOR_LEN + CONFIG_MSC_DESCRIPTOR_LEN + CONFIG_HID_DESCRIPTOR_LEN) +#define INTF_NUM (2 + 1 + CONFIG_MSC_INTF_NUM + CONFIG_HID_INTF_NUM) + +static const uint8_t config_descriptor[] = { + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + /* Interface 0 */ + USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xFF, 0x00, 0x00, 0x02), + /* Endpoint OUT 2 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_OUT_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + /* Endpoint IN 1 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_IN_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + CDC_ACM_DESCRIPTOR_INIT(0x01, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, DAP_PACKET_SIZE, 0x00), +#ifdef CONFIG_CHERRYDAP_USE_MSC + MSC_DESCRIPTOR_INIT(MSC_INTF_NUM, MSC_OUT_EP, MSC_IN_EP, DAP_PACKET_SIZE, 0x00), +#endif +#ifdef CONFIG_USE_HID_CONFIG + HID_DESC() +#endif +}; + +static const uint8_t other_speed_config_descriptor[] = { + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, INTF_NUM, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + /* Interface 0 */ + USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0xFF, 0x00, 0x00, 0x02), + /* Endpoint OUT 2 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_OUT_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + /* Endpoint IN 1 */ + USB_ENDPOINT_DESCRIPTOR_INIT(DAP_IN_EP, USB_ENDPOINT_TYPE_BULK, DAP_PACKET_SIZE, 0x00), + CDC_ACM_DESCRIPTOR_INIT(0x01, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, DAP_PACKET_SIZE, 0x00), +#ifdef CONFIG_CHERRYDAP_USE_MSC + MSC_DESCRIPTOR_INIT(MSC_INTF_NUM, MSC_OUT_EP, MSC_IN_EP, DAP_PACKET_SIZE, 0x00), +#endif +#ifdef CONFIG_USE_HID_CONFIG + HID_DESC() +#endif +}; + +const uint8_t *config_descriptor_callback(uint8_t speed) +{ + (void) speed; + return config_descriptor; +} + +const uint8_t *other_speed_config_descriptor_callback(uint8_t speed) +{ + (void) speed; + return other_speed_config_descriptor; +} + +void USB_Configuration(void) +{ + chry_ringbuffer_init(&g_uartrx, uartrx_ringbuffer, CONFIG_UARTRX_RINGBUF_SIZE); + chry_ringbuffer_init(&g_usbrx, usbrx_ringbuffer, CONFIG_USBRX_RINGBUF_SIZE); + + DAP_Setup(); + + usbd_desc_register(0, &cmsisdap_descriptor); + + /*!< winusb */ + usbd_add_interface(0, &dap_intf); + usbd_add_endpoint(0, &dap_out_ep); + usbd_add_endpoint(0, &dap_in_ep); + + /*!< cdc acm */ + usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf1)); + usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf2)); + usbd_add_endpoint(0, &cdc_out_ep); + usbd_add_endpoint(0, &cdc_in_ep); + +#ifdef CONFIG_USE_HID_CONFIG + /*!< hid */ + usbd_add_interface(0, usbd_hid_init_intf(0, &hid_intf, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE)); + usbd_add_endpoint(0, &hid_custom_in_ep); + usbd_add_endpoint(0, &hid_custom_out_ep); +#endif + +#ifdef CONFIG_CHERRYDAP_USE_MSC + usbd_add_interface(0, usbd_msc_init_intf(0, &intf3, MSC_OUT_EP, MSC_IN_EP)); +#endif + extern void usbd_event_handler(uint8_t busid, uint8_t event); + usbd_initialize(0, HPM_USB0_BASE, usbd_event_handler); +} diff --git a/projects/HSLink-Pro/src/usb_configuration.h b/projects/HSLink-Pro/src/usb_configuration.h new file mode 100644 index 0000000..86338ad --- /dev/null +++ b/projects/HSLink-Pro/src/usb_configuration.h @@ -0,0 +1,9 @@ +#ifndef HSLINK_PRO_USB_CONFIGURATION_H +#define HSLINK_PRO_USB_CONFIGURATION_H + +/** + * @brief 本工程的USB配置函数,用于添加其它配置 + */ +void USB_Configuration(void); + +#endif //HSLINK_PRO_USB_CONFIGURATION_H