Skip to content

Commit 19ae0eb

Browse files
committedAug 23, 2016
USB-CDC: access to rx buffer is now ISR-protected
1 parent 9f678cb commit 19ae0eb

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed
 

‎cores/arduino/USB/CDC.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,24 @@ int Serial_::read(void)
214214
{
215215
ring_buffer *buffer = &cdc_rx_buffer;
216216

217+
uint8_t enableInterrupts = ((__get_PRIMASK() & 0x1) == 0);
218+
__disable_irq();
219+
217220
// if we have enough space enable OUT endpoint to receive more data
218221
if (stalled && availableForStore() >= EPX_SIZE)
219222
{
220223
stalled = false;
221224
usb.epOut(CDC_ENDPOINT_OUT);
222225
}
223-
if (buffer->head == buffer->tail && !buffer->full)
224-
{
225-
return -1;
226-
}
227-
else
226+
int c = -1;
227+
if (buffer->head != buffer->tail || buffer->full)
228228
{
229-
unsigned char c = buffer->buffer[buffer->tail];
229+
c = buffer->buffer[buffer->tail];
230230
buffer->tail = (uint32_t)(buffer->tail + 1) % CDC_SERIAL_BUFFER_SIZE;
231231
buffer->full = false;
232-
233-
return c;
234232
}
233+
if (enableInterrupts) __enable_irq();
234+
return c;
235235
}
236236

237237
void Serial_::flush(void)

0 commit comments

Comments
 (0)
Please sign in to comment.