Adds HardwareSerial::setRxBufferSize() (#5583)

* Adds rxBufferSize parameter to begin()

* Adds HardwareSerial::setRXBufferSize()
This commit is contained in:
Rodrigo Garcia 2021-08-24 02:21:20 -03:00 committed by GitHub
parent 0730e0ec93
commit 1f59c5abec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -103,7 +103,7 @@ void serialEventRun(void)
} }
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {} HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256) {}
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) 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)
{ {
@ -133,7 +133,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
} }
#endif #endif
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd); _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
if (!baud) { if (!baud) {
// using baud rate as zero, forces it to try to detect the current baud rate in place // using baud rate as zero, forces it to try to detect the current baud rate in place
uartStartDetectBaudrate(_uart); uartStartDetectBaudrate(_uart);
@ -147,7 +147,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
if(detectedBaudRate) { if(detectedBaudRate) {
delay(100); // Give some time... delay(100); // Give some time...
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert, rxfifo_full_thrhd); _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
} else { } else {
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
_uart = NULL; _uart = NULL;
@ -268,3 +268,18 @@ void HardwareSerial::setPins(uint8_t rxPin, uint8_t txPin)
uartSetPins(_uart, rxPin, txPin); uartSetPins(_uart, rxPin, txPin);
} }
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
if (_uart) {
log_e("RX Buffer can't be resized when Serial is already running.\n");
return 0;
}
if (new_size <= SOC_UART_FIFO_LEN) {
log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN);
return 0;
}
_rxBufferSize = new_size;
return _rxBufferSize;
}

View File

@ -103,10 +103,12 @@ public:
void setRxInvert(bool); void setRxInvert(bool);
void setPins(uint8_t rxPin, uint8_t txPin); void setPins(uint8_t rxPin, uint8_t txPin);
size_t setRxBufferSize(size_t new_size);
protected: protected:
int _uart_nr; int _uart_nr;
uart_t* _uart; uart_t* _uart;
size_t _rxBufferSize;
}; };
extern void serialEventRun(void) __attribute__((weak)); extern void serialEventRun(void) __attribute__((weak));