Skip to content

Commit 77e9531

Browse files
Adds HardwareSerial::setTxBufferSize() (espressif#6383)
* Adds HardwareSerial::setTxBufferSize() * uartBegin def fix * checks TXBufferSize as defined in IDF Makes sure that the buffer size will not cause a reset to the board. * Removes double value in Rx/Tx Buffer Size Keeps Rx/Tx buffer size as set, not doubling it. It makes the process more clear. Co-authored-by: Rodrigo Garcia <rodrigo.garcia@espressif.com>
1 parent 8fe0efe commit 77e9531

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

cores/esp32/HardwareSerial.cpp

+21-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ void serialEventRun(void)
127127
HardwareSerial::HardwareSerial(int uart_nr) :
128128
_uart_nr(uart_nr),
129129
_uart(NULL),
130-
_rxBufferSize(256),
130+
_rxBufferSize(256),
131+
_txBufferSize(0),
131132
_onReceiveCB(NULL),
132133
_onReceiveErrorCB(NULL),
133134
_eventTask(NULL)
@@ -295,7 +296,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
295296
}
296297

297298
// IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
298-
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
299+
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
299300
if (!baud) {
300301
// using baud rate as zero, forces it to try to detect the current baud rate in place
301302
uartStartDetectBaudrate(_uart);
@@ -309,7 +310,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
309310

310311
if(detectedBaudRate) {
311312
delay(100); // Give some time...
312-
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
313+
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
313314
} else {
314315
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
315316
_uart = NULL;
@@ -458,10 +459,26 @@ size_t HardwareSerial::setRxBufferSize(size_t new_size) {
458459
}
459460

460461
if (new_size <= SOC_UART_FIFO_LEN) {
461-
log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN);
462+
log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
462463
return 0;
463464
}
464465

465466
_rxBufferSize = new_size;
466467
return _rxBufferSize;
467468
}
469+
470+
size_t HardwareSerial::setTxBufferSize(size_t new_size) {
471+
472+
if (_uart) {
473+
log_e("TX Buffer can't be resized when Serial is already running.\n");
474+
return 0;
475+
}
476+
477+
if (new_size <= SOC_UART_FIFO_LEN) {
478+
log_e("TX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
479+
return 0;
480+
}
481+
482+
_txBufferSize = new_size;
483+
return _txBufferSize;
484+
}

cores/esp32/HardwareSerial.h

+2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ class HardwareSerial: public Stream
132132
void setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length
133133

134134
size_t setRxBufferSize(size_t new_size);
135+
size_t setTxBufferSize(size_t new_size);
135136

136137
protected:
137138
int _uart_nr;
138139
uart_t* _uart;
139140
size_t _rxBufferSize;
141+
size_t _txBufferSize;
140142
OnReceiveCb _onReceiveCB;
141143
OnReceiveErrorCb _onReceiveErrorCB;
142144
TaskHandle_t _eventTask;

cores/esp32/esp32-hal-uart.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold) {
130130
}
131131

132132

133-
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd)
133+
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t rx_buffer_size, uint16_t tx_buffer_size, bool inverted, uint8_t rxfifo_full_thrhd)
134134
{
135135
if(uart_nr >= SOC_UART_NUM) {
136136
return NULL;
@@ -163,7 +163,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
163163
uart_config.source_clk = UART_SCLK_APB;
164164

165165

166-
ESP_ERROR_CHECK(uart_driver_install(uart_nr, 2*queueLen, 0, 20, &(uart->uart_event_queue), 0));
166+
ESP_ERROR_CHECK(uart_driver_install(uart_nr, rx_buffer_size, tx_buffer_size, 20, &(uart->uart_event_queue), 0));
167167
ESP_ERROR_CHECK(uart_param_config(uart_nr, &uart_config));
168168
ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
169169

cores/esp32/esp32-hal-uart.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extern "C" {
6161
struct uart_struct_t;
6262
typedef struct uart_struct_t uart_t;
6363

64-
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd);
64+
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t rx_buffer_size, uint16_t tx_buffer_size, bool inverted, uint8_t rxfifo_full_thrhd);
6565
void uartEnd(uart_t* uart);
6666

6767
// This is used to retrieve the Event Queue pointer from a UART IDF Driver in order to allow user to deal with its events

0 commit comments

Comments
 (0)