Skip to content

Commit 5a6fffc

Browse files
authored
Merge pull request #7100 from tannewt/fix_usb_cdc_read
Fix USB issues caused by early usb_cdc.Serial read
2 parents a4238d8 + eb1b2f3 commit 5a6fffc

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

shared-module/usb_cdc/Serial.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ size_t common_hal_usb_cdc_serial_read(usb_cdc_serial_obj_t *self, uint8_t *data,
3838

3939
// Read up to len bytes immediately.
4040
// The number of bytes read will not be larger than what is already in the TinyUSB FIFO.
41-
uint32_t total_num_read = tud_cdc_n_read(self->idx, data, len);
41+
uint32_t total_num_read = 0;
42+
if (tud_cdc_n_connected(self->idx)) {
43+
total_num_read = tud_cdc_n_read(self->idx, data, len);
44+
}
4245

4346
if (wait_forever || wait_for_timeout) {
4447
// Continue filling the buffer past what we already read.
@@ -65,7 +68,9 @@ size_t common_hal_usb_cdc_serial_read(usb_cdc_serial_obj_t *self, uint8_t *data,
6568
data += num_read;
6669

6770
// Try to read another batch of bytes.
68-
num_read = tud_cdc_n_read(self->idx, data, len);
71+
if (tud_cdc_n_connected(self->idx)) {
72+
num_read = tud_cdc_n_read(self->idx, data, len);
73+
}
6974
total_num_read += num_read;
7075
}
7176
}

0 commit comments

Comments
 (0)