Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4d16d26

Browse files
committedAug 23, 2021
Adds HardwareSerial::setRXBufferSize()
1 parent 288dd44 commit 4d16d26

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed
 

‎cores/esp32/HardwareSerial.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ 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

108-
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, size_t rxBufferSize)
108+
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
{
110110
if(0 > _uart_nr || _uart_nr >= SOC_UART_NUM) {
111111
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
@@ -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, rxBufferSize, 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, rxBufferSize, 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+
}

‎cores/esp32/HardwareSerial.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class HardwareSerial: public Stream
5656
public:
5757
HardwareSerial(int uart_nr);
5858

59-
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112, size_t rxBufferSize = 256);
59+
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112);
6060
void end(bool turnOffDebug = true);
6161
void updateBaudRate(unsigned long baud);
6262
int available(void);
@@ -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)
Please sign in to comment.