diff options
author | Peter Van Hoyweghen <peter@Dell4550.(none)> | 2012-07-30 21:54:19 +0200 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2012-09-13 08:46:45 -0400 |
commit | dc86d26a11ffe3e1ac9add8fb2d397fbee20984b (patch) | |
tree | 1a2d991c9a2293c6490baa8b55b608e0a1b0a648 /cores/arduino/CDC.cpp | |
parent | 5ca747e312e70aa87b6056c15f6dae41c2f0f20c (diff) |
Avoid serial buffer overrun on leonardo
Diffstat (limited to 'cores/arduino/CDC.cpp')
-rw-r--r-- | cores/arduino/CDC.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp index 1ee3a48..701e483 100644 --- a/cores/arduino/CDC.cpp +++ b/cores/arduino/CDC.cpp @@ -141,16 +141,22 @@ void Serial_::end(void) void Serial_::accept(void) { ring_buffer *buffer = &cdc_rx_buffer; - int c = USB_Recv(CDC_RX); int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE; // if we should be storing the received character into the location // just before the tail (meaning that the head would advance to the // current location of the tail), we're about to overflow the buffer // and so we don't write the character or advance the head. - if (i != buffer->tail) { + + // while we have room to store a byte + while (i != buffer->tail) { + int c = USB_Recv(CDC_RX); + if (c == -1) + break; // no more data buffer->buffer[buffer->head] = c; buffer->head = i; + + i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE; } } |