• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "usbd_core.h"
2 #include "usbd_cdc.h"
3 
4 /*!< endpoint address */
5 #define CDC_IN_EP  0x81
6 #define CDC_OUT_EP 0x02
7 #define CDC_INT_EP 0x83
8 
9 #define USBD_VID           0xFFFF
10 #define USBD_PID           0xFFFF
11 #define USBD_MAX_POWER     100
12 #define USBD_LANGID_STRING 1033
13 
14 /*!< config descriptor size */
15 #define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN)
16 
17 #ifdef CONFIG_USB_HS
18 #define CDC_MAX_MPS 512
19 #else
20 #define CDC_MAX_MPS 64
21 #endif
22 
23 /*!< global descriptor */
24 static const uint8_t cdc_descriptor[] = {
25     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01),
26     USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
27     CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02),
28     ///////////////////////////////////////
29     /// string0 descriptor
30     ///////////////////////////////////////
31     USB_LANGID_INIT(USBD_LANGID_STRING),
32     ///////////////////////////////////////
33     /// string1 descriptor
34     ///////////////////////////////////////
35     0x14,                       /* bLength */
36     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
37     'C', 0x00,                  /* wcChar0 */
38     'h', 0x00,                  /* wcChar1 */
39     'e', 0x00,                  /* wcChar2 */
40     'r', 0x00,                  /* wcChar3 */
41     'r', 0x00,                  /* wcChar4 */
42     'y', 0x00,                  /* wcChar5 */
43     'U', 0x00,                  /* wcChar6 */
44     'S', 0x00,                  /* wcChar7 */
45     'B', 0x00,                  /* wcChar8 */
46     ///////////////////////////////////////
47     /// string2 descriptor
48     ///////////////////////////////////////
49     0x26,                       /* bLength */
50     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
51     'C', 0x00,                  /* wcChar0 */
52     'h', 0x00,                  /* wcChar1 */
53     'e', 0x00,                  /* wcChar2 */
54     'r', 0x00,                  /* wcChar3 */
55     'r', 0x00,                  /* wcChar4 */
56     'y', 0x00,                  /* wcChar5 */
57     'U', 0x00,                  /* wcChar6 */
58     'S', 0x00,                  /* wcChar7 */
59     'B', 0x00,                  /* wcChar8 */
60     ' ', 0x00,                  /* wcChar9 */
61     'C', 0x00,                  /* wcChar10 */
62     'D', 0x00,                  /* wcChar11 */
63     'C', 0x00,                  /* wcChar12 */
64     ' ', 0x00,                  /* wcChar13 */
65     'D', 0x00,                  /* wcChar14 */
66     'E', 0x00,                  /* wcChar15 */
67     'M', 0x00,                  /* wcChar16 */
68     'O', 0x00,                  /* wcChar17 */
69     ///////////////////////////////////////
70     /// string3 descriptor
71     ///////////////////////////////////////
72     0x16,                       /* bLength */
73     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
74     '2', 0x00,                  /* wcChar0 */
75     '0', 0x00,                  /* wcChar1 */
76     '2', 0x00,                  /* wcChar2 */
77     '2', 0x00,                  /* wcChar3 */
78     '1', 0x00,                  /* wcChar4 */
79     '2', 0x00,                  /* wcChar5 */
80     '3', 0x00,                  /* wcChar6 */
81     '4', 0x00,                  /* wcChar7 */
82     '5', 0x00,                  /* wcChar8 */
83     '6', 0x00,                  /* wcChar9 */
84 #ifdef CONFIG_USB_HS
85     ///////////////////////////////////////
86     /// device qualifier descriptor
87     ///////////////////////////////////////
88     0x0a,
89     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
90     0x00,
91     0x02,
92     0x02,
93     0x02,
94     0x01,
95     0x40,
96     0x01,
97     0x00,
98 #endif
99     0x00
100 };
101 
102 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048];
103 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048];
104 
105 volatile bool ep_tx_busy_flag = false;
106 
107 #ifdef CONFIG_USB_HS
108 #define CDC_MAX_MPS 512
109 #else
110 #define CDC_MAX_MPS 64
111 #endif
112 
usbd_event_handler(uint8_t event)113 void usbd_event_handler(uint8_t event)
114 {
115     switch (event) {
116         case USBD_EVENT_RESET:
117             break;
118         case USBD_EVENT_CONNECTED:
119             break;
120         case USBD_EVENT_DISCONNECTED:
121             break;
122         case USBD_EVENT_RESUME:
123             break;
124         case USBD_EVENT_SUSPEND:
125             break;
126         case USBD_EVENT_CONFIGURED:
127             /* setup first out ep read transfer */
128             usbd_ep_start_read(CDC_OUT_EP, read_buffer, 2048);
129             break;
130         case USBD_EVENT_SET_REMOTE_WAKEUP:
131             break;
132         case USBD_EVENT_CLR_REMOTE_WAKEUP:
133             break;
134 
135         default:
136             break;
137     }
138 }
139 
usbd_cdc_acm_bulk_out(uint8_t ep,uint32_t nbytes)140 void usbd_cdc_acm_bulk_out(uint8_t ep, uint32_t nbytes)
141 {
142     USB_LOG_RAW("actual out len:%d\r\n", nbytes);
143     // for (int i = 0; i < 100; i++) {
144     //     printf("%02x ", read_buffer[i]);
145     // }
146     // printf("\r\n");
147     /* setup next out ep read transfer */
148     usbd_ep_start_read(CDC_OUT_EP, read_buffer, 2048);
149 }
150 
usbd_cdc_acm_bulk_in(uint8_t ep,uint32_t nbytes)151 void usbd_cdc_acm_bulk_in(uint8_t ep, uint32_t nbytes)
152 {
153     USB_LOG_RAW("actual in len:%d\r\n", nbytes);
154 
155     if ((nbytes % CDC_MAX_MPS) == 0 && nbytes) {
156         /* send zlp */
157         usbd_ep_start_write(CDC_IN_EP, NULL, 0);
158     } else {
159         ep_tx_busy_flag = false;
160     }
161 }
162 
163 /*!< endpoint call back */
164 struct usbd_endpoint cdc_out_ep = {
165     .ep_addr = CDC_OUT_EP,
166     .ep_cb = usbd_cdc_acm_bulk_out
167 };
168 
169 struct usbd_endpoint cdc_in_ep = {
170     .ep_addr = CDC_IN_EP,
171     .ep_cb = usbd_cdc_acm_bulk_in
172 };
173 
174 static struct usbd_interface intf0;
175 static struct usbd_interface intf1;
176 
cdc_acm_init(void)177 void cdc_acm_init(void)
178 {
179     const uint8_t data[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
180 
181     memcpy(&write_buffer[0], data, 10);
182     memset(&write_buffer[10], 'a', 2038);
183 
184     usbd_desc_register(cdc_descriptor);
185     usbd_add_interface(usbd_cdc_acm_init_intf(&intf0));
186     usbd_add_interface(usbd_cdc_acm_init_intf(&intf1));
187     usbd_add_endpoint(&cdc_out_ep);
188     usbd_add_endpoint(&cdc_in_ep);
189     usbd_initialize();
190 }
191 
192 volatile uint8_t dtr_enable = 0;
193 
usbd_cdc_acm_set_dtr(uint8_t intf,bool dtr)194 void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr)
195 {
196     if (dtr) {
197         dtr_enable = 1;
198     } else {
199         dtr_enable = 0;
200     }
201 }
202 
cdc_acm_data_send_with_dtr_test(void)203 void cdc_acm_data_send_with_dtr_test(void)
204 {
205     if (dtr_enable) {
206         ep_tx_busy_flag = true;
207         usbd_ep_start_write(CDC_IN_EP, write_buffer, 2048);
208         while (ep_tx_busy_flag) {
209         }
210     }
211 }