Skip to content

Commit

Permalink
Merge pull request #652 from facchinm/fix_flush_locks_on_startup
Browse files Browse the repository at this point in the history
fix Serial.flush() blocks forever #597
  • Loading branch information
facchinm authored Oct 20, 2021
2 parents 3b9a5e7 + b0ba1ee commit a5d52db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
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

0 comments on commit a5d52db

Please sign in to comment.