Skip to content

Commit cfbcc00

Browse files
authored
Merge pull request arduino#106 from facchinm/usb_fixes
usb: c33: fix serial errors
2 parents a728266 + fb9c70e commit cfbcc00

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

cores/arduino/usb/SerialUSB.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,41 +80,40 @@ size_t _SerialUSB::write(uint8_t c) {
8080
return write(&c, 1);
8181
}
8282

83-
size_t _SerialUSB::write(const uint8_t *buf, size_t length) {
84-
if (!_running) {
83+
#include "device/usbd_pvt.h"
84+
85+
size_t _SerialUSB::write(const uint8_t *buf, size_t size) {
86+
if (!_running) {
8587
return 0;
8688
}
8789

88-
static uint64_t last_avail_time;
89-
int written = 0;
90-
if (connected()) {
91-
for (size_t i = 0; i < length;) {
92-
int n = length - i;
93-
int avail = tud_cdc_write_available();
94-
if (n > avail) {
95-
n = avail;
96-
}
97-
if (n) {
98-
int n2 = tud_cdc_write(buf + i, n);
99-
tud_task();
100-
tud_cdc_write_flush();
101-
i += n2;
102-
written += n2;
103-
last_avail_time = millis();
104-
} else {
105-
tud_task();
106-
tud_cdc_write_flush();
107-
if (connected() ||
108-
(!tud_cdc_write_available() && millis() > last_avail_time + 1000 /* 1 second */)) {
109-
break;
110-
}
111-
}
90+
if (!connected()) {
91+
return 0;
92+
}
93+
usbd_int_set(false);
94+
size_t to_send = size, so_far = 0;
95+
while(to_send){
96+
size_t space = tud_cdc_write_available();
97+
if(!space){
98+
usbd_int_set(true);
99+
tud_cdc_write_flush();
100+
continue;
101+
}
102+
if(space > to_send){
103+
space = to_send;
104+
}
105+
size_t sent = tud_cdc_write( buf+so_far, space);
106+
usbd_int_set(true);
107+
if(sent){
108+
so_far += sent;
109+
to_send -= sent;
110+
tud_cdc_write_flush();
111+
} else {
112+
size = so_far;
113+
break;
112114
}
113-
} else {
114-
// reset our timeout
115-
last_avail_time = 0;
116115
}
117-
return written;
116+
return size;
118117
}
119118

120119
_SerialUSB::operator bool() {

extras/tinyusb

Submodule tinyusb updated 297 files

variants/PORTENTA_C33/tusb_config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@
109109

110110

111111
// CDC FIFO size of TX and RX
112-
#define CFG_TUD_CDC_RX_BUFSIZE ((TUD_OPT_HIGH_SPEED ? 512 : 64) * 8)
113-
#define CFG_TUD_CDC_TX_BUFSIZE ((TUD_OPT_HIGH_SPEED ? 512 : 64) * 8)
112+
#define CFG_TUD_CDC_RX_BUFSIZE (4096)
113+
#define CFG_TUD_CDC_TX_BUFSIZE (64)
114114

115115
// CDC Endpoint transfer buffer size, more is faster
116116
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

0 commit comments

Comments
 (0)