3333#include " Adafruit_USBD_CDC.h"
3434#include " Adafruit_USBD_Device.h"
3535
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
4037#define EPOUT 0x00
4138#define EPIN 0x80
4239
40+ uint8_t _cdc_instance_count = 0 ;
41+
4342Adafruit_USBD_CDC Serial;
4443
4544Adafruit_USBD_CDC::Adafruit_USBD_CDC (void ) {
@@ -70,10 +69,15 @@ void Adafruit_USBD_CDC::begin(uint32_t baud) {
7069 return ;
7170 }
7271
73- _begun = true ;
72+ // too many instances
73+ if (!(_cdc_instance_count < CFG_TUD_CDC)) {
74+ return ;
75+ }
7476
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 );
7781}
7882
7983void 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) {
8488void Adafruit_USBD_CDC::end (void ) {
8589 // Resset configuration descriptor without Serial as CDC
8690 USBDevice.clearConfiguration ();
91+ _cdc_instance_count = 0 ;
8792}
8893
8994uint32_t Adafruit_USBD_CDC::baud (void ) {
95+ if (!_begun) {
96+ return 0 ;
97+ }
98+
9099 cdc_line_coding_t coding;
91- tud_cdc_get_line_coding ( &coding);
100+ tud_cdc_n_get_line_coding (_itf, &coding);
92101
93102 return coding.bit_rate ;
94103}
95104
96105uint8_t Adafruit_USBD_CDC::stopbits (void ) {
106+ if (!_begun) {
107+ return 0 ;
108+ }
109+
97110 cdc_line_coding_t coding;
98- tud_cdc_get_line_coding ( &coding);
111+ tud_cdc_n_get_line_coding (_itf, &coding);
99112
100113 return coding.stop_bits ;
101114}
102115
103116uint8_t Adafruit_USBD_CDC::paritytype (void ) {
117+ if (!_begun) {
118+ return 0 ;
119+ }
120+
104121 cdc_line_coding_t coding;
105- tud_cdc_get_line_coding ( &coding);
122+ tud_cdc_n_get_line_coding (_itf, &coding);
106123
107124 return coding.parity ;
108125}
109126
110127uint8_t Adafruit_USBD_CDC::numbits (void ) {
128+ if (!_begun) {
129+ return 0 ;
130+ }
131+
111132 cdc_line_coding_t coding;
112- tud_cdc_get_line_coding ( &coding);
133+ tud_cdc_n_get_line_coding (_itf, &coding);
113134
114135 return coding.data_bits ;
115136}
116137
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+ }
118145
119146Adafruit_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);
121152
122153 // Add an yield to run usb background in case sketch block wait as follows
123154 // while( !Serial ) {}
@@ -128,7 +159,11 @@ Adafruit_USBD_CDC::operator bool() {
128159}
129160
130161int 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);
132167
133168 // Add an yield to run usb background in case sketch block wait as follows
134169 // while( !Serial.available() ) {}
@@ -140,20 +175,39 @@ int Adafruit_USBD_CDC::available(void) {
140175}
141176
142177int Adafruit_USBD_CDC::peek (void ) {
178+ if (!_begun) {
179+ return -1 ;
180+ }
181+
143182 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);
145191}
146192
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+ }
148197
149- void Adafruit_USBD_CDC::flush (void ) { tud_cdc_write_flush (); }
198+ tud_cdc_n_write_flush (_itf);
199+ }
150200
151201size_t Adafruit_USBD_CDC::write (uint8_t ch) { return write (&ch, 1 ); }
152202
153203size_t Adafruit_USBD_CDC::write (const uint8_t *buffer, size_t size) {
204+ if (!_begun) {
205+ return 0 ;
206+ }
207+
154208 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);
157211 remain -= wrcount;
158212 buffer += wrcount;
159213
@@ -167,20 +221,23 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
167221}
168222
169223int 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);
171228}
172229
173230extern " C" {
174231
175232// Invoked when cdc when line state changed e.g connected/disconnected
176233// 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) {
178235 (void )rts;
179236
180237 // DTR = false is counted as disconnected
181238 if (!dtr) {
182239 // touch1200 only with first CDC instance (Serial)
183- if (itf == 0 ) {
240+ if (instance == 0 ) {
184241 cdc_line_coding_t coding;
185242 tud_cdc_get_line_coding (&coding);
186243
0 commit comments