Skip to content

Commit 206b612

Browse files
committed
uart-bridge: improve usb_write_bytes
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 <[email protected]>
1 parent 7f8226a commit 206b612

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

uart-bridge.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <hardware/uart.h>
99
#include <pico/multicore.h>
1010
#include <pico/stdlib.h>
11+
#include <string.h>
1112
#include <tusb.h>
1213

1314
#if !defined(MIN)
@@ -147,12 +148,15 @@ void usb_write_bytes(uint8_t itf) {
147148
mutex_enter_blocking(&ud->uart_mtx);
148149

149150
count = tud_cdc_n_write(itf, ud->uart_buffer, ud->uart_pos);
150-
if (count) {
151-
ud->uart_pos -= count;
152-
tud_cdc_n_write_flush(itf);
153-
}
151+
if (count < ud->uart_pos)
152+
memcpy(ud->uart_buffer, &ud->uart_buffer[count],
153+
ud->uart_pos - count);
154+
ud->uart_pos -= count;
154155

155156
mutex_exit(&ud->uart_mtx);
157+
158+
if (count)
159+
tud_cdc_n_write_flush(itf);
156160
}
157161
}
158162

0 commit comments

Comments
 (0)