Skip to content

Serial1.flush() blocks if called after Serial1.begin() and before port is written to. #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nsted opened this issue Apr 27, 2016 · 4 comments
Milestone

Comments

@nsted
Copy link

nsted commented Apr 27, 2016

Caused problems while working on RS485 project on Arduino Zero. I flushed the port when switching between RX and TX mode, including at startup (ie before data is sent), which caused the program to block.


Test:

void setup() {
  Serial1.begin(9600); 
  SerialUSB.begin(9600);

//  Serial1.write(1);           // uncomment to prevent blocking

  SerialUSB.flush();            // doesn't block
  Serial1.flush();              // blocks everything below if called after Serial1.begin() and before any data sent.

  delay(2000);
  Serial1.println("Serial1 up and running");  
  SerialUSB.println("SerialUSB up and running");
  delay(3000);
}

int i;

void loop() {
  // put your main code here, to run repeatedly:
  Serial1.println(i++);
  SerialUSB.println(i);
}

The issue appears to be tied to this method:

void SERCOM::flushUART()
{
  // Wait for transmission to complete
  while(!sercom->USART.INTFLAG.bit.TXC);
}

@rocketscream
Copy link

The only way to avoid this would be checking the data register empty bit before the while loop:

void SERCOM::flushUART()
{
  if(isDataRegisterEmptyUART()) return;
  // Wait for transmission to complete
  while(!sercom->USART.INTFLAG.bit.TXC);
}

@sandeepmistry
Copy link
Contributor

I agreed with @rocketscream's suggestion above.

@rocketscream would you be interested in submitting a pull request?

@rocketscream
Copy link

@sandeepmistry Okay, did a pull request #144

@sandeepmistry
Copy link
Contributor

Closed via @rocketscream's change in bfd2a0b.

@sandeepmistry sandeepmistry added this to the Release 1.6.7 milestone Jul 19, 2016
boseji pushed a commit to go-ut/combined-ArduinoCore-samd that referenced this issue May 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants