Skip to content

Commit 49fe438

Browse files
committed
Uart: fix acknowledge of UART errors
This fix lock-ups on UART errors (for example when disconnecting and reconnecting RX/TX wires or if the method Serial.begin is called while another device is already transmitting).
1 parent ea59a7b commit 49fe438

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

cores/arduino/SERCOM.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ SERCOM::SERCOM(Sercom* s)
3030
*/
3131
void SERCOM::initUART(SercomUartMode mode, SercomUartSampleRate sampleRate, uint32_t baudrate)
3232
{
33-
resetUART();
3433
initClockNVIC();
34+
resetUART();
3535

3636
//Setting the CTRLA register
3737
sercom->USART.CTRLA.reg = SERCOM_USART_CTRLA_MODE(mode) |
@@ -127,6 +127,16 @@ bool SERCOM::availableDataUART()
127127
return sercom->USART.INTFLAG.bit.RXC;
128128
}
129129

130+
bool SERCOM::isUARTError()
131+
{
132+
return sercom->USART.INTFLAG.bit.ERROR;
133+
}
134+
135+
void SERCOM::acknowledgeUARTError()
136+
{
137+
sercom->USART.INTFLAG.bit.ERROR = 1;
138+
}
139+
130140
bool SERCOM::isBufferOverflowErrorUART()
131141
{
132142
//BUFOVF : Buffer Overflow

cores/arduino/SERCOM.h

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ class SERCOM
162162
bool isDataRegisterEmptyUART( void ) ;
163163
uint8_t readDataUART( void ) ;
164164
int writeDataUART(uint8_t data) ;
165+
bool isUARTError() ;
166+
void acknowledgeUARTError() ;
165167

166168
/* ========== SPI ========== */
167169
void initSPI(SercomSpiTXPad mosi, SercomRXPad miso, SercomSpiCharSize charSize, SercomDataOrder dataOrder) ;

cores/arduino/Uart.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ void Uart::flush()
5959

6060
void Uart::IrqHandler()
6161
{
62-
if(sercom->availableDataUART())
63-
{
62+
if (sercom->availableDataUART()) {
6463
rxBuffer.store_char(sercom->readDataUART());
6564
}
6665

67-
if( sercom->isBufferOverflowErrorUART() ||
68-
sercom->isFrameErrorUART() ||
69-
sercom->isParityErrorUART())
70-
{
66+
if (sercom->isUARTError()) {
67+
sercom->acknowledgeUARTError();
68+
// TODO: if (sercom->isBufferOverflowErrorUART()) ....
69+
// TODO: if (sercom->isFrameErrorUART()) ....
70+
// TODO: if (sercom->isParityErrorUART()) ....
7171
sercom->clearStatusUART();
7272
}
7373
}

0 commit comments

Comments
 (0)