Skip to content

Commit 282f8c4

Browse files
authored
Merge pull request #6660 from tannewt/fix_c3_serial_tx
Improve USB to Serial/JTAG TX
2 parents cb5fa3e + bea9552 commit 282f8c4

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

ports/espressif/supervisor/usb_serial_jtag.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,23 @@ bool usb_serial_jtag_bytes_available(void) {
9898
}
9999

100100
void usb_serial_jtag_write(const char *text, uint32_t length) {
101-
if (USB_SERIAL_JTAG.fram_num.sof_frame_index > 0) {
102-
size_t total_written = 0;
101+
if (!usb_serial_jtag_connected()) {
102+
return;
103+
}
104+
size_t total_written = 0;
105+
while (total_written < length) {
103106
uint32_t start_time = supervisor_ticks_ms32();
104-
// Time out after 5 milliseconds in case usb isn't actually reading CDC.
105-
while (total_written < length && start_time - supervisor_ticks_ms32() < 5) {
106-
total_written += usb_serial_jtag_ll_write_txfifo((const uint8_t *)(text + total_written), length - total_written);
107-
RUN_BACKGROUND_TASKS;
107+
// Wait until we can write to the FIFO again. If it takes too long, then
108+
// assume we're disconnected.
109+
while (!usb_serial_jtag_ll_txfifo_writable()) {
110+
uint32_t now = supervisor_ticks_ms32();
111+
if (now - start_time > 200) {
112+
connected = false;
113+
return;
114+
}
108115
}
109-
usb_serial_jtag_ll_txfifo_flush();
116+
total_written += usb_serial_jtag_ll_write_txfifo((const uint8_t *)(text + total_written), length - total_written);
117+
RUN_BACKGROUND_TASKS;
110118
}
119+
usb_serial_jtag_ll_txfifo_flush();
111120
}

0 commit comments

Comments
 (0)