Unverified Commit 77e95311 authored by Gonzalo Brusco's avatar Gonzalo Brusco Committed by GitHub

Adds HardwareSerial::setTxBufferSize() (#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: default avatarRodrigo Garcia <rodrigo.garcia@espressif.com>
parent 8fe0efe8
...@@ -128,6 +128,7 @@ HardwareSerial::HardwareSerial(int uart_nr) : ...@@ -128,6 +128,7 @@ HardwareSerial::HardwareSerial(int uart_nr) :
_uart_nr(uart_nr), _uart_nr(uart_nr),
_uart(NULL), _uart(NULL),
_rxBufferSize(256), _rxBufferSize(256),
_txBufferSize(0),
_onReceiveCB(NULL), _onReceiveCB(NULL),
_onReceiveErrorCB(NULL), _onReceiveErrorCB(NULL),
_eventTask(NULL) _eventTask(NULL)
...@@ -295,7 +296,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in ...@@ -295,7 +296,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. // 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) { if (!baud) {
// using baud rate as zero, forces it to try to detect the current baud rate in place // using baud rate as zero, forces it to try to detect the current baud rate in place
uartStartDetectBaudrate(_uart); uartStartDetectBaudrate(_uart);
...@@ -309,7 +310,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in ...@@ -309,7 +310,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
if(detectedBaudRate) { if(detectedBaudRate) {
delay(100); // Give some time... 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 { } else {
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
_uart = NULL; _uart = NULL;
...@@ -458,10 +459,26 @@ size_t HardwareSerial::setRxBufferSize(size_t new_size) { ...@@ -458,10 +459,26 @@ size_t HardwareSerial::setRxBufferSize(size_t new_size) {
} }
if (new_size <= SOC_UART_FIFO_LEN) { 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; return 0;
} }
_rxBufferSize = new_size; _rxBufferSize = new_size;
return _rxBufferSize; 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;
}
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;
}
...@@ -132,11 +132,13 @@ public: ...@@ -132,11 +132,13 @@ public:
void setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length 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 setRxBufferSize(size_t new_size);
size_t setTxBufferSize(size_t new_size);
protected: protected:
int _uart_nr; int _uart_nr;
uart_t* _uart; uart_t* _uart;
size_t _rxBufferSize; size_t _rxBufferSize;
size_t _txBufferSize;
OnReceiveCb _onReceiveCB; OnReceiveCb _onReceiveCB;
OnReceiveErrorCb _onReceiveErrorCB; OnReceiveErrorCb _onReceiveErrorCB;
TaskHandle_t _eventTask; TaskHandle_t _eventTask;
......
...@@ -130,7 +130,7 @@ void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold) { ...@@ -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) { if(uart_nr >= SOC_UART_NUM) {
return NULL; return NULL;
...@@ -163,7 +163,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx ...@@ -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; 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, 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_param_config(uart_nr, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
......
...@@ -61,7 +61,7 @@ extern "C" { ...@@ -61,7 +61,7 @@ extern "C" {
struct uart_struct_t; struct uart_struct_t;
typedef struct uart_struct_t uart_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); 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 // This is used to retrieve the Event Queue pointer from a UART IDF Driver in order to allow user to deal with its events
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment