Skip to content

Commit 5f56e04

Browse files
d00616sandeepmistry
authored andcommitted
Add UART hardware flow control (sandeepmistry#48)
* Add UART hardware flow control option
1 parent cf97118 commit 5f56e04

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

cores/nRF5/Uart.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi
2727
IRQn = _IRQn;
2828
uc_pinRX = g_ADigitalPinMap[_pinRX];
2929
uc_pinTX = g_ADigitalPinMap[_pinTX];
30+
uc_hwFlow = 0;
31+
}
32+
33+
Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS)
34+
{
35+
nrfUart = _nrfUart;
36+
IRQn = _IRQn;
37+
uc_pinRX = g_ADigitalPinMap[_pinRX];
38+
uc_pinTX = g_ADigitalPinMap[_pinTX];
39+
uc_pinCTS = g_ADigitalPinMap[_pinCTS];
40+
uc_pinRTS = g_ADigitalPinMap[_pinRTS];
41+
uc_hwFlow = 1;
3042
}
3143

3244
void Uart::begin(unsigned long baudrate)
@@ -39,7 +51,14 @@ void Uart::begin(unsigned long baudrate, uint16_t /*config*/)
3951
nrfUart->PSELTXD = uc_pinTX;
4052
nrfUart->PSELRXD = uc_pinRX;
4153

42-
nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
54+
if (uc_hwFlow == 1) {
55+
nrfUart->PSELCTS = uc_pinCTS;
56+
nrfUart->PSELRTS = uc_pinRTS;
57+
nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Enabled;
58+
} else {
59+
nrfUart->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
60+
}
61+
4362

4463
uint32_t nrfBaudRate;
4564

@@ -144,6 +163,9 @@ void Uart::end()
144163
nrfUart->PSELTXD = 0xFFFFFFFF;
145164
nrfUart->PSELRXD = 0xFFFFFFFF;
146165

166+
nrfUart->PSELRTS = 0xFFFFFFFF;
167+
nrfUart->PSELCTS = 0xFFFFFFFF;
168+
147169
rxBuffer.clear();
148170
}
149171

@@ -188,8 +210,18 @@ size_t Uart::write(const uint8_t data)
188210
}
189211

190212
#if defined(NRF52)
191-
Uart Serial( NRF_UART0, UARTE0_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
213+
#define NRF_UART0_IRQn UARTE0_UART0_IRQn
214+
#elif defined(NRF51)
215+
#define NRF_UART0_IRQn UART0_IRQn
216+
#endif
192217

218+
#if defined(PIN_SERIAL_CTS) && defined(PIN_SERIAL_RTS)
219+
Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX, PIN_SERIAL_CTS, PIN_SERIAL_RTS );
220+
#else
221+
Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
222+
#endif
223+
224+
#if defined(NRF52)
193225
extern "C"
194226
{
195227
void UARTE0_UART0_IRQHandler()
@@ -198,8 +230,6 @@ extern "C"
198230
}
199231
}
200232
#elif defined(NRF51)
201-
Uart Serial( NRF_UART0, UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
202-
203233
extern "C"
204234
{
205235
void UART0_IRQHandler()

cores/nRF5/Uart.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Uart : public HardwareSerial
3030
{
3131
public:
3232
Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX);
33+
Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pinTX, uint8_t _pinCTS, uint8_t _pinRTS );
3334
void begin(unsigned long baudRate);
3435
void begin(unsigned long baudrate, uint16_t config);
3536
void end();
@@ -52,6 +53,9 @@ class Uart : public HardwareSerial
5253

5354
uint8_t uc_pinRX;
5455
uint8_t uc_pinTX;
56+
uint8_t uc_pinCTS;
57+
uint8_t uc_pinRTS;
58+
uint8_t uc_hwFlow;
5559
};
5660

5761
#ifdef __cplusplus

0 commit comments

Comments
 (0)