From 206b612bae85ee0d9e2a84d9396666e727f4ec20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= Date: Sat, 6 Feb 2021 12:05:47 +0100 Subject: [PATCH] uart-bridge: improve usb_write_bytes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tud_cdc_n_write() may not be able to write the full buffer, so we need to handle that by moving the remaining bytes in the buffer to the buffer start. Signed-off-by: Álvaro Fernández Rojas --- uart-bridge.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/uart-bridge.c b/uart-bridge.c index 5c1271d..8bdd592 100644 --- a/uart-bridge.c +++ b/uart-bridge.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #if !defined(MIN) @@ -147,12 +148,15 @@ void usb_write_bytes(uint8_t itf) { mutex_enter_blocking(&ud->uart_mtx); count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos); - if (count) { - ud->uart_pos -= count; - tud_cdc_n_write_flush(itf); - } + if (count < ud->uart_pos) + memcpy(ud->uart_buffer, &ud->uart_buffer[count], + ud->uart_pos - count); + ud->uart_pos -= count; mutex_exit(&ud->uart_mtx); + + if (count) + tud_cdc_n_write_flush(itf); } }