Skip to content

Commit d434635

Browse files
committed
add buffer check before triggering new usb read
1 parent 2615865 commit d434635

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

ports/atmel-samd/usb.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "usb.h"
28+
#include <stdio.h>
2829

2930
#include <stdint.h>
3031

@@ -49,6 +50,7 @@
4950
#include "usb_mass_storage.h"
5051

5152
#include "supervisor/shared/autoreload.h"
53+
#include "supervisor/serial.h"
5254

5355
// Store received characters on our own so that we can filter control characters
5456
// 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
126128
atomic_leave_critical(&flags);
127129
return true;
128130
}
129-
131+
130132
for (uint16_t i = 0; i < count; i++) {
131133
uint8_t c = cdc_packet_buffer[i];
132134
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
150152
}
151153
}
152154
atomic_leave_critical(&flags);
153-
155+
154156
// Trigger a follow up read if we have space.
155-
if (usb_rx_count < USB_RX_BUF_SIZE) {
157+
/*if (usb_rx_count < USB_RX_BUF_SIZE - 64) {
156158
int32_t result = start_read();
157159
if (result != ERR_NONE) {
158160
return true;
159161
}
160-
}
161-
162+
}*/
163+
162164
/* No error. */
163165
return false;
164166
}
@@ -170,7 +172,9 @@ static bool write_complete(const uint8_t ep,
170172
return false; // No errors.
171173
}
172174
// This is called after writes are finished.
175+
173176
usb_transmitting = false;
177+
174178
/* No error. */
175179
return false;
176180
}
@@ -243,9 +247,16 @@ static bool cdc_enabled(void) {
243247
}
244248

245249
bool usb_bytes_available(void) {
250+
// Check if the buffer has data, but not enough
251+
// space to hold another read.
252+
if (usb_rx_count > 64) {
253+
return usb_rx_count > 0;
254+
}
255+
// Buffer has enough room
246256
if (cdc_enabled() && !pending_read) {
247257
start_read();
248258
}
259+
// Buffer is empty and/or no new data is available
249260
if (usb_rx_count == 0) {
250261
return false;
251262
}
@@ -263,16 +274,16 @@ int usb_read(void) {
263274
data = usb_rx_buf[usb_rx_buf_head];
264275
usb_rx_buf_head++;
265276
usb_rx_count--;
266-
if ((USB_RX_BUF_SIZE) == usb_rx_buf_head) {
277+
if (usb_rx_buf_head == USB_RX_BUF_SIZE) {
267278
usb_rx_buf_head = 0;
268279
}
269280
CRITICAL_SECTION_LEAVE();
270281

271282
// Trigger a new read because we just cleared some space.
272-
if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) {
283+
/*if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) {
273284
start_read();
274-
}
275-
285+
}*/
286+
276287
return data;
277288
}
278289

0 commit comments

Comments
 (0)