1
1
#include < ESPeasySerial.h>
2
2
3
+
3
4
// ****************************************
4
5
// ESP32 implementation wrapper
5
6
// Only support HW serial on Serial 0 .. 2
6
7
// ****************************************
7
8
8
9
#ifdef ESP32
10
+
11
+ // Temporary work-around for bug in ESP32 code, where the pin matrix is not cleaned up between calling end() and begin()
12
+ // Work-around is to keep track of the last used pins for a serial port,
13
+ // as it is likely that a node will always use the same pins most of the time.
14
+ // If not, a reboot may be OK to fix it.
15
+ // Another idea is to swap pins among UART ports.
16
+ // e.g. use the same pins on Serial1 if it was used on Serial2 before.
17
+
18
+ // PR to fix it: https://github.com/espressif/arduino-esp32/pull/5385
19
+ // See: https://github.com/espressif/arduino-esp32/issues/3878
20
+
21
+
22
+ static int receivePin0 = -1 ;
23
+ static int transmitPin0 = -1 ;
24
+ static int receivePin1 = -1 ;
25
+ static int transmitPin1 = -1 ;
26
+ static int receivePin2 = -1 ;
27
+ static int transmitPin2 = -1 ;
28
+
29
+ bool pinsChanged (ESPEasySerialPort port,
30
+ int receivePin,
31
+ int transmitPin)
32
+ {
33
+ switch (port) {
34
+ case ESPEasySerialPort::serial0: return receivePin != receivePin0 || transmitPin != transmitPin0;
35
+ case ESPEasySerialPort::serial1: return receivePin != receivePin1 || transmitPin != transmitPin1;
36
+ #ifndef ESP32S2
37
+ case ESPEasySerialPort::serial2: return receivePin != receivePin2 || transmitPin != transmitPin2;
38
+ #endif
39
+ }
40
+ return false ;
41
+ }
42
+
43
+ void setPinsCache (ESPEasySerialPort port,
44
+ int receivePin,
45
+ int transmitPin)
46
+ {
47
+ switch (port) {
48
+ case ESPEasySerialPort::serial0:
49
+ receivePin0 = receivePin;
50
+ transmitPin0 = transmitPin;
51
+ break ;
52
+ case ESPEasySerialPort::serial1:
53
+ receivePin1 = receivePin;
54
+ transmitPin1 = transmitPin;
55
+ break ;
56
+
57
+ #ifndef ESP32S2
58
+ case ESPEasySerialPort::serial2:
59
+ receivePin2 = receivePin;
60
+ transmitPin2 = transmitPin;
61
+ break ;
62
+
63
+ #endif
64
+ }
65
+ }
66
+
67
+ // End of messy work-around.
68
+
9
69
ESPeasySerial::ESPeasySerial (
10
70
ESPEasySerialPort port,
11
71
int receivePin,
@@ -17,7 +77,9 @@ ESPeasySerial::ESPeasySerial(
17
77
switch (port) {
18
78
case ESPEasySerialPort::serial0:
19
79
case ESPEasySerialPort::serial1:
80
+ #ifndef ESP32S2
20
81
case ESPEasySerialPort::serial2:
82
+ #endif
21
83
_serialtype = port;
22
84
break ;
23
85
default :
@@ -56,7 +118,7 @@ void ESPeasySerial::begin(unsigned long baud, uint32_t config
56
118
57
119
if (txPin != -1 ) { _transmitPin = txPin; }
58
120
59
- if (invert) { _inverse_logic = true ; }
121
+ _inverse_logic = invert;
60
122
61
123
if (!isValid ()) {
62
124
_baud = 0 ;
@@ -78,7 +140,10 @@ void ESPeasySerial::begin(unsigned long baud, uint32_t config
78
140
// Timeout added for 1.0.1
79
141
// See: https://github.com/espressif/arduino-esp32/commit/233d31bed22211e8c85f82bcf2492977604bbc78
80
142
// getHW()->begin(baud, config, _receivePin, _transmitPin, invert, timeout_ms);
81
- getHW ()->begin (baud, config, _receivePin, _transmitPin, _inverse_logic);
143
+ if (pinsChanged (_serialtype, _receivePin, _transmitPin)) {
144
+ setPinsCache (_serialtype, _receivePin, _transmitPin);
145
+ getHW ()->begin (baud, config, _receivePin, _transmitPin, _inverse_logic);
146
+ }
82
147
}
83
148
}
84
149
}
@@ -87,12 +152,15 @@ void ESPeasySerial::end() {
87
152
if (!isValid ()) {
88
153
return ;
89
154
}
155
+ flush ();
90
156
if (isI2Cserial ()) {
91
157
#ifndef DISABLE_SC16IS752_Serial
92
158
_i2cserial->end ();
93
159
#endif
94
160
} else {
95
- getHW ()->end ();
161
+ // Work-around to fix proper detach RX pin for older ESP32 core versions
162
+ // For now do not call end()
163
+ // getHW()->end();
96
164
}
97
165
}
98
166
@@ -126,10 +194,14 @@ const HardwareSerial * ESPeasySerial::getHW() const {
126
194
bool ESPeasySerial::isValid () const {
127
195
switch (_serialtype) {
128
196
case ESPEasySerialPort::serial0:
197
+ case ESPEasySerialPort::serial1:
198
+ return true ;
129
199
case ESPEasySerialPort::serial2:
200
+ #ifdef ESP32S2
201
+ return false ;
202
+ #else
130
203
return true ;
131
- case ESPEasySerialPort::serial1:
132
- return _transmitPin != -1 && _receivePin != -1 ;
204
+ #endif
133
205
case ESPEasySerialPort::sc16is752:
134
206
#ifndef DISABLE_SC16IS752_Serial
135
207
return _i2cserial != nullptr ;
@@ -188,7 +260,7 @@ size_t ESPeasySerial::write(const uint8_t *buffer, size_t size) {
188
260
189
261
size_t ESPeasySerial::write (const char *buffer) {
190
262
if (!buffer) { return 0 ; }
191
- return write (buffer, strlen (buffer));
263
+ return write (buffer, strlen_P (buffer));
192
264
}
193
265
194
266
int ESPeasySerial::read (void ) {
@@ -227,8 +299,9 @@ void ESPeasySerial::flush(void) {
227
299
#ifndef DISABLE_SC16IS752_Serial
228
300
_i2cserial->flush ();
229
301
#endif
302
+ } else {
303
+ getHW ()->flush ();
230
304
}
231
- getHW ()->flush ();
232
305
}
233
306
234
307
int ESPeasySerial::baudRate (void ) {
0 commit comments