Skip to content

Commit

Permalink
refactor: 优化工程结构
Browse files Browse the repository at this point in the history
  • Loading branch information
HalfSweet committed Sep 27, 2024
1 parent 0b28efa commit 7564799
Show file tree
Hide file tree
Showing 9 changed files with 572 additions and 460 deletions.
621 changes: 166 additions & 455 deletions dap_main.c

Large diffs are not rendered by default.

125 changes: 124 additions & 1 deletion dap_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,139 @@
#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_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(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
11 changes: 9 additions & 2 deletions projects/HSLink-Pro/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -100,8 +99,16 @@ sdk_app_src(USB2UART/usb2uart.c)
sdk_app_inc(USB2UART)

sdk_compile_definitions(-DPRODUCT_STRING="HSLink Pro")
#sdk_compile_definitions(-DCONFIG_USE_HID_CONFIG=1)
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
Expand Down
77 changes: 77 additions & 0 deletions projects/HSLink-Pro/src/HID_COMM/hid_comm.c
Original file line number Diff line number Diff line change
@@ -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

95 changes: 95 additions & 0 deletions projects/HSLink-Pro/src/HID_COMM/hid_comm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// Created by HalfSweet on 24-9-25.
//

#ifndef HSLINK_PRO_HID_COMM_H
#define HSLINK_PRO_HID_COMM_H

#include <stdint.h>
#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
4 changes: 3 additions & 1 deletion projects/HSLink-Pro/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion projects/HSLink-Pro/src/usb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading

0 comments on commit 7564799

Please sign in to comment.