Skip to content

Commit d1a781d

Browse files
authored
Update HardwareSerial.cpp
1 parent 1f397a7 commit d1a781d

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "pins_arduino.h"
77
#include "HardwareSerial.h"
88
#include "soc/soc_caps.h"
9+
#include "driver/uart.h"
910

1011
#ifndef SOC_RX0
1112
#if CONFIG_IDF_TARGET_ESP32
@@ -35,7 +36,7 @@ void serialEvent(void) {}
3536
#ifndef RX1
3637
#if CONFIG_IDF_TARGET_ESP32
3738
#define RX1 9
38-
#elif CONFIG_IDF_TARGET_ESP32S2
39+
#elif CONFIG_IDF_TARGET_ESP32S2
3940
#define RX1 18
4041
#elif CONFIG_IDF_TARGET_ESP32C3
4142
#define RX1 18
@@ -84,6 +85,8 @@ void serialEvent2(void) {}
8485
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
8586
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
8687
HardwareSerial Serial0(0);
88+
#elif ARDUINO_HW_CDC_ON_BOOT
89+
HardwareSerial Serial0(0);
8790
#else
8891
HardwareSerial Serial(0);
8992
#endif
@@ -98,6 +101,8 @@ void serialEventRun(void)
98101
{
99102
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
100103
if(Serial0.available()) serialEvent();
104+
#elif ARDUINO_HW_CDC_ON_BOOT
105+
if(Serial0.available()) serialEvent();
101106
#else
102107
if(Serial.available()) serialEvent();
103108
#endif
@@ -110,7 +115,6 @@ void serialEventRun(void)
110115
}
111116
#endif
112117

113-
114118
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL), _rxBufferSize(256) {}
115119

116120
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)
@@ -119,28 +123,39 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
119123
log_e("Serial number is invalid, please use numers from 0 to %u", SOC_UART_NUM - 1);
120124
return;
121125
}
126+
127+
// First Time or after end() --> set default Pins
128+
if (!uartIsDriverInstalled(_uart)) {
129+
switch (_uart_nr) {
130+
case UART_NUM_0:
131+
rxPin = rxPin < 0 ? SOC_RX0 : rxPin;
132+
txPin = txPin < 0 ? SOC_TX0 : txPin;
133+
break;
134+
#if SOC_UART_NUM > 1 // may save some flash bytes...
135+
case UART_NUM_1:
136+
rxPin = rxPin < 0 ? RX1 : rxPin;
137+
txPin = txPin < 0 ? TX1 : txPin;
138+
break;
139+
#endif
140+
#if SOC_UART_NUM > 2 // may save some flash bytes...
141+
case UART_NUM_2:
142+
rxPin = rxPin < 0 ? RX2 : rxPin;
143+
txPin = txPin < 0 ? TX2 : txPin;
144+
break;
145+
#endif
146+
default:
147+
log_e("Bad UART Number");
148+
return;
149+
}
150+
}
151+
122152
if(_uart) {
123153
// in this case it is a begin() over a previous begin() - maybe to change baud rate
124154
// thus do not disable debug output
125155
end(false);
126156
}
127-
if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
128-
rxPin = SOC_RX0;
129-
txPin = SOC_TX0;
130-
}
131-
#if SOC_UART_NUM > 1
132-
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
133-
rxPin = RX1;
134-
txPin = TX1;
135-
}
136-
#endif
137-
#if SOC_UART_NUM > 2
138-
if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
139-
rxPin = RX2;
140-
txPin = TX2;
141-
}
142-
#endif
143157

158+
// IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
144159
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
145160
if (!baud) {
146161
// using baud rate as zero, forces it to try to detect the current baud rate in place
@@ -276,9 +291,16 @@ void HardwareSerial::setRxInvert(bool invert)
276291
uartSetRxInvert(_uart, invert);
277292
}
278293

279-
void HardwareSerial::setPins(uint8_t rxPin, uint8_t txPin)
294+
// negative Pin value will keep it unmodified
295+
void HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
296+
{
297+
uartSetPins(_uart, rxPin, txPin, ctsPin, rtsPin);
298+
}
299+
300+
// Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
301+
void HardwareSerial::setHwFlowCtrlMode(uint8_t mode, uint8_t threshold)
280302
{
281-
uartSetPins(_uart, rxPin, txPin);
303+
uartSetHwFlowCtrlMode(_uart, mode, threshold);
282304
}
283305

284306
size_t HardwareSerial::setRxBufferSize(size_t new_size) {

0 commit comments

Comments
 (0)