diff --git a/cores/arduino/CDCSerialClass.cpp b/cores/arduino/CDCSerialClass.cpp index 9e5c4870..4e5b93d0 100644 --- a/cores/arduino/CDCSerialClass.cpp +++ b/cores/arduino/CDCSerialClass.cpp @@ -30,7 +30,6 @@ #include "wiring_digital.h" #include "variant.h" -#define CDCACM_FIXED_DELAY 120 extern void CDCSerial_Handler(void); extern void serialEventRun1(void) __attribute__((weak)); @@ -133,7 +132,7 @@ void CDCSerialClass::flush( void ) size_t CDCSerialClass::write( const uint8_t uc_data ) { - uint32_t retries = 2; + uint32_t retries = 1; if (!_shared_data->device_open || !_shared_data->host_open) return(0); @@ -145,11 +144,12 @@ size_t CDCSerialClass::write( const uint8_t uc_data ) // current location of the tail), we're about to overflow the buffer // and so we don't write the character or advance the head. if (i != _tx_buffer->tail) { + _tx_buffer->lock = 1; _tx_buffer->data[_tx_buffer->head] = uc_data; _tx_buffer->head = i; - - // Just use a fixed delay to make it compatible with the CODK-M based firmware - delayMicroseconds(CDCACM_FIXED_DELAY); + _tx_buffer->lock = 0; + // Just use a fixed delay to make it compatible with the CODK-M based firmware + delayMicroseconds(_writeDelayUsec); break; } } while (retries--); diff --git a/system/libarc32_arduino101/framework/include/platform.h b/system/libarc32_arduino101/framework/include/platform.h index 801f87f5..428d90cd 100644 --- a/system/libarc32_arduino101/framework/include/platform.h +++ b/system/libarc32_arduino101/framework/include/platform.h @@ -45,11 +45,12 @@ struct cdc_ring_buffer { /** Ring buffer data */ - uint8_t data[CDCACM_BUFFER_SIZE]; + volatile uint8_t data[CDCACM_BUFFER_SIZE]; /** Ring buffer head pointer, modified by producer */ - int head; + volatile int head; /** Ring buffer head pointer, modified by consumer */ - int tail; + volatile int tail; + volatile uint8_t lock; }; struct cdc_acm_shared_data {