Skip to content

HardwareSerial Available For Write #9319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
SuGlider authored Mar 1, 2024
commit 3d75161535b6a47429294c2b3210bc203bc4ae70
23 changes: 13 additions & 10 deletions cores/esp32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,35 +537,38 @@ bool HardwareSerial::setMode(SerialMode mode)
return uartSetMode(_uart, mode);
}

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

if (_uart) {
log_e("RX Buffer can't be resized when Serial is already running.\n");
log_e("RX Buffer can't be resized when Serial is already running. Set it before calling begin().");
return 0;
}

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

_rxBufferSize = new_size;
return _rxBufferSize;
}

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

if (_uart) {
log_e("TX Buffer can't be resized when Serial is already running.\n");
log_e("TX Buffer can't be resized when Serial is already running. Set it before calling begin().");
return 0;
}

if (new_size <= SOC_UART_FIFO_LEN) {
log_e("TX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
return 0;
if (new_size < SOC_UART_FIFO_LEN) {
log_w("TX Buffer set to minimum value: %d.", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
_txBufferSize = 0; // it will use just UART FIFO with SOC_UART_FIFO_LEN bytes (128 for most SoC)
return SOC_UART_FIFO_LEN;
}

_txBufferSize = new_size;
return _txBufferSize;
// if new_size is SOC_UART_FIFO_LEN, _txBufferSize will be zero - just use the UART FIFO space
_txBufferSize = new_size - SOC_UART_FIFO_LEN; // for total correct report from "availableForWrite()" that matches a call to this function
return new_size;
}