From ce6107480256b3251b0d069f5c678b09bc36d90e Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Mon, 17 Sep 2018 12:16:18 -0700 Subject: [PATCH] Add functionality allowing rxBuffer of HardwareSerial to be changed in size via HardwareSerial::setRxBufferSize. (#1855) --- cores/esp32/HardwareSerial.cpp | 4 ++++ cores/esp32/HardwareSerial.h | 1 + cores/esp32/esp32-hal-uart.c | 20 ++++++++++++++++++++ cores/esp32/esp32-hal-uart.h | 2 ++ 4 files changed, 27 insertions(+) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index e46c7a02..920e4abd 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -46,6 +46,10 @@ void HardwareSerial::end() _uart = 0; } +size_t HardwareSerial::setRxBufferSize(size_t new_size) { + return uartResizeRxBuffer(_uart, new_size); +} + void HardwareSerial::setDebugOutput(bool en) { if(_uart == 0) { diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index f1e04e90..a5a3f671 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -70,6 +70,7 @@ public: uint32_t baudRate(); operator bool() const; + size_t setRxBufferSize(size_t); void setDebugOutput(bool); protected: diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index d8d13295..8fdb0cc3 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -240,6 +240,26 @@ void uartEnd(uart_t* uart) uartDetachTx(uart); } +size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) { + if(uart == NULL) { + return; + } + + UART_MUTEX_LOCK(); + if(uart->queue != NULL) { + uint8_t c; + while(xQueueReceive(uart->queue, &c, 0)); + vQueueDelete(uart->queue); + uart->queue = xQueueCreate(new_size, sizeof(uint8_t)); + if(uart->queue == NULL) { + return NULL; + } + } + UART_MUTEX_UNLOCK(); + + return new_size; +} + uint32_t uartAvailable(uart_t* uart) { if(uart == NULL || uart->queue == NULL) { diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 5cd6e965..e3023a1c 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -67,6 +67,8 @@ void uartFlush(uart_t* uart); void uartSetBaudRate(uart_t* uart, uint32_t baud_rate); uint32_t uartGetBaudRate(uart_t* uart); +size_t uartResizeRxBuffer(uart_t* uart, size_t new_size); + void uartSetDebug(uart_t* uart); int uartGetDebug();