add buffer check before triggering new usb read

This commit is contained in:
sommersoft 2018-03-23 15:07:02 +00:00
parent 261586518a
commit d434635822
1 changed files with 20 additions and 9 deletions

View File

@ -25,6 +25,7 @@
*/
#include "usb.h"
#include <stdio.h>
#include <stdint.h>
@ -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;
}