Skip to content

Commit 3ddf463

Browse files
committed
USBDevice: added 'full' flag to USB buffer
This way all the 64 bytes available can be filled up
1 parent 44174a0 commit 3ddf463

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

cores/arduino/USB/CDC.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ struct ring_buffer {
3737
uint8_t buffer[CDC_SERIAL_BUFFER_SIZE];
3838
volatile uint32_t head;
3939
volatile uint32_t tail;
40+
volatile bool full;
4041
};
41-
ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
42+
ring_buffer cdc_rx_buffer = {{0}, 0, 0, false};
4243

4344
typedef struct {
4445
uint32_t dwDTERate;
@@ -160,19 +161,22 @@ void Serial_::accept(void)
160161
// current location of the tail), we're about to overflow the buffer
161162
// and so we don't write the character or advance the head.
162163
uint32_t k = 0;
163-
i = (i + 1) % CDC_SERIAL_BUFFER_SIZE;
164-
while ((i != ringBuffer->tail) && (len > 0)) {
164+
while (len > 0 && !ringBuffer->full) {
165165
len--;
166-
ringBuffer->buffer[ringBuffer->head] = buffer[k++];
167-
ringBuffer->head = i;
168-
i = (i + 1) % CDC_SERIAL_BUFFER_SIZE;
166+
ringBuffer->buffer[i++] = buffer[k++];
167+
i %= CDC_SERIAL_BUFFER_SIZE;
168+
if (i == ringBuffer->tail)
169+
ringBuffer->full = true;
169170
}
171+
ringBuffer->head = i;
170172
interrupts();
171173
}
172174

173175
int Serial_::available(void)
174176
{
175177
ring_buffer *buffer = &cdc_rx_buffer;
178+
if (buffer->full)
179+
return CDC_SERIAL_BUFFER_SIZE;
176180
if (buffer->head == buffer->tail) {
177181
USB->DEVICE.DeviceEndpoint[2].EPINTENSET.reg = USB_DEVICE_EPINTENCLR_TRCPT(1);
178182
}
@@ -182,13 +186,9 @@ int Serial_::available(void)
182186
int Serial_::peek(void)
183187
{
184188
ring_buffer *buffer = &cdc_rx_buffer;
185-
186-
if (buffer->head == buffer->tail)
187-
{
189+
if (buffer->head == buffer->tail && !buffer->full) {
188190
return -1;
189-
}
190-
else
191-
{
191+
} else {
192192
return buffer->buffer[buffer->tail];
193193
}
194194
}
@@ -203,19 +203,20 @@ int Serial_::read(void)
203203
ring_buffer *buffer = &cdc_rx_buffer;
204204

205205
// if the head isn't ahead of the tail, we don't have any characters
206-
if (buffer->head == buffer->tail)
206+
if (buffer->head == buffer->tail && !buffer->full)
207207
{
208208
if (usb.available(CDC_ENDPOINT_OUT))
209209
accept();
210210
}
211-
if (buffer->head == buffer->tail)
211+
if (buffer->head == buffer->tail && !buffer->full)
212212
{
213213
return -1;
214214
}
215215
else
216216
{
217217
unsigned char c = buffer->buffer[buffer->tail];
218218
buffer->tail = (uint32_t)(buffer->tail + 1) % CDC_SERIAL_BUFFER_SIZE;
219+
buffer->full = false;
219220
// if (usb.available(CDC_ENDPOINT_OUT))
220221
// accept();
221222
return c;

0 commit comments

Comments
 (0)