6
6
#include " pins_arduino.h"
7
7
#include " HardwareSerial.h"
8
8
#include " soc/soc_caps.h"
9
+ #include " driver/uart.h"
9
10
10
11
#ifndef SOC_RX0
11
12
#if CONFIG_IDF_TARGET_ESP32
@@ -35,7 +36,7 @@ void serialEvent(void) {}
35
36
#ifndef RX1
36
37
#if CONFIG_IDF_TARGET_ESP32
37
38
#define RX1 9
38
- #elif CONFIG_IDF_TARGET_ESP32S2
39
+ #elif CONFIG_IDF_TARGET_ESP32S2
39
40
#define RX1 18
40
41
#elif CONFIG_IDF_TARGET_ESP32C3
41
42
#define RX1 18
@@ -84,6 +85,8 @@ void serialEvent2(void) {}
84
85
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
85
86
#if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
86
87
HardwareSerial Serial0 (0 );
88
+ #elif ARDUINO_HW_CDC_ON_BOOT
89
+ HardwareSerial Serial0 (0 );
87
90
#else
88
91
HardwareSerial Serial (0 );
89
92
#endif
@@ -98,6 +101,8 @@ void serialEventRun(void)
98
101
{
99
102
#if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
100
103
if (Serial0.available ()) serialEvent ();
104
+ #elif ARDUINO_HW_CDC_ON_BOOT
105
+ if (Serial0.available ()) serialEvent ();
101
106
#else
102
107
if (Serial.available ()) serialEvent ();
103
108
#endif
@@ -110,7 +115,6 @@ void serialEventRun(void)
110
115
}
111
116
#endif
112
117
113
-
114
118
HardwareSerial::HardwareSerial (int uart_nr) : _uart_nr(uart_nr), _uart(NULL ), _rxBufferSize(256 ) {}
115
119
116
120
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
119
123
log_e (" Serial number is invalid, please use numers from 0 to %u" , SOC_UART_NUM - 1 );
120
124
return ;
121
125
}
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
+
122
152
if (_uart) {
123
153
// in this case it is a begin() over a previous begin() - maybe to change baud rate
124
154
// thus do not disable debug output
125
155
end (false );
126
156
}
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
143
157
158
+ // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
144
159
_uart = uartBegin (_uart_nr, baud ? baud : 9600 , config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
145
160
if (!baud) {
146
161
// 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)
276
291
uartSetRxInvert (_uart, invert);
277
292
}
278
293
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)
280
302
{
281
- uartSetPins (_uart, rxPin, txPin );
303
+ uartSetHwFlowCtrlMode (_uart, mode, threshold );
282
304
}
283
305
284
306
size_t HardwareSerial::setRxBufferSize (size_t new_size) {
0 commit comments