Skip to content

Commit e8cba09

Browse files
SuGliderCopilotpre-commit-ci-lite[bot]
authored
feat(tinyusb_cdc): avoid infinite loop when TinyUSB CDC layer hangs (#11904)
* feat(tinyusb_cdc): write timeout Added a timeout check to prevent locking during CDC writes. * fix(usb_cdc): better timeout calculation Co-authored-by: Copilot <[email protected]> * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 9875e37 commit e8cba09

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

cores/esp32/USBCDC.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,11 @@ size_t USBCDC::write(const uint8_t *buffer, size_t size) {
405405
return 0;
406406
}
407407
size_t to_send = size, so_far = 0;
408+
// writeTimeout will prevent that TinyUSB failure locks the while(to_send) loop
409+
uint32_t writeTimeout = millis() + tx_timeout_ms;
408410
while (to_send) {
409-
if (!tud_cdc_n_connected(itf)) {
411+
if (!tud_cdc_n_connected(itf) || (int32_t)(millis() - writeTimeout) >= 0) {
412+
log_e("USB is disconnected or CDC writing has timed out.");
410413
size = so_far;
411414
break;
412415
}

0 commit comments

Comments
 (0)