1 /*
2 * Copyright (c) 2023 ~ 2025, sakumisu
3 * Copyright (c) 2023 ~ 2025, HalfSweet
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7 #ifndef DAP_MAIN_H
8 #define DAP_MAIN_H
9
10 #include "usbd_core.h"
11 #include "usbd_cdc.h"
12 #include "usbd_msc.h"
13 #include "chry_ringbuffer.h"
14 #include "DAP_config.h"
15 #include "DAP.h"
16
17 #define DAP_IN_EP 0x81
18 #define DAP_OUT_EP 0x02
19
20 #define CDC_IN_EP 0x83
21 #define CDC_OUT_EP 0x04
22 #define CDC_INT_EP 0x85
23
24 #define MSC_IN_EP 0x86
25 #define MSC_OUT_EP 0x07
26
27 #define USBD_VID 0x0D28
28 #define USBD_PID 0x0204
29 #define USBD_MAX_POWER 500
30 #define USBD_LANGID_STRING 1033
31
32 #define CMSIS_DAP_INTERFACE_SIZE (9 + 7 + 7)
33
34 #ifdef CONFIG_CHERRYDAP_USE_MSC
35 #define CONFIG_MSC_DESCRIPTOR_LEN CDC_ACM_DESCRIPTOR_LEN
36 #define CONFIG_MSC_INTF_NUM 1
37 #define MSC_INTF_NUM (0x02 + 1)
38 #else
39 #define CONFIG_MSC_DESCRIPTOR_LEN 0
40 #define CONFIG_MSC_INTF_NUM 0
41 #define MSC_INTF_NUM (0x02)
42 #endif
43
44 #ifdef CONFIG_USB_HS
45 #if DAP_PACKET_SIZE != 512
46 #error "DAP_PACKET_SIZE must be 512 in hs"
47 #endif
48 #else
49 #if DAP_PACKET_SIZE != 64
50 #error "DAP_PACKET_SIZE must be 64 in fs"
51 #endif
52 #endif
53
54 #define USBD_WINUSB_VENDOR_CODE 0x20
55
56 #define USBD_WEBUSB_ENABLE 0
57 #define USBD_BULK_ENABLE 1
58 #define USBD_WINUSB_ENABLE 1
59
60 /* WinUSB Microsoft OS 2.0 descriptor sizes */
61 #define WINUSB_DESCRIPTOR_SET_HEADER_SIZE 10
62 #define WINUSB_FUNCTION_SUBSET_HEADER_SIZE 8
63 #define WINUSB_FEATURE_COMPATIBLE_ID_SIZE 20
64
65 #define FUNCTION_SUBSET_LEN 160
66 #define DEVICE_INTERFACE_GUIDS_FEATURE_LEN 132
67
68 #define USBD_WINUSB_DESC_SET_LEN (WINUSB_DESCRIPTOR_SET_HEADER_SIZE + USBD_WEBUSB_ENABLE * FUNCTION_SUBSET_LEN + USBD_BULK_ENABLE * FUNCTION_SUBSET_LEN)
69
70 #define USBD_NUM_DEV_CAPABILITIES (USBD_WEBUSB_ENABLE + USBD_WINUSB_ENABLE)
71
72 #define USBD_WEBUSB_DESC_LEN 24
73 #define USBD_WINUSB_DESC_LEN 28
74
75 #define USBD_BOS_WTOTALLENGTH (0x05 + \
76 USBD_WEBUSB_DESC_LEN * USBD_WEBUSB_ENABLE + \
77 USBD_WINUSB_DESC_LEN * USBD_WINUSB_ENABLE)
78
79 #define CONFIG_UARTRX_RINGBUF_SIZE (8 * 1024)
80 #define CONFIG_USBRX_RINGBUF_SIZE (8 * 1024)
81
82 #ifdef __cplusplus
83 extern "C"
84 {
85 #endif
86
87 extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t uartrx_ringbuffer[CONFIG_UARTRX_RINGBUF_SIZE];
88 extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usbrx_ringbuffer[CONFIG_USBRX_RINGBUF_SIZE];
89 extern USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t usb_tmpbuffer[DAP_PACKET_SIZE];
90
91 extern const struct usb_descriptor cmsisdap_descriptor;
92 extern __ALIGN_BEGIN const uint8_t USBD_WinUSBDescriptorSetDescriptor[];
93 extern __ALIGN_BEGIN const uint8_t USBD_BinaryObjectStoreDescriptor[];
94 extern char *string_descriptors[];
95
96 extern struct usbd_interface dap_intf;
97 extern struct usbd_interface intf1;
98 extern struct usbd_interface intf2;
99 extern struct usbd_interface intf3;
100 extern struct usbd_interface hid_intf;
101
102 extern struct usbd_endpoint dap_out_ep;
103 extern struct usbd_endpoint dap_in_ep;
104 extern struct usbd_endpoint cdc_out_ep;
105 extern struct usbd_endpoint cdc_in_ep;
106
107 extern chry_ringbuffer_t g_uartrx;
108 extern chry_ringbuffer_t g_usbrx;
109
chry_dap_init(uint8_t busid,uint32_t reg_base)110 __STATIC_INLINE void chry_dap_init(uint8_t busid, uint32_t reg_base)
111 {
112 chry_ringbuffer_init(&g_uartrx, uartrx_ringbuffer, CONFIG_UARTRX_RINGBUF_SIZE);
113 chry_ringbuffer_init(&g_usbrx, usbrx_ringbuffer, CONFIG_USBRX_RINGBUF_SIZE);
114
115 DAP_Setup();
116
117 usbd_desc_register(0, &cmsisdap_descriptor);
118
119 /*!< winusb */
120 usbd_add_interface(0, &dap_intf);
121 usbd_add_endpoint(0, &dap_out_ep);
122 usbd_add_endpoint(0, &dap_in_ep);
123
124 /*!< cdc acm */
125 usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf1));
126 usbd_add_interface(0, usbd_cdc_acm_init_intf(0, &intf2));
127 usbd_add_endpoint(0, &cdc_out_ep);
128 usbd_add_endpoint(0, &cdc_in_ep);
129
130 #ifdef CONFIG_CHERRYDAP_USE_MSC
131 usbd_add_interface(0, usbd_msc_init_intf(0, &intf3, MSC_OUT_EP, MSC_IN_EP));
132 #endif
133 extern void usbd_event_handler(uint8_t busid, uint8_t event);
134 usbd_initialize(busid, reg_base, usbd_event_handler);
135 }
136
137 void chry_dap_handle(void);
138
139 void chry_dap_usb2uart_handle(void);
140
141 void chry_dap_usb2uart_uart_config_callback(struct cdc_line_coding *line_coding);
142
143 void chry_dap_usb2uart_uart_send_bydma(uint8_t *data, uint16_t len);
144
145 void chry_dap_usb2uart_uart_send_complete(uint32_t size);
146
147 #ifdef __cplusplus
148 }
149 #endif
150
151 #endif