Skip to content

fix Serial.flush() blocks forever #597 #652

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

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cores/arduino/SERCOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions cores/arduino/SERCOM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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