Skip to content

Commit 7fc402f

Browse files
sandeepmistrycmaglie
authored andcommitted
Leverage SERCOM h/w functionality for RTS and CTS
1 parent bd1bd60 commit 7fc402f

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

cores/arduino/SERCOM.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ void SERCOM::disableDataRegisterEmptyInterruptUART()
183183
sercom->USART.INTENCLR.reg = SERCOM_USART_INTENCLR_DRE;
184184
}
185185

186+
void SERCOM::enableReceiveCompleteInterruptUART()
187+
{
188+
sercom->USART.INTENSET.reg |= SERCOM_USART_INTENSET_RXC;
189+
}
190+
191+
void SERCOM::disableReceiveCompleteInterruptUART()
192+
{
193+
sercom->USART.INTENCLR.reg = SERCOM_USART_INTENCLR_RXC;
194+
}
195+
186196
/* =========================
187197
* ===== Sercom SPI
188198
* =========================

cores/arduino/SERCOM.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class SERCOM
165165
void acknowledgeUARTError() ;
166166
void enableDataRegisterEmptyInterruptUART();
167167
void disableDataRegisterEmptyInterruptUART();
168+
void enableReceiveCompleteInterruptUART();
169+
void disableReceiveCompleteInterruptUART();
168170

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

cores/arduino/Uart.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ void Uart::begin(unsigned long baudrate, uint16_t config)
4646
pinPeripheral(uc_pinTX, g_APinDescription[uc_pinTX].ulPinType);
4747

4848
if (uc_pinRTS != NO_RTS_PIN) {
49-
pinMode(uc_pinRTS, OUTPUT);
50-
digitalWrite(uc_pinRTS, LOW);
49+
pinPeripheral(uc_pinRTS, g_APinDescription[uc_pinRTS].ulPinType);
5150
}
5251

5352
if (uc_pinCTS != NO_CTS_PIN) {
54-
pinMode(uc_pinCTS, INPUT);
53+
pinPeripheral(uc_pinCTS, g_APinDescription[uc_pinCTS].ulPinType);
5554
}
5655

5756
sercom->initUART(UART_INT_CLOCK, SAMPLE_RATE_x16, baudrate);
@@ -86,9 +85,10 @@ void Uart::IrqHandler()
8685
rxBuffer.store_char(sercom->readDataUART());
8786

8887
if (uc_pinRTS != NO_RTS_PIN) {
89-
// if there is NOT enough space in the RX buffer, de-assert RTS
88+
// if there is NOT enough space in the RX buffer,
89+
// diable the receive complete interrupt
9090
if (rxBuffer.availableForStore() < RTS_RX_THRESHOLD) {
91-
digitalWrite(uc_pinRTS, HIGH);
91+
sercom->disableReceiveCompleteInterruptUART();
9292
}
9393
}
9494
}
@@ -132,9 +132,10 @@ int Uart::read()
132132
int c = rxBuffer.read_char();
133133

134134
if (uc_pinRTS != NO_RTS_PIN) {
135-
// if there is enough space in the RX buffer, assert RTS
135+
// if there is enough space in the RX buffer,
136+
// enable the receive completer interrupt
136137
if (rxBuffer.availableForStore() > RTS_RX_THRESHOLD) {
137-
digitalWrite(uc_pinRTS, LOW);
138+
sercom->enableReceiveCompleteInterruptUART();
138139
}
139140
}
140141

@@ -143,16 +144,6 @@ int Uart::read()
143144

144145
size_t Uart::write(const uint8_t data)
145146
{
146-
if (uc_pinRTS != NO_RTS_PIN) {
147-
// assert RTS
148-
digitalWrite(uc_pinRTS, LOW);
149-
}
150-
151-
if (uc_pinCTS != NO_CTS_PIN) {
152-
// wait until CTS is asserted
153-
while (digitalRead(uc_pinCTS) != LOW);
154-
}
155-
156147
if (sercom->isDataRegisterEmptyUART() && txBuffer.available() == 0) {
157148
sercom->writeDataUART(data);
158149
} else {
@@ -217,14 +208,22 @@ SercomParityMode Uart::extractParity(uint16_t config)
217208

218209
int Uart::attachRts(uint8_t pin)
219210
{
220-
uc_pinRTS = pin;
211+
if (uc_padTX == UART_TX_RTS_CTS_PAD_0_2_3) {
212+
uc_pinRTS = pin;
221213

222-
return 1;
214+
return 1;
215+
}
216+
217+
return 0;
223218
}
224219

225220
int Uart::attachCts(uint8_t pin)
226221
{
227-
uc_pinCTS = pin;
222+
if (uc_padTX == UART_TX_RTS_CTS_PAD_0_2_3) {
223+
uc_pinCTS = pin;
228224

229-
return 1;
225+
return 1;
226+
}
227+
228+
return 0;
230229
}

0 commit comments

Comments
 (0)