forked from arduino/ArduinoCore-mbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSBCDC.cpp
619 lines (506 loc) · 16.7 KB
/
USBCDC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* Copyright (c) 2018-2019, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Arduino.h"
#include "PeripheralPins.h"
#if DEVICE_USBDEVICE && defined(SERIAL_CDC)
#include "stdint.h"
#include "USBCDC.h"
#include "EndpointResolver.h"
#include "AsyncOp.h"
#include "usb_phy_api.h"
using namespace arduino;
static const uint8_t cdc_line_coding_default[7] = {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
#define DEFAULT_CONFIGURATION (1)
#define CDC_SET_LINE_CODING 0x20
#define CDC_GET_LINE_CODING 0x21
#define CDC_SET_CONTROL_LINE_STATE 0x22
// Control Line State bits
#define CLS_DTR (1 << 0)
#define CLS_RTS (1 << 1)
class USBCDC::AsyncWrite: public AsyncOp {
public:
AsyncWrite(USBCDC *serial, uint8_t *buf, uint32_t size):
serial(serial), tx_buf(buf), tx_size(size), result(false)
{
}
virtual ~AsyncWrite()
{
}
virtual bool process()
{
if (!serial->_terminal_connected) {
result = false;
return true;
}
uint32_t actual_size = 0;
serial->send_nb(tx_buf, tx_size, &actual_size, true);
tx_size -= actual_size;
tx_buf += actual_size;
if (tx_size == 0) {
result = true;
return true;
}
// Start transfer if it hasn't been
serial->_send_isr_start();
return false;
}
USBCDC *serial;
uint8_t *tx_buf;
uint32_t tx_size;
bool result;
};
class USBCDC::AsyncRead: public AsyncOp {
public:
AsyncRead(USBCDC *serial, uint8_t *buf, uint32_t size, uint32_t *size_read, bool read_all)
: serial(serial), rx_buf(buf), rx_size(size), rx_actual(size_read), all(read_all), result(false)
{
}
virtual ~AsyncRead()
{
}
virtual bool process()
{
if (!serial->_terminal_connected) {
result = false;
return true;
}
uint32_t actual_size = 0;
serial->receive_nb(rx_buf, rx_size, &actual_size);
rx_buf += actual_size;
*rx_actual += actual_size;
rx_size -= actual_size;
if ((!all && *rx_actual > 0) || (rx_size == 0)) {
// Wake thread if request is done
result = true;
return true;
}
serial->_receive_isr_start();
return false;
}
USBCDC *serial;
uint8_t *rx_buf;
uint32_t rx_size;
uint32_t *rx_actual;
bool all;
bool result;
};
class USBCDC::AsyncWait: public AsyncOp {
public:
AsyncWait(USBCDC *serial)
: serial(serial)
{
}
virtual ~AsyncWait()
{
}
virtual bool process()
{
if (serial->_terminal_connected) {
return true;
}
return false;
}
USBCDC *serial;
};
USBCDC::USBCDC(bool connect_blocking, const char* name, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
: internal::PluggableUSBModule(2), extraDescriptor(name)
{
PluggableUSBD().plug(this);
}
USBCDC::USBCDC(USBPhy *phy, const char* name, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
: internal::PluggableUSBModule(2), extraDescriptor(name)
{
PluggableUSBD().plug(this);
}
USBCDC::~USBCDC()
{
PluggableUSBD().deinit();
}
void USBCDC::init(EndpointResolver& resolver)
{
memcpy(_cdc_line_coding, cdc_line_coding_default, sizeof(_cdc_line_coding));
_bulk_in = resolver.endpoint_in(USB_EP_TYPE_BULK, CDC_MAX_PACKET_SIZE);
_bulk_out = resolver.endpoint_out(USB_EP_TYPE_BULK, CDC_MAX_PACKET_SIZE);
_int_in = resolver.endpoint_in(USB_EP_TYPE_INT, CDC_MAX_PACKET_SIZE);
MBED_ASSERT(resolver.valid());
_terminal_connected = false;
_tx_in_progress = false;
_tx_buf = _tx_buffer;
_tx_size = 0;
_rx_in_progress = false;
_rx_buf = _rx_buffer;
_rx_size = 0;
}
void USBCDC::callback_reset()
{
assert_locked();
/* Called in ISR context */
_change_terminal_connected(false);
};
void USBCDC::callback_state_change(USBDevice::DeviceState new_state)
{
assert_locked();
/* Called in ISR context */
if (new_state != USBDevice::Configured) {
_change_terminal_connected(false);
}
}
uint32_t USBCDC::callback_request(const USBDevice::setup_packet_t *setup, USBDevice::RequestResult *result, uint8_t** data)
{
assert_locked();
/* Called in ISR context */
*result = USBDevice::PassThrough;
uint32_t size = 0;
if (setup->wIndex != pluggedInterface) {
return size;
}
/* Only process class-specific requests */
if (setup->bmRequestType.Type == CLASS_TYPE) {
switch (setup->bRequest) {
case CDC_GET_LINE_CODING:
*result = USBDevice::Send;
*data = _cdc_line_coding;
size = 7;
break;
case CDC_SET_LINE_CODING:
*result = USBDevice::Receive;
*data = _cdc_new_line_coding;
size = 7;
break;
case CDC_SET_CONTROL_LINE_STATE:
if (setup->wValue & CLS_DTR) {
_change_terminal_connected(true);
} else {
_change_terminal_connected(false);
}
*result = USBDevice::Success;
break;
default:
*result = USBDevice::Failure;
break;
}
}
return size;
}
bool USBCDC::callback_request_xfer_done(const USBDevice::setup_packet_t *setup, bool aborted)
{
assert_locked();
/* Called in ISR context */
bool success = false;
if (setup->wIndex != pluggedInterface || aborted) {
return success;
}
/* Process class-specific requests */
if (setup->bmRequestType.Type == CLASS_TYPE) {
if ((setup->bRequest == CDC_SET_LINE_CODING) && (setup->wLength == 7)) {
if (memcmp(_cdc_line_coding, _cdc_new_line_coding, 7)) {
memcpy(_cdc_line_coding, _cdc_new_line_coding, 7);
const uint8_t *buf = _cdc_line_coding;
int baud = buf[0] + (buf[1] << 8)
+ (buf[2] << 16) + (buf[3] << 24);
int stop = buf[4];
int bits = buf[6];
int parity = buf[5];
line_coding_changed(baud, bits, parity, stop);
}
success = true;
}
if (setup->bRequest == CDC_GET_LINE_CODING) {
success = true;
}
}
return success;
}
bool USBCDC::callback_set_configuration(uint8_t configuration)
{
assert_locked();
/* Called in ISR context */
bool ret = false;
// Configure endpoints > 0
PluggableUSBD().endpoint_add(_int_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_INT);
PluggableUSBD().endpoint_add(_bulk_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, ::mbed::callback(this, &USBCDC::_send_isr));
PluggableUSBD().endpoint_add(_bulk_out, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, ::mbed::callback(this, &USBCDC::_receive_isr));
PluggableUSBD().read_start(_bulk_out, _rx_buf, sizeof(_rx_buffer));
_rx_in_progress = true;
ret = true;
return ret;
}
void USBCDC::callback_set_interface(uint16_t interface, uint8_t alternate)
{
assert_locked();
}
void USBCDC::_change_terminal_connected(bool connected)
{
assert_locked();
_terminal_connected = connected;
if (!_terminal_connected) {
// Abort TX
if (_tx_in_progress) {
PluggableUSBD().endpoint_abort(_bulk_in);
_tx_in_progress = false;
}
_tx_buf = _tx_buffer;
_tx_size = 0;
_tx_list.process();
MBED_ASSERT(_tx_list.empty());
// Abort RX
if (_rx_in_progress) {
PluggableUSBD().endpoint_abort(_bulk_in);
_rx_in_progress = false;
}
_rx_buf = _rx_buffer;
_rx_size = 0;
_rx_list.process();
MBED_ASSERT(_rx_list.empty());
}
_connected_list.process();
}
bool USBCDC::ready()
{
lock();
bool ready = _terminal_connected;
unlock();
return ready;
}
void USBCDC::wait_ready()
{
lock();
AsyncWait wait_op(this);
_connected_list.add(&wait_op);
unlock();
wait_op.wait(NULL);
}
bool USBCDC::send(uint8_t *buffer, uint32_t size)
{
lock();
AsyncWrite write_op(this, buffer, size);
_tx_list.add(&write_op);
unlock();
write_op.wait(NULL);
return write_op.result;
}
void USBCDC::send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now)
{
lock();
*actual = 0;
if (_terminal_connected && !_tx_in_progress) {
uint32_t free = sizeof(_tx_buffer) - _tx_size;
uint32_t write_size = free > size ? size : free;
if (size > 0) {
memcpy(_tx_buf, buffer, write_size);
}
_tx_size += write_size;
*actual = write_size;
if (now) {
_send_isr_start();
}
}
unlock();
}
void USBCDC::_send_isr_start()
{
assert_locked();
if (!_tx_in_progress && _tx_size) {
if (PluggableUSBD().write_start(_bulk_in, _tx_buffer, _tx_size)) {
_tx_in_progress = true;
}
}
}
/*
* Called by when CDC data is sent
* Warning: Called in ISR
*/
void USBCDC::_send_isr()
{
assert_locked();
PluggableUSBD().write_finish(_bulk_in);
_tx_buf = _tx_buffer;
_tx_size = 0;
_tx_in_progress = false;
_tx_list.process();
if (!_tx_in_progress) {
data_tx();
}
}
bool USBCDC::receive(uint8_t *buffer, uint32_t size, uint32_t *size_read)
{
lock();
bool read_all = size_read == NULL;
uint32_t size_read_dummy;
uint32_t *size_read_ptr = read_all ? &size_read_dummy : size_read;
*size_read_ptr = 0;
AsyncRead read_op(this, buffer, size, size_read_ptr, read_all);
_rx_list.add(&read_op);
unlock();
read_op.wait(NULL);
return read_op.result;
}
void USBCDC::receive_nb(uint8_t *buffer, uint32_t size, uint32_t *size_read)
{
*size_read = 0;
if (_terminal_connected && !_rx_in_progress) {
// Copy data over
uint32_t copy_size = _rx_size > size ? size : _rx_size;
memcpy(buffer, _rx_buf, copy_size);
*size_read = copy_size;
_rx_buf += copy_size;
_rx_size -= copy_size;
if (_rx_size == 0) {
_receive_isr_start();
}
}
}
void USBCDC::_receive_isr_start()
{
if ((_rx_size == 0) && !_rx_in_progress) {
// Refill the buffer
PluggableUSBD().read_start(_bulk_out, _rx_buffer, sizeof(_rx_buffer));
_rx_in_progress = true;
}
}
/*
* Called by when CDC data is received
* Warning: Called in ISR
*/
void USBCDC::_receive_isr()
{
assert_locked();
MBED_ASSERT(_rx_size == 0);
_rx_buf = _rx_buffer;
_rx_size = PluggableUSBD().read_finish(_bulk_out);
_rx_in_progress = false;
_rx_list.process();
if (!_rx_in_progress) {
data_rx();
}
}
const uint8_t *USBCDC::string_iinterface_desc()
{
return (const uint8_t *)extraDescriptor;
}
uint8_t USBCDC::getProductVersion()
{
return 1;
}
const uint8_t *USBCDC::string_iproduct_desc()
{
static const uint8_t stringIproductDescriptor[] = {
0x16,
STRING_DESCRIPTOR,
'C', 0, 'D', 0, 'C', 0, ' ', 0, 'D', 0, 'E', 0, 'V', 0, 'I', 0, 'C', 0, 'E', 0
};
return stringIproductDescriptor;
}
#define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
const uint8_t *USBCDC::configuration_desc(uint8_t index)
{
uint8_t config_descriptor_temp[] = {
// configuration descriptor
9, // bLength
2, // bDescriptorType
LSB(CONFIG1_DESC_SIZE), // wTotalLength
MSB(CONFIG1_DESC_SIZE),
2, // bNumInterfaces
1, // bConfigurationValue
0, // iConfiguration
0x80, // bmAttributes
50, // bMaxPower
// IAD to associate the two CDC interfaces
0x08, // bLength
0x0b, // bDescriptorType
pluggedInterface, // bFirstInterface
0x02, // bInterfaceCount
0x02, // bFunctionClass
0x02, // bFunctionSubClass
0, // bFunctionProtocol
0, // iFunction
// interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
9, // bLength
4, // bDescriptorType
pluggedInterface, // bInterfaceNumber
0, // bAlternateSetting
1, // bNumEndpoints
0x02, // bInterfaceClass
0x02, // bInterfaceSubClass
0x01, // bInterfaceProtocol
(extraDescriptor != NULL) ? 0x5 : 0x0, // iInterface
// CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
5, // bFunctionLength
0x24, // bDescriptorType
0x00, // bDescriptorSubtype
0x10, 0x01, // bcdCDC
// Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
5, // bFunctionLength
0x24, // bDescriptorType
0x01, // bDescriptorSubtype
0x03, // bmCapabilities
1, // bDataInterface
// Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
4, // bFunctionLength
0x24, // bDescriptorType
0x02, // bDescriptorSubtype
0x06, // bmCapabilities
// Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
5, // bFunctionLength
0x24, // bDescriptorType
0x06, // bDescriptorSubtype
pluggedInterface, // bMasterInterface
uint8_t(pluggedInterface + 1), // bSlaveInterface0
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
ENDPOINT_DESCRIPTOR_LENGTH, // bLength
ENDPOINT_DESCRIPTOR, // bDescriptorType
_int_in, // bEndpointAddress
E_INTERRUPT, // bmAttributes (0x03=intr)
LSB(CDC_MAX_PACKET_SIZE), // wMaxPacketSize (LSB)
MSB(CDC_MAX_PACKET_SIZE), // wMaxPacketSize (MSB)
16, // bInterval
// interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
9, // bLength
4, // bDescriptorType
uint8_t(pluggedInterface + 1), // bInterfaceNumber
0, // bAlternateSetting
2, // bNumEndpoints
0x0A, // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, // bInterfaceProtocol
0, // iInterface
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
ENDPOINT_DESCRIPTOR_LENGTH, // bLength
ENDPOINT_DESCRIPTOR, // bDescriptorType
_bulk_in, // bEndpointAddress
E_BULK, // bmAttributes (0x02=bulk)
LSB(CDC_MAX_PACKET_SIZE), // wMaxPacketSize (LSB)
MSB(CDC_MAX_PACKET_SIZE), // wMaxPacketSize (MSB)
0, // bInterval
// endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
ENDPOINT_DESCRIPTOR_LENGTH, // bLength
ENDPOINT_DESCRIPTOR, // bDescriptorType
_bulk_out, // bEndpointAddress
E_BULK, // bmAttributes (0x02=bulk)
LSB(CDC_MAX_PACKET_SIZE), // wMaxPacketSize (LSB)
MSB(CDC_MAX_PACKET_SIZE), // wMaxPacketSize (MSB)
0 // bInterval
};
if (index == 0) {
MBED_ASSERT(sizeof(config_descriptor_temp) == sizeof(_config_descriptor));
memcpy(_config_descriptor, config_descriptor_temp, sizeof(_config_descriptor));
return _config_descriptor;
} else {
return NULL;
}
}
#endif