Skip to content

Commit b3eeb0b

Browse files
committed
Improve support for ESP32 S2 and ESP32C3
Clarified documentation for supportability of newer ESP32 variants Added info on how to check if the selected pins are valid Corrects pin validation for ESP32 S2 and ESP32 C3 devices
1 parent 566809f commit b3eeb0b

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# EspSoftwareSerial
22

3-
## Implementation of the Arduino software serial library for the ESP8266 / ESP32
3+
## Implementation of the Arduino software serial library for the ESP8266 / ESP32 family
44

55
This fork implements interrupt service routine best practice.
66
In the receive interrupt, instead of blocking for whole bytes
@@ -27,6 +27,8 @@ ongoing, there will be some inexactness in interrupt timings. This may
2727
lead to inevitable, but few, bit errors when having heavy data traffic
2828
at high baud rates.
2929

30+
This library supports ESP8266, ESP32, ESP32-S2 and ESP32-C3 devices
31+
3032
## Resource optimization
3133

3234
The memory footprint can be optimized to just fit the amount of expected
@@ -97,6 +99,43 @@ To detect incoming bytes with the parity bit set (MARK parity), use the
9799
``readParity()`` function. To send a byte with the parity bit set, just add
98100
``MARK`` as the second argument when writing, e.g. ``write(ch, SWSERIAL_PARITY_MARK)``.
99101

102+
## Checking for correct pin selection / configuration
103+
In general, most pins on the ESP8266 and ESP32 devices can be used by SoftwareSerial,
104+
however each device has a number of pins that have special functions or require careful
105+
handling to prevent undesirable situations, for example they are connected to the
106+
on-board SPI flash memory or they are used to determine boot and programming modes
107+
after powerup or brownouts. These pins are not able to be configured by this library.
108+
109+
The exact list for each device can be found in the [ESP32 data sheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
110+
in sections 2.2 (Pin Descriptions) and 2.4 (Strapping pins) or by referring to the
111+
``isValidGPIOpin()``, ``isValidRxGPIOpin()`` and ``isValidTxGPIOpin()`` functions
112+
113+
The easiest and safest method is to test the object returned at runtime, to see if
114+
its valid. For example:
115+
116+
```
117+
#include <SoftwareSerial.h>
118+
119+
#define MYPORT_TX 12
120+
#define MYPORT_RX 13
121+
122+
SoftwareSerial myPort;
123+
124+
125+
....
126+
127+
128+
Serial.begin(115200); // Standard hardware serial port
129+
130+
myPort.begin(38400, SWSERIAL_8N1, MYPORT_RX, MYPORT_TX, false, 256, 256);
131+
if (!myPort) {
132+
Serial.println("Invalid pin configuration, check config");
133+
while (1) ;
134+
}
135+
136+
....
137+
```
138+
100139
## Using and updating EspSoftwareSerial in the esp8266com/esp8266 Arduino build environment
101140

102141
EspSoftwareSerial is both part of the BSP download for ESP8266 in Arduino,

src/SoftwareSerial.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,27 @@ bool SoftwareSerial::isValidGPIOpin(int8_t pin) {
5050
#if defined(ESP8266)
5151
return (pin >= 0 && pin <= 16) && !isFlashInterfacePin(pin);
5252
#elif defined(ESP32)
53+
54+
#ifdef CONFIG_IDF_TARGET_ESP32
55+
// Datasheet https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf,
56+
// Pinout https://docs.espressif.com/projects/esp-idf/en/latest/esp32/_images/esp32-devkitC-v4-pinout.jpg
5357
return (pin >= 0 && pin <= 5) || (pin >= 12 && pin <= 19) ||
5458
(pin >= 21 && pin <= 23) || (pin >= 25 && pin <= 27) || (pin >= 32 && pin <= 39);
59+
60+
#elif CONFIG_IDF_TARGET_ESP32S2
61+
// Datasheet https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf,
62+
// Pinout https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/_images/esp32-s2_saola1-pinout.jpg
63+
return (pin >= 1 && pin <= 21) || (pin >= 33 && pin <= 44);
64+
65+
#elif CONFIG_IDF_TARGET_ESP32C3
66+
// Datasheet https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf,
67+
// Pinout https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/_images/esp32-c3-devkitm-1-v1-pinout.jpg
68+
return (pin >= 0 && pin <= 1) || (pin >= 3 && pin <= 7) || (pin >= 18 && pin <= 21);
69+
70+
#else
71+
return true;
72+
#endif
73+
5574
#else
5675
return true;
5776
#endif
@@ -68,7 +87,16 @@ bool SoftwareSerial::isValidRxGPIOpin(int8_t pin) {
6887
bool SoftwareSerial::isValidTxGPIOpin(int8_t pin) {
6988
return isValidGPIOpin(pin)
7089
#if defined(ESP32)
71-
&& (pin < 34)
90+
#ifdef CONFIG_IDF_TARGET_ESP32
91+
&& (pin < 34)
92+
93+
#elif CONFIG_IDF_TARGET_ESP32S2
94+
&& (pin < 45)
95+
96+
#elif CONFIG_IDF_TARGET_ESP32C3
97+
// Nothing to do
98+
#endif
99+
72100
#endif
73101
;
74102
}

0 commit comments

Comments
 (0)