Skip to content

Commit 5aeca20

Browse files
authored
Merge pull request #299 from adafruit/update-serial-host-example
Enhance Host CDC
2 parents 29a0342 + 2fc1e48 commit 5aeca20

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

examples/DualRole/CDC/serial_host_bridge/serial_host_bridge.ino

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ Adafruit_USBH_CDC SerialHost;
5656
//--------------------------------------------------------------------+
5757

5858
void setup() {
59-
Serial1.begin(115200);
60-
6159
Serial.begin(115200);
62-
while ( !Serial ) delay(10); // wait for native usb
60+
// while ( !Serial ) delay(10); // wait for native usb
6361

6462
Serial.println("TinyUSB Host Serial Echo Example");
6563
}
@@ -89,7 +87,7 @@ void loop()
8987
//--------------------------------------------------------------------+
9088

9189
void setup1() {
92-
while ( !Serial ) delay(10); // wait for native usb
90+
// while ( !Serial ) delay(10); // wait for native usb
9391
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
9492

9593
// Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
@@ -125,6 +123,8 @@ void setup1() {
125123
// Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the
126124
// host bit-banging processing works done in core1 to free up core0 for other works
127125
USBHost.begin(1);
126+
127+
SerialHost.begin(115200);
128128
}
129129

130130
void loop1()
@@ -140,23 +140,20 @@ void loop1()
140140
//--------------------------------------------------------------------+
141141
// TinyUSB Host callbacks
142142
//--------------------------------------------------------------------+
143+
extern "C" {
143144

144145
// Invoked when a device with CDC interface is mounted
145146
// idx is index of cdc interface in the internal pool.
146147
void tuh_cdc_mount_cb(uint8_t idx) {
147148
// bind SerialHost object to this interface index
148-
SerialHost.setInterfaceIndex(idx);
149-
SerialHost.begin(115200);
150-
149+
SerialHost.mount(idx);
151150
Serial.println("SerialHost is connected to a new CDC device");
152151
}
153152

154153
// Invoked when a device with CDC interface is unmounted
155154
void tuh_cdc_umount_cb(uint8_t idx) {
156-
if (idx == SerialHost.getInterfaceIndex()) {
157-
// unbind SerialHost if this interface is unmounted
158-
SerialHost.end();
159-
160-
Serial.println("SerialHost is disconnected");
161-
}
155+
SerialHost.umount(idx);
156+
Serial.println("SerialHost is disconnected");
162157
}
158+
159+
}

src/arduino/cdc/Adafruit_USBH_CDC.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void Adafruit_USBH_CDC::begin(unsigned long baudrate) {
4242
if (_baud == 0) {
4343
_baud = 115200; // default, backward compatible with previous API begin(0)
4444
}
45+
46+
// if already mounted, this will set baudrate
47+
if (mounted()) {
48+
setBaudrate(_baud);
49+
}
4550
}
4651

4752
void Adafruit_USBH_CDC::begin(unsigned long baudrate, uint16_t config) {
@@ -103,6 +108,11 @@ bool Adafruit_USBH_CDC::setBaudrate(uint32_t baudrate,
103108
return false;
104109
}
105110

111+
if (baud() == baudrate) {
112+
// skip if already matched
113+
return true;
114+
}
115+
106116
return tuh_cdc_set_baudrate(_idx, baudrate, complete_cb, user_data);
107117
}
108118

0 commit comments

Comments
 (0)