From d434635822ed7d712c60b3c7a17b957bc09d919e Mon Sep 17 00:00:00 2001 From: sommersoft Date: Fri, 23 Mar 2018 15:07:02 +0000 Subject: [PATCH] add buffer check before triggering new usb read --- ports/atmel-samd/usb.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index 740486aa1e..a90308564e 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -25,6 +25,7 @@ */ #include "usb.h" +#include #include @@ -49,6 +50,7 @@ #include "usb_mass_storage.h" #include "supervisor/shared/autoreload.h" +#include "supervisor/serial.h" // Store received characters on our own so that we can filter control characters // and act immediately on CTRL-C for example. @@ -126,7 +128,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u atomic_leave_critical(&flags); return true; } - + for (uint16_t i = 0; i < count; i++) { uint8_t c = cdc_packet_buffer[i]; if (c == mp_interrupt_char) { @@ -150,15 +152,15 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u } } atomic_leave_critical(&flags); - + // Trigger a follow up read if we have space. - if (usb_rx_count < USB_RX_BUF_SIZE) { + /*if (usb_rx_count < USB_RX_BUF_SIZE - 64) { int32_t result = start_read(); if (result != ERR_NONE) { return true; } - } - + }*/ + /* No error. */ return false; } @@ -170,7 +172,9 @@ static bool write_complete(const uint8_t ep, return false; // No errors. } // This is called after writes are finished. + usb_transmitting = false; + /* No error. */ return false; } @@ -243,9 +247,16 @@ static bool cdc_enabled(void) { } bool usb_bytes_available(void) { + // Check if the buffer has data, but not enough + // space to hold another read. + if (usb_rx_count > 64) { + return usb_rx_count > 0; + } + // Buffer has enough room if (cdc_enabled() && !pending_read) { start_read(); } + // Buffer is empty and/or no new data is available if (usb_rx_count == 0) { return false; } @@ -263,16 +274,16 @@ int usb_read(void) { data = usb_rx_buf[usb_rx_buf_head]; usb_rx_buf_head++; usb_rx_count--; - if ((USB_RX_BUF_SIZE) == usb_rx_buf_head) { + if (usb_rx_buf_head == USB_RX_BUF_SIZE) { usb_rx_buf_head = 0; } CRITICAL_SECTION_LEAVE(); // Trigger a new read because we just cleared some space. - if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) { + /*if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) { start_read(); - } - + }*/ + return data; }