Skip to content

Commit 58b9f07

Browse files
authored
HardwareSerial Available For Write (#9319)
* 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. * feat: sets TX buffer This will allow to set TX buffer to a minimum with no error message. It also makes it works as in the Arduino API specification that is to return the buffer available space. In ESP32 case it will be the minmum the HW TX FIFO size of 128 bytes. * feat: adjust availableForWrite This change will make sure that if no TX Ringbuffer is used, it will return the UART FIFO available space. Otherwise, it will return the Ringbuffer available space, as defined in the Arduino especification.
1 parent aed7b4f commit 58b9f07

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -537,35 +537,37 @@ 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

563565
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;
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-
570+
// if new_size is higher than SOC_UART_FIFO_LEN, TX Ringbuffer will be active and it will be used to report back "availableToWrite()"
568571
_txBufferSize = new_size;
569-
return _txBufferSize;
572+
return new_size;
570573
}
571-

Diff for: cores/esp32/esp32-hal-uart.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ uint32_t uartAvailableForWrite(uart_t* uart)
642642
uint32_t available = uart_ll_get_txfifo_len(UART_LL_GET_HW(uart->num));
643643
size_t txRingBufferAvailable = 0;
644644
if (ESP_OK == uart_get_tx_buffer_free_size(uart->num, &txRingBufferAvailable)) {
645-
available += txRingBufferAvailable;
645+
available = txRingBufferAvailable == 0 ? available : txRingBufferAvailable;
646646
}
647647
UART_MUTEX_UNLOCK();
648648
return available;

0 commit comments

Comments
 (0)