Skip to content

Commit 2e70fc8

Browse files
committed
adds Hardware Flow Control mode and CTS/RTS pin setting
1 parent 56da126 commit 2e70fc8

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ void HardwareSerial::setAllPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_
301301
uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin);
302302
}
303303

304+
// Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
305+
void HardwareSerial::setHwFlowCtrlMode(uint8_t mode, uint8_t threshold)
306+
{
307+
uartSetHwFlowCtrlMode(_uart, mode, threshold);
308+
}
309+
304310
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
305311

306312
if (_uart) {

cores/esp32/HardwareSerial.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ class HardwareSerial: public Stream
110110

111111
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
112112
// SetPins shall be called after Serial begin()
113-
void setPins(int8_t rxPin, int8_t txPin);
114-
void setAllPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
115-
113+
void setPins(int8_t rxPin = -1, int8_t txPin = -1);
114+
void setAllPins(int8_t rxPin = -1, int8_t txPin = -1, int8_t ctsPin = -1, int8_t rtsPin = -1);
115+
// Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
116+
void setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length
117+
116118
size_t setRxBufferSize(size_t new_size);
117119

118120
protected:

cores/esp32/esp32-hal-uart.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ void uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t
168168
UART_MUTEX_UNLOCK();
169169
}
170170

171+
//
172+
void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold) {
173+
if(uart == NULL) {
174+
return;
175+
}
176+
// IDF will issue corresponding error message when mode or threshold are wrong and prevent crashing
177+
// IDF will check (mode > HW_FLOWCTRL_CTS_RTS || threshold >= SOC_UART_FIFO_LEN)
178+
uart_set_hw_flow_ctrl(uart->num, (uart_hw_flowcontrol_t) mode, threshold);
179+
}
180+
171181

172182
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd)
173183
{

cores/esp32/esp32-hal-uart.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ extern "C" {
4848
#define SERIAL_7O2 0x800003b
4949
#define SERIAL_8O2 0x800003f
5050

51+
// These are Hardware Flow Contol possible usage
52+
// equivalent to UDF enum uart_hw_flowcontrol_t from
53+
// https://github.com/espressif/esp-idf/blob/master/components/hal/include/hal/uart_types.h#L75-L81
54+
#define HW_FLOWCTRL_DISABLE 0x0 // disable HW Flow Control
55+
#define HW_FLOWCTRL_RTS 0x1 // use only RTS PIN for HW Flow Control
56+
#define HW_FLOWCTRL_CTS 0x2 // use only CTS PIN for HW Flow Control
57+
#define HW_FLOWCTRL_CTS_RTS 0x3 // use both CTS and RTS PIN for HW Flow Control
58+
5159
struct uart_struct_t;
5260
typedef struct uart_struct_t uart_t;
5361

@@ -80,6 +88,9 @@ bool uartIsDriverInstalled(uart_t* uart);
8088
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
8189
void uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin);
8290

91+
// Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins
92+
void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold);
93+
8394
void uartStartDetectBaudrate(uart_t *uart);
8495
unsigned long uartDetectBaudrate(uart_t *uart);
8596

0 commit comments

Comments
 (0)