Skip to content

Commit c71148f

Browse files
committed
[2.0.7] Make work-around for ESP32 not detaching pins between end/begin
ESP32 does have some issue where it is not properly detaching the RX/TX pins when calling `end()` This happens especially when the RX pin receives data when calling `end()`. Problem is that the serial port will no longer work after calling `end()` followed by `begin()` until it reboots. This very messy work-around is to cache the used pins and not calling `begin()` on the port if using the same pins for a specific port. Should be fixed with: espressif/arduino-esp32#5385 But that core version cannot yet be used due to other issues, like a bug with web authentication and issues with WiFi and ESP-NOW.
1 parent 0205f09 commit c71148f

File tree

3 files changed

+84
-11
lines changed

3 files changed

+84
-11
lines changed

ESPEasySerial_ESP32.cpp

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,71 @@
11
#include <ESPeasySerial.h>
22

3+
34
// ****************************************
45
// ESP32 implementation wrapper
56
// Only support HW serial on Serial 0 .. 2
67
// ****************************************
78

89
#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+
969
ESPeasySerial::ESPeasySerial(
1070
ESPEasySerialPort port,
1171
int receivePin,
@@ -17,7 +77,9 @@ ESPeasySerial::ESPeasySerial(
1777
switch (port) {
1878
case ESPEasySerialPort::serial0:
1979
case ESPEasySerialPort::serial1:
80+
#ifndef ESP32S2
2081
case ESPEasySerialPort::serial2:
82+
#endif
2183
_serialtype = port;
2284
break;
2385
default:
@@ -56,7 +118,7 @@ void ESPeasySerial::begin(unsigned long baud, uint32_t config
56118

57119
if (txPin != -1) { _transmitPin = txPin; }
58120

59-
if (invert) { _inverse_logic = true; }
121+
_inverse_logic = invert;
60122

61123
if (!isValid()) {
62124
_baud = 0;
@@ -78,7 +140,10 @@ void ESPeasySerial::begin(unsigned long baud, uint32_t config
78140
// Timeout added for 1.0.1
79141
// See: https://github.com/espressif/arduino-esp32/commit/233d31bed22211e8c85f82bcf2492977604bbc78
80142
// 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+
}
82147
}
83148
}
84149
}
@@ -87,12 +152,15 @@ void ESPeasySerial::end() {
87152
if (!isValid()) {
88153
return;
89154
}
155+
flush();
90156
if (isI2Cserial()) {
91157
#ifndef DISABLE_SC16IS752_Serial
92158
_i2cserial->end();
93159
#endif
94160
} 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();
96164
}
97165
}
98166

@@ -126,10 +194,14 @@ const HardwareSerial * ESPeasySerial::getHW() const {
126194
bool ESPeasySerial::isValid() const {
127195
switch (_serialtype) {
128196
case ESPEasySerialPort::serial0:
197+
case ESPEasySerialPort::serial1:
198+
return true;
129199
case ESPEasySerialPort::serial2:
200+
#ifdef ESP32S2
201+
return false;
202+
#else
130203
return true;
131-
case ESPEasySerialPort::serial1:
132-
return _transmitPin != -1 && _receivePin != -1;
204+
#endif
133205
case ESPEasySerialPort::sc16is752:
134206
#ifndef DISABLE_SC16IS752_Serial
135207
return _i2cserial != nullptr;
@@ -188,7 +260,7 @@ size_t ESPeasySerial::write(const uint8_t *buffer, size_t size) {
188260

189261
size_t ESPeasySerial::write(const char *buffer) {
190262
if (!buffer) { return 0; }
191-
return write(buffer, strlen(buffer));
263+
return write(buffer, strlen_P(buffer));
192264
}
193265

194266
int ESPeasySerial::read(void) {
@@ -227,8 +299,9 @@ void ESPeasySerial::flush(void) {
227299
#ifndef DISABLE_SC16IS752_Serial
228300
_i2cserial->flush();
229301
#endif
302+
} else {
303+
getHW()->flush();
230304
}
231-
getHW()->flush();
232305
}
233306

234307
int ESPeasySerial::baudRate(void) {

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ESPeasySerial",
3-
"version": "2.0.6",
3+
"version": "2.0.7",
44
"keywords": [
55
"serial", "io", "softwareserial", "hardwareserial"
66
],
@@ -27,6 +27,6 @@
2727
],
2828
"frameworks": "arduino",
2929
"platforms": [
30-
"espressif8266","espressif32"
30+
"espressif8266","espressif32","espressif32s2"
3131
]
3232
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=ESPeasySerial
2-
version=2.0.6
2+
version=2.0.7
33
author=Gijs Noorlander
44
maintainer=Gijs Noorlander <[email protected]>
55
sentence=Wrapper for SoftwareSerial, SC16IS752 I2C to UART bridge and HardwareSerial for ESP8266 and ESP32/ESP32S2.
66
paragraph=
77
depends=SC16IS752
88
category=Signal Input/Output
99
url=
10-
architectures=esp8266,esp32
10+
architectures=esp8266,esp32,esp32s2

0 commit comments

Comments
 (0)