File tree 1 file changed +27
-9
lines changed
1 file changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -235,22 +235,40 @@ void SerialUSB::flush() {
235
235
size_t SerialUSB::write (uint8_t c) {
236
236
return write (&c, 1 );
237
237
}
238
- size_t SerialUSB::write (const uint8_t *p, size_t len) {
238
+ size_t SerialUSB::write (const uint8_t *buf, size_t length) {
239
+ static uint64_t last_avail_time;
239
240
uint32_t owner;
240
241
if (!mutex_try_enter (&usb_mutex, &owner)) {
241
242
if (owner == get_core_num ()) return 0 ; // would deadlock otherwise
242
243
mutex_enter_blocking (&usb_mutex);
243
244
}
244
- size_t remain = len;
245
- while (remain && tud_cdc_connected ()) {
246
- size_t cnt = tud_cdc_write (p, remain);
247
- p += cnt;
248
- remain -= cnt;
249
- tud_task ();
250
- tud_cdc_write_flush ();
245
+ int i = 0 ;
246
+ if (tud_cdc_connected ()) {
247
+ for (int i = 0 ; i < length;) {
248
+ int n = length - i;
249
+ int avail = tud_cdc_write_available ();
250
+ if (n > avail) n = avail;
251
+ if (n) {
252
+ int n2 = tud_cdc_write (buf + i, n);
253
+ tud_task ();
254
+ tud_cdc_write_flush ();
255
+ i += n2;
256
+ last_avail_time = time_us_64 ();
257
+ } else {
258
+ tud_task ();
259
+ tud_cdc_write_flush ();
260
+ if (!tud_cdc_connected () ||
261
+ (!tud_cdc_write_available () && time_us_64 () > last_avail_time + 1000000 /* 1 second */ )) {
262
+ break ;
263
+ }
264
+ }
265
+ }
266
+ } else {
267
+ // reset our timeout
268
+ last_avail_time = 0 ;
251
269
}
252
270
mutex_exit (&usb_mutex);
253
- return len - remain ;
271
+ return i ;
254
272
}
255
273
SerialUSB::operator bool () {
256
274
uint32_t owner;
You can’t perform that action at this time.
0 commit comments