Skip to content

Improve CDC read code #1953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 12 additions & 54 deletions hardware/arduino/cores/arduino/CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@
#if defined(USBCON)
#ifdef CDC_ENABLED

#if (RAMEND < 1000)
#define SERIAL_BUFFER_SIZE 16
#else
#define SERIAL_BUFFER_SIZE 64
#endif

struct ring_buffer
{
unsigned char buffer[SERIAL_BUFFER_SIZE];
volatile int head;
volatile int tail;
};

ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};

typedef struct
{
u32 dwDTERate;
Expand Down Expand Up @@ -129,7 +114,6 @@ bool WEAK CDC_Setup(Setup& setup)
}


int _serialPeek = -1;
void Serial_::begin(unsigned long baud_count)
{
}
Expand All @@ -142,55 +126,29 @@ void Serial_::end(void)
{
}

void Serial_::accept(void)
{
ring_buffer *buffer = &cdc_rx_buffer;
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.

// 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;
}
}

int Serial_::available(void)
{
ring_buffer *buffer = &cdc_rx_buffer;
return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
if (peek_buffer >= 0) {
return 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that the
return 1;
should be:
return 1 + USB_Available(CDC_RX);

To avoid situations where queued chars may seems to "disappear":

av1 = Serial.available(); // av1 may be > 1
Serial.peek();
av2 = Serial.available(); // after the peek av2 is 1

assert(av2>=av1); // Assertion fails if av1>1

}
return USB_Available(CDC_RX);
}

int Serial_::peek(void)
{
ring_buffer *buffer = &cdc_rx_buffer;
if (buffer->head == buffer->tail) {
return -1;
} else {
return buffer->buffer[buffer->tail];
}
if (peek_buffer < 0)
peek_buffer = USB_Recv(CDC_RX);
return peek_buffer;
}

int Serial_::read(void)
{
ring_buffer *buffer = &cdc_rx_buffer;
// if the head isn't ahead of the tail, we don't have any characters
if (buffer->head == buffer->tail) {
return -1;
} else {
unsigned char c = buffer->buffer[buffer->tail];
buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
if (peek_buffer >= 0) {
int c = peek_buffer;
peek_buffer = -1;
return c;
}
}
return USB_Recv(CDC_RX);
}

void Serial_::flush(void)
Expand Down
3 changes: 1 addition & 2 deletions hardware/arduino/cores/arduino/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ extern USBDevice_ USBDevice;
class Serial_ : public Stream
{
private:
ring_buffer *_cdc_rx_buffer;
int peek_buffer;
public:
void begin(unsigned long);
void begin(unsigned long, uint8_t);
void end(void);

virtual int available(void);
virtual void accept(void);
virtual int peek(void);
virtual int read(void);
virtual void flush(void);
Expand Down
2 changes: 0 additions & 2 deletions hardware/arduino/cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,6 @@ ISR(USB_GEN_vect)
{
#ifdef CDC_ENABLED
USB_Flush(CDC_TX); // Send a tx frame if found
if (USB_Available(CDC_RX)) // Handle received bytes (if any)
Serial.accept();
#endif

// check whether the one-shot period has elapsed. if so, turn off the LED
Expand Down