33
33
#include " Adafruit_USBD_CDC.h"
34
34
#include " Adafruit_USBD_Device.h"
35
35
36
- // TODO Multiple instances supports
37
- // static uint8_t _itf_count;
38
- // static Adafruit_USBD_CDC* _itf_arr[]
39
-
36
+ // Starting endpoints; adjusted elsewhere as needed
40
37
#define EPOUT 0x00
41
38
#define EPIN 0x80
42
39
40
+ uint8_t _cdc_instance_count = 0 ;
41
+
43
42
Adafruit_USBD_CDC Serial;
44
43
45
44
Adafruit_USBD_CDC::Adafruit_USBD_CDC (void ) {
@@ -70,10 +69,15 @@ void Adafruit_USBD_CDC::begin(uint32_t baud) {
70
69
return ;
71
70
}
72
71
73
- _begun = true ;
72
+ // too many instances
73
+ if (!(_cdc_instance_count < CFG_TUD_CDC)) {
74
+ return ;
75
+ }
74
76
75
- Serial.setStringDescriptor (" TinyUSB Serial" );
76
- USBDevice.addInterface (Serial);
77
+ _begun = true ;
78
+ _itf = _cdc_instance_count++;
79
+ this ->setStringDescriptor (" TinyUSB Serial" );
80
+ USBDevice.addInterface (*this );
77
81
}
78
82
79
83
void Adafruit_USBD_CDC::begin (uint32_t baud, uint8_t config) {
@@ -84,40 +88,67 @@ void Adafruit_USBD_CDC::begin(uint32_t baud, uint8_t config) {
84
88
void Adafruit_USBD_CDC::end (void ) {
85
89
// Resset configuration descriptor without Serial as CDC
86
90
USBDevice.clearConfiguration ();
91
+ _cdc_instance_count = 0 ;
87
92
}
88
93
89
94
uint32_t Adafruit_USBD_CDC::baud (void ) {
95
+ if (!_begun) {
96
+ return 0 ;
97
+ }
98
+
90
99
cdc_line_coding_t coding;
91
- tud_cdc_get_line_coding ( &coding);
100
+ tud_cdc_n_get_line_coding (_itf, &coding);
92
101
93
102
return coding.bit_rate ;
94
103
}
95
104
96
105
uint8_t Adafruit_USBD_CDC::stopbits (void ) {
106
+ if (!_begun) {
107
+ return 0 ;
108
+ }
109
+
97
110
cdc_line_coding_t coding;
98
- tud_cdc_get_line_coding ( &coding);
111
+ tud_cdc_n_get_line_coding (_itf, &coding);
99
112
100
113
return coding.stop_bits ;
101
114
}
102
115
103
116
uint8_t Adafruit_USBD_CDC::paritytype (void ) {
117
+ if (!_begun) {
118
+ return 0 ;
119
+ }
120
+
104
121
cdc_line_coding_t coding;
105
- tud_cdc_get_line_coding ( &coding);
122
+ tud_cdc_n_get_line_coding (_itf, &coding);
106
123
107
124
return coding.parity ;
108
125
}
109
126
110
127
uint8_t Adafruit_USBD_CDC::numbits (void ) {
128
+ if (!_begun) {
129
+ return 0 ;
130
+ }
131
+
111
132
cdc_line_coding_t coding;
112
- tud_cdc_get_line_coding ( &coding);
133
+ tud_cdc_n_get_line_coding (_itf, &coding);
113
134
114
135
return coding.data_bits ;
115
136
}
116
137
117
- int Adafruit_USBD_CDC::dtr (void ) { return tud_cdc_connected (); }
138
+ int Adafruit_USBD_CDC::dtr (void ) {
139
+ if (!_begun) {
140
+ return 0 ;
141
+ }
142
+
143
+ return tud_cdc_n_connected (_itf);
144
+ }
118
145
119
146
Adafruit_USBD_CDC::operator bool () {
120
- bool ret = tud_cdc_connected ();
147
+ if (!_begun) {
148
+ return false ;
149
+ }
150
+
151
+ bool ret = tud_cdc_n_connected (_itf);
121
152
122
153
// Add an yield to run usb background in case sketch block wait as follows
123
154
// while( !Serial ) {}
@@ -128,7 +159,11 @@ Adafruit_USBD_CDC::operator bool() {
128
159
}
129
160
130
161
int Adafruit_USBD_CDC::available (void ) {
131
- uint32_t count = tud_cdc_available ();
162
+ if (!_begun) {
163
+ return 0 ;
164
+ }
165
+
166
+ uint32_t count = tud_cdc_n_available (_itf);
132
167
133
168
// Add an yield to run usb background in case sketch block wait as follows
134
169
// while( !Serial.available() ) {}
@@ -140,20 +175,39 @@ int Adafruit_USBD_CDC::available(void) {
140
175
}
141
176
142
177
int Adafruit_USBD_CDC::peek (void ) {
178
+ if (!_begun) {
179
+ return -1 ;
180
+ }
181
+
143
182
uint8_t ch;
144
- return tud_cdc_peek (&ch) ? (int )ch : -1 ;
183
+ return tud_cdc_n_peek (_itf, &ch) ? (int )ch : -1 ;
184
+ }
185
+
186
+ int Adafruit_USBD_CDC::read (void ) {
187
+ if (!_begun) {
188
+ return -1 ;
189
+ }
190
+ return (int )tud_cdc_n_read_char (_itf);
145
191
}
146
192
147
- int Adafruit_USBD_CDC::read (void ) { return (int )tud_cdc_read_char (); }
193
+ void Adafruit_USBD_CDC::flush (void ) {
194
+ if (!_begun) {
195
+ return ;
196
+ }
148
197
149
- void Adafruit_USBD_CDC::flush (void ) { tud_cdc_write_flush (); }
198
+ tud_cdc_n_write_flush (_itf);
199
+ }
150
200
151
201
size_t Adafruit_USBD_CDC::write (uint8_t ch) { return write (&ch, 1 ); }
152
202
153
203
size_t Adafruit_USBD_CDC::write (const uint8_t *buffer, size_t size) {
204
+ if (!_begun) {
205
+ return 0 ;
206
+ }
207
+
154
208
size_t remain = size;
155
- while (remain && tud_cdc_connected ( )) {
156
- size_t wrcount = tud_cdc_write ( buffer, remain);
209
+ while (remain && tud_cdc_n_connected (_itf )) {
210
+ size_t wrcount = tud_cdc_n_write (_itf, buffer, remain);
157
211
remain -= wrcount;
158
212
buffer += wrcount;
159
213
@@ -167,20 +221,23 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
167
221
}
168
222
169
223
int Adafruit_USBD_CDC::availableForWrite (void ) {
170
- return tud_cdc_write_available ();
224
+ if (!_begun) {
225
+ return 0 ;
226
+ }
227
+ return tud_cdc_n_write_available (_itf);
171
228
}
172
229
173
230
extern " C" {
174
231
175
232
// Invoked when cdc when line state changed e.g connected/disconnected
176
233
// Use to reset to DFU when disconnect with 1200 bps
177
- void tud_cdc_line_state_cb (uint8_t itf , bool dtr, bool rts) {
234
+ void tud_cdc_line_state_cb (uint8_t instance , bool dtr, bool rts) {
178
235
(void )rts;
179
236
180
237
// DTR = false is counted as disconnected
181
238
if (!dtr) {
182
239
// touch1200 only with first CDC instance (Serial)
183
- if (itf == 0 ) {
240
+ if (instance == 0 ) {
184
241
cdc_line_coding_t coding;
185
242
tud_cdc_get_line_coding (&coding);
186
243
0 commit comments