Skip to content

Serial.flush() blocks forever #597

Closed
@jpmeijers

Description

@jpmeijers

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:

// if(isDataRegisterEmptyUART())
// return;

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions