diff --git a/cores/arduino/USB/CDC.cpp b/cores/arduino/USB/CDC.cpp index 6ae5e6581b8968cd972594c6dbc379e6c3732c8b..e6fc4a5bad496a9046c611612d0ddbc495bbbee7 100644 --- a/cores/arduino/USB/CDC.cpp +++ b/cores/arduino/USB/CDC.cpp @@ -214,24 +214,24 @@ int Serial_::read(void) { ring_buffer *buffer = &cdc_rx_buffer; + uint8_t enableInterrupts = ((__get_PRIMASK() & 0x1) == 0); + __disable_irq(); + // if we have enough space enable OUT endpoint to receive more data if (stalled && availableForStore() >= EPX_SIZE) { stalled = false; usb.epOut(CDC_ENDPOINT_OUT); } - if (buffer->head == buffer->tail && !buffer->full) - { - return -1; - } - else + int c = -1; + if (buffer->head != buffer->tail || buffer->full) { - unsigned char c = buffer->buffer[buffer->tail]; + c = buffer->buffer[buffer->tail]; buffer->tail = (uint32_t)(buffer->tail + 1) % CDC_SERIAL_BUFFER_SIZE; buffer->full = false; - - return c; } + if (enableInterrupts) __enable_irq(); + return c; } void Serial_::flush(void)