Skip to content

Commit a5d52db

Browse files
authored
Merge pull request #652 from facchinm/fix_flush_locks_on_startup
fix Serial.flush() blocks forever #597
2 parents 3b9a5e7 + b0ba1ee commit a5d52db

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

cores/arduino/SERCOM.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,10 @@ void SERCOM::enableUART()
112112
void SERCOM::flushUART()
113113
{
114114
// Skip checking transmission completion if data register is empty
115-
// if(isDataRegisterEmptyUART())
116-
// return;
115+
// Wait for transmission to complete, if ok to do so.
116+
while(!sercom->USART.INTFLAG.bit.TXC && onFlushWaitUartTXC);
117117

118-
// Wait for transmission to complete
119-
while(!sercom->USART.INTFLAG.bit.TXC);
118+
onFlushWaitUartTXC = false;
120119
}
121120

122121
void SERCOM::clearStatusUART()
@@ -183,6 +182,10 @@ int SERCOM::writeDataUART(uint8_t data)
183182

184183
//Put data into DATA register
185184
sercom->USART.DATA.reg = (uint16_t)data;
185+
186+
// indicate it's ok to wait for TXC flag when flushing
187+
onFlushWaitUartTXC = true;
188+
186189
return 1;
187190
}
188191

cores/arduino/SERCOM.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ class SERCOM
218218
uint8_t calculateBaudrateSynchronous(uint32_t baudrate) ;
219219
uint32_t division(uint32_t dividend, uint32_t divisor) ;
220220
void initClockNVIC( void ) ;
221+
222+
// Flag set when data is loaded into sercom->USART.DATA.reg.
223+
// Helps with preventing UART lockups when flushing on startup
224+
// and the asyncronous nature of the DRE and TXC interrupt flags.
225+
bool onFlushWaitUartTXC = false;
221226
};
222227

223228
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy