From 9a5e3d5c3ed4d0ee813fdc99e855da3ba1e012b2 Mon Sep 17 00:00:00 2001 From: Gonzalo Brusco Date: Mon, 7 Mar 2022 15:14:28 -0300 Subject: [PATCH 1/4] Adds HardwareSerial::setTxBufferSize() --- cores/esp32/HardwareSerial.cpp | 18 +++++++++++++++--- cores/esp32/HardwareSerial.h | 2 ++ cores/esp32/esp32-hal-uart.c | 4 ++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 61c41cebfb0..e3266f89435 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -127,7 +127,8 @@ void serialEventRun(void) HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), -_rxBufferSize(256), +_rxBufferSize(256), +_txBufferSize(0), _onReceiveCB(NULL), _onReceiveErrorCB(NULL), _eventTask(NULL) @@ -289,7 +290,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in } // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. - _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd); + _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); if (!baud) { // using baud rate as zero, forces it to try to detect the current baud rate in place uartStartDetectBaudrate(_uart); @@ -303,7 +304,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in if(detectedBaudRate) { delay(100); // Give some time... - _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd); + _uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd); } else { log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); _uart = NULL; @@ -459,3 +460,14 @@ size_t HardwareSerial::setRxBufferSize(size_t new_size) { _rxBufferSize = new_size; return _rxBufferSize; } + +size_t HardwareSerial::setTxBufferSize(size_t new_size) { + + if (_uart) { + log_e("TX Buffer can't be resized when Serial is already running.\n"); + return 0; + } + + _txBufferSize = new_size; + return _txBufferSize; +} diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 7a25b64e37f..eeb2288032c 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -132,11 +132,13 @@ class HardwareSerial: public Stream void setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length size_t setRxBufferSize(size_t new_size); + size_t setTxBufferSize(size_t new_size); protected: int _uart_nr; uart_t* _uart; size_t _rxBufferSize; + size_t _txBufferSize; OnReceiveCb _onReceiveCB; OnReceiveErrorCb _onReceiveErrorCB; TaskHandle_t _eventTask; diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 4581334bba8..83ee13bc7ab 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -130,7 +130,7 @@ void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold) { } -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) +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) { if(uart_nr >= SOC_UART_NUM) { return NULL; @@ -163,7 +163,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx uart_config.source_clk = UART_SCLK_APB; - ESP_ERROR_CHECK(uart_driver_install(uart_nr, 2*queueLen, 0, 20, &(uart->uart_event_queue), 0)); + ESP_ERROR_CHECK(uart_driver_install(uart_nr, 2*rx_buffer_size, 2*tx_buffer_size, 20, &(uart->uart_event_queue), 0)); ESP_ERROR_CHECK(uart_param_config(uart_nr, &uart_config)); ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); From c44468f7cc76ced54de681d3f030b91112d03293 Mon Sep 17 00:00:00 2001 From: Gonzalo Brusco Date: Mon, 7 Mar 2022 15:27:41 -0300 Subject: [PATCH 2/4] uartBegin def fix --- cores/esp32/esp32-hal-uart.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 25c17ea6ab6..ec7912c3d7a 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -61,7 +61,7 @@ extern "C" { struct uart_struct_t; typedef struct uart_struct_t uart_t; -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); +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); void uartEnd(uart_t* uart); // This is used to retrieve the Event Queue pointer from a UART IDF Driver in order to allow user to deal with its events From 89746b45757aea7d99ffcbaf60f710418292db3f Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 19 Mar 2022 10:45:43 -0300 Subject: [PATCH 3/4] checks TXBufferSize as defined in IDF Makes sure that the buffer size will not cause a reset to the board. --- cores/esp32/HardwareSerial.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index add317ac222..6dee4f660d7 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -459,7 +459,7 @@ size_t HardwareSerial::setRxBufferSize(size_t new_size) { } if (new_size <= SOC_UART_FIFO_LEN) { - log_e("RX Buffer must be higher than %d.\n", 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; } @@ -474,6 +474,11 @@ size_t HardwareSerial::setTxBufferSize(size_t new_size) { 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; + } + _txBufferSize = new_size; return _txBufferSize; } From 82aed791f54fcf19438ecc3d3d53760af12297ca Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 19 Mar 2022 10:48:10 -0300 Subject: [PATCH 4/4] Removes double value in Rx/Tx Buffer Size Keeps Rx/Tx buffer size as set, not doubling it. It makes the process more clear. --- cores/esp32/esp32-hal-uart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 83ee13bc7ab..9046dac4089 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -163,7 +163,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx uart_config.source_clk = UART_SCLK_APB; - ESP_ERROR_CHECK(uart_driver_install(uart_nr, 2*rx_buffer_size, 2*tx_buffer_size, 20, &(uart->uart_event_queue), 0)); + ESP_ERROR_CHECK(uart_driver_install(uart_nr, rx_buffer_size, tx_buffer_size, 20, &(uart->uart_event_queue), 0)); ESP_ERROR_CHECK(uart_param_config(uart_nr, &uart_config)); ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));