diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index e2b12bda3f4..03e4164644c 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -97,14 +97,18 @@ void serialEvent2(void) {} #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) #if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC -HardwareSerial Serial0(0); +// sets UART0 (default console) RX/TX default pins already configured in boot +HardwareSerial Serial0(0, SOC_RX0, SOC_TX0); #else -HardwareSerial Serial(0); +// sets UART0 (default console) RX/TX default pins already configured in boot +HardwareSerial Serial(0, SOC_RX0, SOC_TX0); #endif #if SOC_UART_NUM > 1 +// UART1 is not initialized in boot HardwareSerial Serial1(1); #endif #if SOC_UART_NUM > 2 +// UART2 is not initialized in boot HardwareSerial Serial2(2); #endif @@ -132,7 +136,11 @@ void serialEventRun(void) #define HSERIAL_MUTEX_UNLOCK() #endif -HardwareSerial::HardwareSerial(int uart_nr) : +// Adds the default rxPin and txPin whenever it is already initialized in Boot Time (UART0) +// default values are -1, -1 for UART1 and UART2 +// This will allow user to call HardwareSerial::end() with no previous HardwareSerial::begin() +// detahing RX and TX attached in boot time. It also shall detach console pins when using HardwareSerial::setPins() +HardwareSerial::HardwareSerial(int uart_nr, int8_t rxPin, int8_t txPin) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256), @@ -146,8 +154,8 @@ _eventTask(NULL) #if !CONFIG_DISABLE_HAL_LOCKS ,_lock(NULL) #endif -,_rxPin(-1) -,_txPin(-1) +,_rxPin(rxPin) +,_txPin(txPin) ,_ctsPin(-1) ,_rtsPin(-1) { @@ -340,28 +348,36 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in #endif HSERIAL_MUTEX_LOCK(); + // make sure that if rxPin|txPin has changed, the previous GPIOs (_rxPin|_txPin) are detached + // Example: Serial.begin(115200); Serial.begin(115200, 2, 5); ==> second begin() shall detach RX/TX from first one + if (_uart != NULL && rxPin >= 0 && rxPin != _rxPin) { + uartDetachPins(_uart, _rxPin, -1, -1, -1); + } + if (_uart != NULL && txPin >= 0 && txPin != _txPin) { + uartDetachPins(_uart, -1, _txPin, -1, -1); + } // First Time or after end() --> set default Pins if (!uartIsDriverInstalled(_uart)) { switch (_uart_nr) { case UART_NUM_0: if (rxPin < 0 && txPin < 0) { - rxPin = SOC_RX0; - txPin = SOC_TX0; + rxPin = _rxPin < 0 ? SOC_RX0 : _rxPin; + txPin = _txPin < 0 ? SOC_TX0 : _txPin; } break; #if SOC_UART_NUM > 1 // may save some flash bytes... case UART_NUM_1: if (rxPin < 0 && txPin < 0) { - rxPin = RX1; - txPin = TX1; + rxPin = _rxPin < 0 ? RX1 : _rxPin; + txPin = _txPin < 0 ? TX1 : _txPin; } break; #endif #if SOC_UART_NUM > 2 // may save some flash bytes... case UART_NUM_2: if (rxPin < 0 && txPin < 0) { - rxPin = RX2; - txPin = TX2; + rxPin = _rxPin < 0 ? RX2 : _rxPin; + txPin = _txPin < 0 ? TX2 : _txPin; } break; #endif @@ -417,10 +433,11 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in uartSetRxFIFOFull(_uart, fifoFull); _rxFIFOFull = fifoFull; } - - _rxPin = rxPin; - _txPin = txPin; - + // all fine, set the new pins + if (_uart != NULL) { + _rxPin = rxPin >= 0 ? rxPin : _rxPin; + _txPin = txPin >= 0 ? txPin : _txPin; + } HSERIAL_MUTEX_UNLOCK(); } diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 6291d241778..0296df1da7f 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -71,7 +71,9 @@ typedef std::function OnReceiveErrorCb; class HardwareSerial: public Stream { public: - HardwareSerial(int uart_nr); + // UART0 (cosole) has its RX/TX pins initialized in boot + // it shall set default pins when user calls HardwareSerial.end() with no previous begin() + HardwareSerial(int uart_nr, int8_t rxPin = -1, int8_t txPin = -1); ~HardwareSerial(); // setRxTimeout sets the timeout after which onReceive callback will be called (after receiving data, it waits for this time of UART rx inactivity to call the callback fnc)