mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-29 18:52:13 +01:00
Avoid serial buffer overrun on leonardo
This commit is contained in:
parent
e2b99206d8
commit
6ab2a9f95e
@ -141,16 +141,22 @@ void Serial_::end(void)
|
|||||||
void Serial_::accept(void)
|
void Serial_::accept(void)
|
||||||
{
|
{
|
||||||
ring_buffer *buffer = &cdc_rx_buffer;
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
int c = USB_Recv(CDC_RX);
|
|
||||||
int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
|
int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
|
||||||
|
|
||||||
// if we should be storing the received character into the location
|
// if we should be storing the received character into the location
|
||||||
// just before the tail (meaning that the head would advance to the
|
// just before the tail (meaning that the head would advance to the
|
||||||
// current location of the tail), we're about to overflow the buffer
|
// current location of the tail), we're about to overflow the buffer
|
||||||
// and so we don't write the character or advance the head.
|
// 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->buffer[buffer->head] = c;
|
||||||
buffer->head = i;
|
buffer->head = i;
|
||||||
|
|
||||||
|
i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -603,7 +603,7 @@ ISR(USB_GEN_vect)
|
|||||||
{
|
{
|
||||||
#ifdef CDC_ENABLED
|
#ifdef CDC_ENABLED
|
||||||
USB_Flush(CDC_TX); // Send a tx frame if found
|
USB_Flush(CDC_TX); // Send a tx frame if found
|
||||||
while (USB_Available(CDC_RX)) // Handle received bytes (if any)
|
if (USB_Available(CDC_RX)) // Handle received bytes (if any)
|
||||||
Serial.accept();
|
Serial.accept();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user