diff --git a/cores/arduino/SERCOM.cpp b/cores/arduino/SERCOM.cpp index 03ecea7ea..20adf51c7 100644 --- a/cores/arduino/SERCOM.cpp +++ b/cores/arduino/SERCOM.cpp @@ -112,11 +112,10 @@ void SERCOM::enableUART() void SERCOM::flushUART() { // Skip checking transmission completion if data register is empty -// if(isDataRegisterEmptyUART()) -// return; + // Wait for transmission to complete, if ok to do so. + while(!sercom->USART.INTFLAG.bit.TXC && onFlushWaitUartTXC); - // Wait for transmission to complete - while(!sercom->USART.INTFLAG.bit.TXC); + onFlushWaitUartTXC = false; } void SERCOM::clearStatusUART() @@ -183,6 +182,10 @@ int SERCOM::writeDataUART(uint8_t data) //Put data into DATA register sercom->USART.DATA.reg = (uint16_t)data; + + // indicate it's ok to wait for TXC flag when flushing + onFlushWaitUartTXC = true; + return 1; } diff --git a/cores/arduino/SERCOM.h b/cores/arduino/SERCOM.h index cbc3a05de..80b54de5b 100644 --- a/cores/arduino/SERCOM.h +++ b/cores/arduino/SERCOM.h @@ -218,6 +218,11 @@ class SERCOM uint8_t calculateBaudrateSynchronous(uint32_t baudrate) ; uint32_t division(uint32_t dividend, uint32_t divisor) ; void initClockNVIC( void ) ; + + // Flag set when data is loaded into sercom->USART.DATA.reg. + // Helps with preventing UART lockups when flushing on startup + // and the asyncronous nature of the DRE and TXC interrupt flags. + bool onFlushWaitUartTXC = false; }; #endif