Skip to content

Commit 0ef6be1

Browse files
committed
cdc instance assigned when begin
revert itf to instance rename
1 parent 69576fb commit 0ef6be1

File tree

4 files changed

+79
-49
lines changed

4 files changed

+79
-49
lines changed

examples/CDC/multiCDC/multiCDC.ino

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
MIT license, check LICENSE for more information
1212
1313
The example creates three virtual serial ports. Text entered on
14-
any of the ports will be echoed to the other two ports.
14+
any of the ports will be echoed to the all ports.
1515
1616
The max number of CDC ports (CFG_TUD_CDC) has to be changed to at
1717
least 3, changed in the core tusb_config.h file.
@@ -22,61 +22,75 @@
2222
#define LED LED_BUILTIN
2323

2424
/*
25-
Create extra USB Serial Ports. "Serial" is already created.
25+
Create extra USB Serial Ports. "Serial" is already created.
2626
*/
27-
Adafruit_USBD_CDC USBSer2(1);
28-
Adafruit_USBD_CDC USBSer3(2);
29-
30-
Adafruit_USBD_CDC *ports[CFG_TUD_CDC] = { &Serial, &USBSer2, &USBSer3, NULL };
27+
Adafruit_USBD_CDC USBSer1;
28+
Adafruit_USBD_CDC USBSer2;
3129

3230
void setup() {
3331
pinMode(LED, OUTPUT);
32+
3433
// start up all of the USB Vitual ports, and wait for them to enumerate.
35-
Serial.begin(115200); // Do these in order, or
36-
USBSer2.begin(115200); // "bad things will happen"
37-
USBSer3.begin(115200);
38-
while (!Serial || !USBSer2 || !USBSer3) {
34+
Serial.begin(115200);
35+
USBSer1.begin(115200);
36+
USBSer2.begin(115200);
37+
38+
while (!Serial || !USBSer1 || !USBSer2) {
3939
if (Serial) {
4040
Serial.println("Waiting for other USB ports");
4141
}
42+
if (USBSer1) {
43+
USBSer1.println("Waiting for other USB ports");
44+
}
4245
if (USBSer2) {
4346
USBSer2.println("Waiting for other USB ports");
4447
}
45-
if (USBSer3) {
46-
USBSer3.println("Waiting for other USB ports");
47-
}
4848
delay(1000);
4949
}
50+
5051
Serial.print("You are port 0\n\r\n0> ");
51-
USBSer2.print("You are port 1\n\r\n1> ");
52-
USBSer3.print("You are port 2\n\r\n2> ");
52+
USBSer1.print("You are port 1\n\r\n1> ");
53+
USBSer2.print("You are port 2\n\r\n2> ");
5354
}
5455

5556
int LEDstate = 0;
5657

5758
void loop() {
5859
int ch;
60+
5961
ch = Serial.read();
6062
if (ch > 0) {
61-
USBSer2.write(ch);
62-
USBSer3.write(ch);
63+
printAll(ch);
6364
}
64-
ch = USBSer2.read();
65+
66+
ch = USBSer1.read();
6567
if (ch > 0) {
66-
Serial.write(ch);
67-
USBSer3.write(ch);
68+
printAll(ch);
6869
}
69-
ch = USBSer3.read();
70+
71+
ch = USBSer2.read();
7072
if (ch > 0) {
71-
Serial.write(ch);
72-
USBSer2.write(ch);
73+
printAll(ch);
7374
}
75+
7476
if (delay_without_delaying(500)) {
7577
LEDstate = !LEDstate;
7678
digitalWrite(LED, LEDstate);
7779
}
7880
}
7981

82+
// print to all CDC ports
83+
void printAll(int ch) {
84+
// print as it is
85+
Serial.write(ch);
86+
87+
// always lower case
88+
USBSer1.write(tolower(ch));
89+
90+
// always upper case
91+
USBSer2.write(toupper(ch));
92+
}
93+
8094
// Helper: non-blocking "delay" alternative.
8195
boolean delay_without_delaying(unsigned long time) {
8296
// return false if we're still "delaying", true if time ms has passed.
@@ -89,5 +103,3 @@ boolean delay_without_delaying(unsigned long time) {
89103
}
90104
return false;
91105
}
92-
93-
// Helper: non-blocking line-at-a-time read.

src/arduino/Adafruit_TinyUSB_API.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ void TinyUSB_Device_Task(void) {
4646
}
4747
#endif
4848

49+
// TODO should use getInstanceCount() API (when closed to BSP release cycle)
50+
// from Adafruit_USBD_CDC.cpp
51+
extern uint8_t _cdc_instance_count;
52+
4953
void TinyUSB_Device_FlushCDC(void) {
50-
for (uint8_t CDCinstance=0; CDCinstance < CFG_TUD_CDC; CDCinstance++) {
51-
// note that flushing an uninitialized CDC is harmless.
52-
tud_cdc_n_write_flush(CDCinstance);
54+
for (uint8_t instance = 0; instance < _cdc_instance_count; instance++) {
55+
tud_cdc_n_write_flush(instance);
5356
}
5457
}
5558
}

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@
3333
#include "Adafruit_USBD_CDC.h"
3434
#include "Adafruit_USBD_Device.h"
3535

36-
#define EPOUT 0x00 // Starting endpoints; adjusted elsewhere as needed
36+
// Starting endpoints; adjusted elsewhere as needed
37+
#define EPOUT 0x00
3738
#define EPIN 0x80
3839

40+
uint8_t _cdc_instance_count = 0;
41+
3942
Adafruit_USBD_CDC Serial;
4043

41-
Adafruit_USBD_CDC::Adafruit_USBD_CDC(int instance) {
44+
Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) {
4245
_begun = false;
43-
_instance = instance;
46+
_itf = 0;
4447
}
4548

4649
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
@@ -66,7 +69,13 @@ void Adafruit_USBD_CDC::begin(uint32_t baud) {
6669
return;
6770
}
6871

72+
// too many instances
73+
if (!(_cdc_instance_count < CFG_TUD_CDC)) {
74+
return;
75+
}
76+
6977
_begun = true;
78+
_itf = _cdc_instance_count++;
7079
this->setStringDescriptor("TinyUSB Serial");
7180
USBDevice.addInterface(*this);
7281
}
@@ -79,40 +88,41 @@ void Adafruit_USBD_CDC::begin(uint32_t baud, uint8_t config) {
7988
void Adafruit_USBD_CDC::end(void) {
8089
// Resset configuration descriptor without Serial as CDC
8190
USBDevice.clearConfiguration();
91+
_cdc_instance_count = 0;
8292
}
8393

8494
uint32_t Adafruit_USBD_CDC::baud(void) {
8595
cdc_line_coding_t coding;
86-
tud_cdc_n_get_line_coding(_instance, &coding);
96+
tud_cdc_n_get_line_coding(_itf, &coding);
8797

8898
return coding.bit_rate;
8999
}
90100

91101
uint8_t Adafruit_USBD_CDC::stopbits(void) {
92102
cdc_line_coding_t coding;
93-
tud_cdc_n_get_line_coding(_instance, &coding);
103+
tud_cdc_n_get_line_coding(_itf, &coding);
94104

95105
return coding.stop_bits;
96106
}
97107

98108
uint8_t Adafruit_USBD_CDC::paritytype(void) {
99109
cdc_line_coding_t coding;
100-
tud_cdc_n_get_line_coding(_instance, &coding);
110+
tud_cdc_n_get_line_coding(_itf, &coding);
101111

102112
return coding.parity;
103113
}
104114

105115
uint8_t Adafruit_USBD_CDC::numbits(void) {
106116
cdc_line_coding_t coding;
107-
tud_cdc_n_get_line_coding(_instance, &coding);
117+
tud_cdc_n_get_line_coding(_itf, &coding);
108118

109119
return coding.data_bits;
110120
}
111121

112-
int Adafruit_USBD_CDC::dtr(void) { return tud_cdc_n_connected(_instance); }
122+
int Adafruit_USBD_CDC::dtr(void) { return tud_cdc_n_connected(_itf); }
113123

114124
Adafruit_USBD_CDC::operator bool() {
115-
bool ret = tud_cdc_n_connected(_instance);
125+
bool ret = tud_cdc_n_connected(_itf);
116126

117127
// Add an yield to run usb background in case sketch block wait as follows
118128
// while( !Serial ) {}
@@ -123,7 +133,7 @@ Adafruit_USBD_CDC::operator bool() {
123133
}
124134

125135
int Adafruit_USBD_CDC::available(void) {
126-
uint32_t count = tud_cdc_n_available(_instance);
136+
uint32_t count = tud_cdc_n_available(_itf);
127137

128138
// Add an yield to run usb background in case sketch block wait as follows
129139
// while( !Serial.available() ) {}
@@ -136,19 +146,19 @@ int Adafruit_USBD_CDC::available(void) {
136146

137147
int Adafruit_USBD_CDC::peek(void) {
138148
uint8_t ch;
139-
return tud_cdc_n_peek(_instance, &ch) ? (int)ch : -1;
149+
return tud_cdc_n_peek(_itf, &ch) ? (int)ch : -1;
140150
}
141151

142-
int Adafruit_USBD_CDC::read(void) { return (int)tud_cdc_n_read_char(_instance); }
152+
int Adafruit_USBD_CDC::read(void) { return (int)tud_cdc_n_read_char(_itf); }
143153

144-
void Adafruit_USBD_CDC::flush(void) { tud_cdc_n_write_flush(_instance); }
154+
void Adafruit_USBD_CDC::flush(void) { tud_cdc_n_write_flush(_itf); }
145155

146156
size_t Adafruit_USBD_CDC::write(uint8_t ch) { return write(&ch, 1); }
147157

148158
size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
149159
size_t remain = size;
150-
while (remain && tud_cdc_n_connected(_instance)) {
151-
size_t wrcount = tud_cdc_n_write(_instance, buffer, remain);
160+
while (remain && tud_cdc_n_connected(_itf)) {
161+
size_t wrcount = tud_cdc_n_write(_itf, buffer, remain);
152162
remain -= wrcount;
153163
buffer += wrcount;
154164

@@ -162,14 +172,14 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
162172
}
163173

164174
int Adafruit_USBD_CDC::availableForWrite(void) {
165-
return tud_cdc_n_write_available(_instance);
175+
return tud_cdc_n_write_available(_itf);
166176
}
167177

168178
extern "C" {
169179

170180
// Invoked when cdc when line state changed e.g connected/disconnected
171181
// Use to reset to DFU when disconnect with 1200 bps
172-
void tud_cdc_line_state_cb(uint8_t instance, bool dtr, bool rts) {
182+
void tud_cdc_line_state_cb(uint8_t instance, bool dtr, bool rts) {
173183
(void)rts;
174184

175185
// DTR = false is counted as disconnected

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
3636
public:
37-
Adafruit_USBD_CDC(int instance);
38-
Adafruit_USBD_CDC(void) { Adafruit_USBD_CDC(0); }
37+
Adafruit_USBD_CDC(void);
3938

4039
// fron Adafruit_USBD_Interface
4140
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
@@ -71,11 +70,17 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
7170
virtual int availableForWrite(void);
7271
using Print::write; // pull in write(str) from Print
7372
operator bool();
74-
uint8_t getInstance() { return _instance; }
7573

7674
private:
7775
bool _begun;
78-
uint8_t _instance; // which CDC of CFG_TUD_CDC total CDCs.
76+
uint8_t _itf;
77+
78+
/* TODO when closed to BSP release cycle (SAMD, nRF, rp2040)
79+
* rename _itf to _instance and add
80+
*
81+
* static uint8_t getInstanceCount(void);
82+
* static uint8_t _instance_count;
83+
*/
7984
};
8085

8186
extern Adafruit_USBD_CDC Serial;

0 commit comments

Comments
 (0)