Skip to content

Commit 1f59c5a

Browse files
authored
Adds HardwareSerial::setRxBufferSize() (#5583)
* Adds rxBufferSize parameter to begin() * Adds HardwareSerial::setRXBufferSize()
1 parent 0730e0e commit 1f59c5a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void serialEventRun(void)
103103
}
104104

105105

106-
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
106+
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256) {}
107107

108108
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd)
109109
{
@@ -133,7 +133,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
133133
}
134134
#endif
135135

136-
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
136+
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
137137
if (!baud) {
138138
// using baud rate as zero, forces it to try to detect the current baud rate in place
139139
uartStartDetectBaudrate(_uart);
@@ -147,7 +147,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
147147

148148
if(detectedBaudRate) {
149149
delay(100); // Give some time...
150-
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd);
150+
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
151151
} else {
152152
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
153153
_uart = NULL;
@@ -268,3 +268,18 @@ void HardwareSerial::setPins(uint8_t rxPin, uint8_t txPin)
268268
uartSetPins(_uart, rxPin, txPin);
269269
}
270270

271+
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
272+
273+
if (_uart) {
274+
log_e("RX Buffer can't be resized when Serial is already running.\n");
275+
return 0;
276+
}
277+
278+
if (new_size <= SOC_UART_FIFO_LEN) {
279+
log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN);
280+
return 0;
281+
}
282+
283+
_rxBufferSize = new_size;
284+
return _rxBufferSize;
285+
}

Diff for: cores/esp32/HardwareSerial.h

+2
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ class HardwareSerial: public Stream
103103

104104
void setRxInvert(bool);
105105
void setPins(uint8_t rxPin, uint8_t txPin);
106+
size_t setRxBufferSize(size_t new_size);
106107

107108
protected:
108109
int _uart_nr;
109110
uart_t* _uart;
111+
size_t _rxBufferSize;
110112
};
111113

112114
extern void serialEventRun(void) __attribute__((weak));

0 commit comments

Comments
 (0)