Skip to content

Commit

Permalink
uart-bridge: improve usb_write_bytes
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Noltari committed Feb 6, 2021
1 parent 7f8226a commit 206b612
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions uart-bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <hardware/uart.h>
#include <pico/multicore.h>
#include <pico/stdlib.h>
#include <string.h>
#include <tusb.h>

#if !defined(MIN)
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 206b612

Please sign in to comment.