Skip to content

Default Console UART Rx Tx defined in HardwareSerial Constructor #8620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions cores/esp32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is for UART0 only, would it not be safer to keep the old signature and handle the default pins in the constructor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a better way, less intrusive.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@me-no-dev - This PR will remain as draft. Please review and merge #8629 to 2.0.13

#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

Expand Down Expand Up @@ -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),
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion cores/esp32/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ typedef std::function<void(hardwareSerial_error_t)> 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)
Expand Down