From f56fc92b3b195a21333f1eec0f95b6c25c82ebbe Mon Sep 17 00:00:00 2001 From: Stanislav Galabov Date: Fri, 21 Jan 2022 16:16:49 +0200 Subject: [PATCH 1/2] Make HardwareSerial's begin() method virtual, so as to allow overriding it in subclasses where needed --- cores/esp32/HardwareSerial.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 796412c8ce3..d9c9cbe84e6 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -61,7 +61,7 @@ class HardwareSerial: public Stream // it will work as UART Rx interrupt void onReceive(void(*function)(void)); - void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112); + virtual void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112); void end(bool turnOffDebug = true); void updateBaudRate(unsigned long baud); int available(void); From 59a6f1010460271ac8f1ee9c56a1ae352989ee15 Mon Sep 17 00:00:00 2001 From: Stanislav Galabov Date: Mon, 31 Jan 2022 13:51:46 +0200 Subject: [PATCH 2/2] Make rxPin and txPin persistent --- cores/esp32/HardwareSerial.cpp | 68 +++++++++++++++++++++++++--------- cores/esp32/HardwareSerial.h | 6 ++- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 82799ffcfc4..9fec933be75 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -10,7 +10,7 @@ #ifndef SOC_RX0 #if CONFIG_IDF_TARGET_ESP32 #define SOC_RX0 3 -#elif CONFIG_IDF_TARGET_ESP32S2 +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 #define SOC_RX0 44 #elif CONFIG_IDF_TARGET_ESP32C3 #define SOC_RX0 20 @@ -20,7 +20,7 @@ #ifndef SOC_TX0 #if CONFIG_IDF_TARGET_ESP32 #define SOC_TX0 1 -#elif CONFIG_IDF_TARGET_ESP32S2 +#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 #define SOC_TX0 43 #elif CONFIG_IDF_TARGET_ESP32C3 #define SOC_TX0 21 @@ -35,10 +35,12 @@ void serialEvent(void) {} #ifndef RX1 #if CONFIG_IDF_TARGET_ESP32 #define RX1 9 -#elif CONFIG_IDF_TARGET_ESP32S2 +#elif CONFIG_IDF_TARGET_ESP32S2 #define RX1 18 #elif CONFIG_IDF_TARGET_ESP32C3 #define RX1 18 +#elif CONFIG_IDF_TARGET_ESP32S3 +#define RX1 15 #endif #endif @@ -49,6 +51,8 @@ void serialEvent(void) {} #define TX1 17 #elif CONFIG_IDF_TARGET_ESP32C3 #define TX1 19 +#elif CONFIG_IDF_TARGET_ESP32S3 +#define TX1 16 #endif #endif @@ -60,12 +64,16 @@ void serialEvent1(void) {} #ifndef RX2 #if CONFIG_IDF_TARGET_ESP32 #define RX2 16 +#elif CONFIG_IDF_TARGET_ESP32S3 +#define RX2 19 #endif #endif #ifndef TX2 #if CONFIG_IDF_TARGET_ESP32 #define TX2 17 +#elif CONFIG_IDF_TARGET_ESP32S3 +#define TX2 20 #endif #endif @@ -107,7 +115,7 @@ void serialEventRun(void) #endif -HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256) {} +HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256), _rxPin(-1), _txPin(-1) {} void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms, uint8_t rxfifo_full_thrhd) { @@ -120,24 +128,44 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // thus do not disable debug output end(false); } - if(_uart_nr == 0 && rxPin < 0 && txPin < 0) { - rxPin = SOC_RX0; - txPin = SOC_TX0; + + if (rxPin > -1) { + _rxPin = rxPin; + } + if (txPin > -1) { + _txPin = txPin; + } + + if(_uart_nr == 0) { + if (_rxPin < 0) { + _rxPin = SOC_RX0; + } + if (_txPin < 0) { + _txPin = SOC_TX0; + } } #if SOC_UART_NUM > 1 - if(_uart_nr == 1 && rxPin < 0 && txPin < 0) { - rxPin = RX1; - txPin = TX1; + if(_uart_nr == 1) { + if (_rxPin < 0) { + _rxPin = RX1; + } + if (_txPin < 0) { + _txPin = TX1; + } } #endif #if SOC_UART_NUM > 2 - if(_uart_nr == 2 && rxPin < 0 && txPin < 0) { - rxPin = RX2; - txPin = TX2; + if(_uart_nr == 2) { + if (_rxPin < 0) { + _rxPin = RX2; + } + if (_txPin < 0) { + _txPin = TX2; + } } #endif - _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, 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); @@ -151,7 +179,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, 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; @@ -272,9 +300,15 @@ void HardwareSerial::setRxInvert(bool invert) uartSetRxInvert(_uart, invert); } -void HardwareSerial::setPins(uint8_t rxPin, uint8_t txPin) +void HardwareSerial::setPins(int8_t rxPin, int8_t txPin) { - uartSetPins(_uart, rxPin, txPin); + if (rxPin > -1) { + _rxPin = rxPin; + } + if (txPin > -1) { + _txPin = txPin; + } + uartSetPins(_uart, _rxPin, _txPin); } size_t HardwareSerial::setRxBufferSize(size_t new_size) { diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index d9c9cbe84e6..91bcc6d49c7 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -61,7 +61,7 @@ class HardwareSerial: public Stream // it will work as UART Rx interrupt void onReceive(void(*function)(void)); - virtual void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112); + void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL, uint8_t rxfifo_full_thrhd = 112); void end(bool turnOffDebug = true); void updateBaudRate(unsigned long baud); int available(void); @@ -107,13 +107,15 @@ class HardwareSerial: public Stream void setDebugOutput(bool); void setRxInvert(bool); - void setPins(uint8_t rxPin, uint8_t txPin); + void setPins(int8_t rxPin, int8_t txPin); size_t setRxBufferSize(size_t new_size); protected: int _uart_nr; uart_t* _uart; size_t _rxBufferSize; + int8_t _rxPin; + int8_t _txPin; }; extern void serialEventRun(void) __attribute__((weak));