From 3df9324bff784686c786ba9947a97c8d0fc42301 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Sat, 11 Apr 2020 10:44:14 -0600 Subject: [PATCH 1/3] Fixes UART detach. Fixes #3878 --- cores/esp32/HardwareSerial.cpp | 7 ++++++- cores/esp32/HardwareSerial.h | 2 ++ cores/esp32/esp32-hal-uart.c | 14 +++++++------- cores/esp32/esp32-hal-uart.h | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index be80ca27d1d..39a55fcc4d9 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -53,6 +53,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in } _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert); + _tx_pin = txPin; + _rx_pin = rxPin; if(!baud) { uartStartDetectBaudrate(_uart); @@ -70,6 +72,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in } 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; + _tx_pin = 0; + _rx_pin = 0; } } } @@ -84,7 +88,8 @@ void HardwareSerial::end() if(uartGetDebug() == _uart_nr) { uartSetDebug(0); } - uartEnd(_uart); + log_v("pins %d %d",_tx_pin, _rx_pin); + uartEnd(_uart, _tx_pin, _rx_pin); _uart = 0; } diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 820476748da..f6fab2a9989 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -104,6 +104,8 @@ class HardwareSerial: public Stream protected: int _uart_nr; uart_t* _uart; + uint8_t _tx_pin; + uint8_t _rx_pin; }; extern void serialEventRun(void) __attribute__((weak)); diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 8ec1fe6f7d1..480ffcc3d0c 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -124,21 +124,21 @@ void uartDisableInterrupt(uart_t* uart) UART_MUTEX_UNLOCK(); } -void uartDetachRx(uart_t* uart) +void uartDetachRx(uart_t* uart, uint8_t rxPin) { if(uart == NULL) { return; } - pinMatrixInDetach(UART_RXD_IDX(uart->num), false, false); + pinMatrixInDetach(rxPin, false, false); uartDisableInterrupt(uart); } -void uartDetachTx(uart_t* uart) +void uartDetachTx(uart_t* uart, uint8_t txPin) { if(uart == NULL) { return; } - pinMatrixOutDetach(UART_TXD_IDX(uart->num), false, false); + pinMatrixOutDetach(txPin, false, false); } void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted) @@ -226,7 +226,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx return uart; } -void uartEnd(uart_t* uart) +void uartEnd(uart_t* uart, uint8_t txPin, uint8_t rxPin) { if(uart == NULL) { return; @@ -243,8 +243,8 @@ void uartEnd(uart_t* uart) UART_MUTEX_UNLOCK(); - uartDetachRx(uart); - uartDetachTx(uart); + uartDetachRx(uart, rxPin); + uartDetachTx(uart, txPin); } size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) { diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index e44d25bbc5e..ffec2a45ac8 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -52,7 +52,7 @@ 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); -void uartEnd(uart_t* uart); +void uartEnd(uart_t* uart, uint8_t rxPin, uint8_t txPin); uint32_t uartAvailable(uart_t* uart); uint32_t uartAvailableForWrite(uart_t* uart); From 61062270d4ff40e3af768ad220dfb259db4c06a2 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Sat, 11 Apr 2020 10:49:41 -0600 Subject: [PATCH 2/3] 0 is not a good holder value for pins! --- cores/esp32/HardwareSerial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 39a55fcc4d9..7c35b6ca931 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -72,8 +72,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in } 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; - _tx_pin = 0; - _rx_pin = 0; + _tx_pin = -1; + _rx_pin = -1; } } } From 813b7f4f52172ce092ac1a40782e246c225b6105 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Sat, 11 Apr 2020 10:51:15 -0600 Subject: [PATCH 3/3] 0 is not a good holder value for pins! --- cores/esp32/HardwareSerial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 7c35b6ca931..a6c362931ac 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -72,8 +72,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in } 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; - _tx_pin = -1; - _rx_pin = -1; + _tx_pin = 255; + _rx_pin = 255; } } }