Skip to content

Commit 3d75161

Browse files
authored
feat(uart): setBufferSize
It makes sure that setting TX buffer size will match availableForWrite() response. It also sets the buffer to the minimum instead of doing nothing and returning an error. For RX Buffer, it sets the minimum and also don't return an error. This makes the APIs better and easy to understand its results.
1 parent aed7b4f commit 3d75161

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -537,35 +537,38 @@ bool HardwareSerial::setMode(SerialMode mode)
537537
return uartSetMode(_uart, mode);
538538
}
539539

540+
// minimum total RX Buffer size is the UART FIFO space (128 bytes for most SoC) + 1. IDF imposition.
540541
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
541542

542543
if (_uart) {
543-
log_e("RX Buffer can't be resized when Serial is already running.\n");
544+
log_e("RX Buffer can't be resized when Serial is already running. Set it before calling begin().");
544545
return 0;
545546
}
546547

547548
if (new_size <= SOC_UART_FIFO_LEN) {
548-
log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
549-
return 0;
549+
log_w("RX Buffer set to minimum value: %d.", SOC_UART_FIFO_LEN + 1); // ESP32, S2, S3 and C3 means higher than 128
550+
new_size = SOC_UART_FIFO_LEN + 1;
550551
}
551552

552553
_rxBufferSize = new_size;
553554
return _rxBufferSize;
554555
}
555556

557+
// minimum total TX Buffer size is the UART FIFO space (128 bytes for most SoC).
556558
size_t HardwareSerial::setTxBufferSize(size_t new_size) {
557559

558560
if (_uart) {
559-
log_e("TX Buffer can't be resized when Serial is already running.\n");
561+
log_e("TX Buffer can't be resized when Serial is already running. Set it before calling begin().");
560562
return 0;
561563
}
562564

563-
if (new_size <= SOC_UART_FIFO_LEN) {
564-
log_e("TX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
565-
return 0;
565+
if (new_size < SOC_UART_FIFO_LEN) {
566+
log_w("TX Buffer set to minimum value: %d.", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
567+
_txBufferSize = 0; // it will use just UART FIFO with SOC_UART_FIFO_LEN bytes (128 for most SoC)
568+
return SOC_UART_FIFO_LEN;
566569
}
567-
568-
_txBufferSize = new_size;
569-
return _txBufferSize;
570+
// if new_size is SOC_UART_FIFO_LEN, _txBufferSize will be zero - just use the UART FIFO space
571+
_txBufferSize = new_size - SOC_UART_FIFO_LEN; // for total correct report from "availableForWrite()" that matches a call to this function
572+
return new_size;
570573
}
571574

0 commit comments

Comments
 (0)