Skip to content

Commit a7ac8f6

Browse files
pbrookcmaglie
authored andcommitted
Improve CDC read code
Read CDC data from USB FIFO on demand instead of in ISR. Remove superfluous ring buffer. Signed-off-by: Paul Brook <[email protected]>
1 parent 8a37e72 commit a7ac8f6

File tree

3 files changed

+13
-58
lines changed

3 files changed

+13
-58
lines changed

arduino/cores/arduino/CDC.cpp

+12-54
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@
2323
#if defined(USBCON)
2424
#ifdef CDC_ENABLED
2525

26-
#if (RAMEND < 1000)
27-
#define SERIAL_BUFFER_SIZE 16
28-
#else
29-
#define SERIAL_BUFFER_SIZE 64
30-
#endif
31-
32-
struct ring_buffer
33-
{
34-
unsigned char buffer[SERIAL_BUFFER_SIZE];
35-
volatile int head;
36-
volatile int tail;
37-
};
38-
39-
ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
40-
4126
typedef struct
4227
{
4328
u32 dwDTERate;
@@ -129,7 +114,6 @@ bool WEAK CDC_Setup(Setup& setup)
129114
}
130115

131116

132-
int _serialPeek = -1;
133117
void Serial_::begin(unsigned long baud_count)
134118
{
135119
}
@@ -142,55 +126,29 @@ void Serial_::end(void)
142126
{
143127
}
144128

145-
void Serial_::accept(void)
146-
{
147-
ring_buffer *buffer = &cdc_rx_buffer;
148-
int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
149-
150-
// if we should be storing the received character into the location
151-
// just before the tail (meaning that the head would advance to the
152-
// current location of the tail), we're about to overflow the buffer
153-
// and so we don't write the character or advance the head.
154-
155-
// while we have room to store a byte
156-
while (i != buffer->tail) {
157-
int c = USB_Recv(CDC_RX);
158-
if (c == -1)
159-
break; // no more data
160-
buffer->buffer[buffer->head] = c;
161-
buffer->head = i;
162-
163-
i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
164-
}
165-
}
166-
167129
int Serial_::available(void)
168130
{
169-
ring_buffer *buffer = &cdc_rx_buffer;
170-
return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
131+
if (peek_buffer >= 0) {
132+
return 1;
133+
}
134+
return USB_Available(CDC_RX);
171135
}
172136

173137
int Serial_::peek(void)
174138
{
175-
ring_buffer *buffer = &cdc_rx_buffer;
176-
if (buffer->head == buffer->tail) {
177-
return -1;
178-
} else {
179-
return buffer->buffer[buffer->tail];
180-
}
139+
if (peek_buffer < 0)
140+
peek_buffer = USB_Recv(CDC_RX);
141+
return peek_buffer;
181142
}
182143

183144
int Serial_::read(void)
184145
{
185-
ring_buffer *buffer = &cdc_rx_buffer;
186-
// if the head isn't ahead of the tail, we don't have any characters
187-
if (buffer->head == buffer->tail) {
188-
return -1;
189-
} else {
190-
unsigned char c = buffer->buffer[buffer->tail];
191-
buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
146+
if (peek_buffer >= 0) {
147+
int c = peek_buffer;
148+
peek_buffer = -1;
192149
return c;
193-
}
150+
}
151+
return USB_Recv(CDC_RX);
194152
}
195153

196154
void Serial_::flush(void)

arduino/cores/arduino/USBAPI.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ extern USBDevice_ USBDevice;
2828
class Serial_ : public Stream
2929
{
3030
private:
31-
ring_buffer *_cdc_rx_buffer;
31+
int peek_buffer;
3232
public:
3333
void begin(unsigned long);
3434
void begin(unsigned long, uint8_t);
3535
void end(void);
3636

3737
virtual int available(void);
38-
virtual void accept(void);
3938
virtual int peek(void);
4039
virtual int read(void);
4140
virtual void flush(void);

arduino/cores/arduino/USBCore.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,6 @@ ISR(USB_GEN_vect)
614614
{
615615
#ifdef CDC_ENABLED
616616
USB_Flush(CDC_TX); // Send a tx frame if found
617-
if (USB_Available(CDC_RX)) // Handle received bytes (if any)
618-
Serial.accept();
619617
#endif
620618

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

0 commit comments

Comments
 (0)