Closed
Description
Using the latest version of the SAMD core, doing a Serial.flush() will hang forever.
Problem can be reproduced using this sketch:
void setup() {
SerialUSB.begin(115200);
delay(2000);
SerialUSB.println("Start");
Serial.begin(9600);
Serial.flush();
SerialUSB.println("Setup end");
}
void loop() {
SerialUSB.println("Loop");
delay(1000);
}
Which will only print:
15:19:30.572 -> Start
Tracing the issue down takes me to Serial.flush()
, which is defined in Uart.cpp. This calls sercom->flushUART();
in SERCOM.cpp.
The previous versions of this core had the following code:
void SERCOM::flushUART()
{
// Skip checking transmission completion if data register is empty
if(isDataRegisterEmptyUART())
return;
// Wait for transmission to complete
while(!sercom->USART.INTFLAG.bit.TXC);
}
The current version has the if(isDataRegisterEmptyUART())
commented out:
ArduinoCore-samd/cores/arduino/SERCOM.cpp
Lines 115 to 116 in 0b60a79
I don't understand why this is commented out, as clearly the while(!sercom->USART.INTFLAG.bit.TXC);
will never return if flush()
is called when the TX buffer is already empty.