From 8ed97b313d290079cc8a89820d597431a25c7dae Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 8 Sep 2023 15:11:40 -0300 Subject: [PATCH 1/7] detach pins in HardwareSerial::setPins() --- cores/esp32/HardwareSerial.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index e2b12bda3f4..23dc187179d 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -554,10 +554,16 @@ void HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t r // uartSetPins() checks if pins are valid for each function and for the SoC if (uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin)) { - _txPin = _txPin >= 0 ? txPin : _txPin; - _rxPin = _rxPin >= 0 ? rxPin : _rxPin; - _rtsPin = _rtsPin >= 0 ? rtsPin : _rtsPin; - _ctsPin = _ctsPin >= 0 ? ctsPin : _ctsPin; + // detach previous attached UART pins if not set as same as before + if (_rxPin >= 0 && _rxPin != rxPin) uartDetachPins(_uart, _rxPin, -1, -1, -1); + if (_txPin >= 0 && _txPin != txPin) uartDetachPins(_uart, -1, _txPin, -1, -1); + if (_ctsPin >= 0 && _ctsPin != ctsPin) uartDetachPins(_uart, -1, -1, _ctsPin, -1); + if (_rtsPin >= 0 && _rtsPin != rtsPin) uartDetachPins(_uart, -1, -1, -1, _rtsPin); + // set new pins for a future end() or a setPins() + _txPin = txPin >= 0 ? txPin : _txPin; + _rxPin = rxPin >= 0 ? rxPin : _rxPin; + _rtsPin = rtsPin >= 0 ? rtsPin : _rtsPin; + _ctsPin = ctsPin >= 0 ? ctsPin : _ctsPin; } else { log_e("Error when setting Serial port Pins. Invalid Pin.\n"); } From 1c1349656fb196383c951a72db14a937621b64e3 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 8 Sep 2023 19:21:49 -0300 Subject: [PATCH 2/7] make uartDetachPins() simpler --- cores/esp32/HardwareSerial.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 23dc187179d..f21c191e539 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -442,7 +442,7 @@ void HardwareSerial::end(bool fullyTerminate) _rxFIFOFull = 0; - uartDetachPins(_uart, _rxPin, _txPin, _ctsPin, _rtsPin); + uartDetachPins(_rxPin, _txPin, _ctsPin, _rtsPin); _rxPin = _txPin = _ctsPin = _rtsPin = -1; } @@ -555,10 +555,10 @@ void HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t r // uartSetPins() checks if pins are valid for each function and for the SoC if (uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin)) { // detach previous attached UART pins if not set as same as before - if (_rxPin >= 0 && _rxPin != rxPin) uartDetachPins(_uart, _rxPin, -1, -1, -1); - if (_txPin >= 0 && _txPin != txPin) uartDetachPins(_uart, -1, _txPin, -1, -1); - if (_ctsPin >= 0 && _ctsPin != ctsPin) uartDetachPins(_uart, -1, -1, _ctsPin, -1); - if (_rtsPin >= 0 && _rtsPin != rtsPin) uartDetachPins(_uart, -1, -1, -1, _rtsPin); + if (_rxPin >= 0 && _rxPin != rxPin) uartDetachPins(_rxPin, -1, -1, -1); + if (_txPin >= 0 && _txPin != txPin) uartDetachPins(-1, _txPin, -1, -1); + if (_ctsPin >= 0 && _ctsPin != ctsPin) uartDetachPins(-1, -1, _ctsPin, -1); + if (_rtsPin >= 0 && _rtsPin != rtsPin) uartDetachPins(-1, -1, -1, _rtsPin); // set new pins for a future end() or a setPins() _txPin = txPin >= 0 ? txPin : _txPin; _rxPin = rxPin >= 0 ? rxPin : _rxPin; From 06085a2444b61b70bbf99d9880bec01720b94e7d Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 8 Sep 2023 19:22:46 -0300 Subject: [PATCH 3/7] make uartDetachPins() simpler --- 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 1d23ba47c52..488ee026294 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -123,7 +123,7 @@ bool uartIsDriverInstalled(uart_t* uart); // Negative Pin Number will keep it unmodified, thus this function can set/reset individual pins bool uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); -void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); +void uartDetachPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); // Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold); From 370a6374c4170b30bc8ab976c00863caefbe9f52 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 8 Sep 2023 19:23:41 -0300 Subject: [PATCH 4/7] make uartDetachPins() simpler --- cores/esp32/esp32-hal-uart.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 7706f132d5e..93b27217dab 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -79,12 +79,8 @@ static uart_t _uart_bus_array[] = { // be seen in the previous pins and new pins as well. // Valid pin UART_PIN_NO_CHANGE is defined to (-1) // Negative Pin Number will keep it unmodified, thus this function can detach individual pins -void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) +void uartDetachPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) { - if(uart == NULL) { - return; - } - UART_MUTEX_LOCK(); if (txPin >= 0) { gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[txPin], PIN_FUNC_GPIO); From 6f3a5d852c04e9499806bb5161af8d28d142b3a3 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 8 Sep 2023 19:51:08 -0300 Subject: [PATCH 5/7] reverts yartDetachPins change --- cores/esp32/HardwareSerial.cpp | 8 ++++---- cores/esp32/esp32-hal-uart.c | 6 +++++- cores/esp32/esp32-hal-uart.h | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index f21c191e539..bae340ce025 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -555,10 +555,10 @@ void HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t r // uartSetPins() checks if pins are valid for each function and for the SoC if (uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin)) { // detach previous attached UART pins if not set as same as before - if (_rxPin >= 0 && _rxPin != rxPin) uartDetachPins(_rxPin, -1, -1, -1); - if (_txPin >= 0 && _txPin != txPin) uartDetachPins(-1, _txPin, -1, -1); - if (_ctsPin >= 0 && _ctsPin != ctsPin) uartDetachPins(-1, -1, _ctsPin, -1); - if (_rtsPin >= 0 && _rtsPin != rtsPin) uartDetachPins(-1, -1, -1, _rtsPin); + if (_rxPin >= 0 && _rxPin != rxPin) uartDetachPins(_uart, _rxPin, -1, -1, -1); + if (_txPin >= 0 && _txPin != txPin) uartDetachPins(_uart, -1, _txPin, -1, -1); + if (_ctsPin >= 0 && _ctsPin != ctsPin) uartDetachPins(_uart, -1, -1, _ctsPin, -1); + if (_rtsPin >= 0 && _rtsPin != rtsPin) uartDetachPins(_uart, -1, -1, -1, _rtsPin); // set new pins for a future end() or a setPins() _txPin = txPin >= 0 ? txPin : _txPin; _rxPin = rxPin >= 0 ? rxPin : _rxPin; diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 93b27217dab..fe1989a2133 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -79,8 +79,12 @@ static uart_t _uart_bus_array[] = { // be seen in the previous pins and new pins as well. // Valid pin UART_PIN_NO_CHANGE is defined to (-1) // Negative Pin Number will keep it unmodified, thus this function can detach individual pins -void uartDetachPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) +void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) { + if(uart == NULL) { + return false; + } + UART_MUTEX_LOCK(); if (txPin >= 0) { gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[txPin], PIN_FUNC_GPIO); diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 488ee026294..1d23ba47c52 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -123,7 +123,7 @@ bool uartIsDriverInstalled(uart_t* uart); // Negative Pin Number will keep it unmodified, thus this function can set/reset individual pins bool uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); -void uartDetachPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); +void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin); // Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold); From 45a31b10a368e8375dace4419cf418ad98b7328d Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 8 Sep 2023 19:52:55 -0300 Subject: [PATCH 6/7] reverts uartDetachPins() return --- 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 fe1989a2133..7706f132d5e 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -82,7 +82,7 @@ static uart_t _uart_bus_array[] = { void uartDetachPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin) { if(uart == NULL) { - return false; + return; } UART_MUTEX_LOCK(); From 24967ac4ab983254b54eac1fde50c5e2767311f9 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 8 Sep 2023 19:54:37 -0300 Subject: [PATCH 7/7] reverts uartDetachPins() wrong call --- cores/esp32/HardwareSerial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index bae340ce025..23dc187179d 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -442,7 +442,7 @@ void HardwareSerial::end(bool fullyTerminate) _rxFIFOFull = 0; - uartDetachPins(_rxPin, _txPin, _ctsPin, _rtsPin); + uartDetachPins(_uart, _rxPin, _txPin, _ctsPin, _rtsPin); _rxPin = _txPin = _ctsPin = _rtsPin = -1; }