Skip to content

Library does not work properly on ESP32 S2 - fix provided. #219

@tchilton

Description

@tchilton

Nice code - thanks, but there are several related problems here that have driven me into a deep dive to fix them.

  1. The readme is not clear on which versions of the ESP32 are supported - it does not state ESP32 S2 or ESP C3, but there are bugfixes in for C3. Please update the readme to more clearly show which CPU versions are supported.

  2. Getting the code to compile - as others have reported, using the stock ESP32 board definitions via Arduino board manager, which is currently listed as in alpha, results in compile errors with `__atomic_exchange_1' type messages.

The solution to this is to update to the latest ESP32/ESP32 S2 code from Expresif's website as detailed in
(https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-guides/tools/idf-tools.html#xtensa-esp32s2-elf) and clarified in the article at (https://www.mischianti.org/2020/12/01/esp32-s2-pinout-specs-and-arduino-ide-configuration-1/). Note the change from the 2020 r3 to 2021 r1 code and the version 3.1 of esptools. Once this is correctly applied, the code will compile properly.

  1. The pin restrictions listed in the source code are not accurate for ESP32 S2 and ESP C3 devices and this results in silent configuration failures and what looks like dead code. The root of this is in the begin method. There must be a better way to report config failures - even something as simple as a Serial.println from the class to say that the pin is not valid on a failure from the isValidXxGPIOPin functions. Given that most of the methods have void return types, this appears to be the only sensible route forwards.

The fixes need to make ESP32 S2 and probably ESP C3 (I've not got one to test with) usable on all the mapped out pins are as follows, The definitions are from the Expresif IDF and not standard to Arduino and the links to the vendors module pinouts are listed below.

bool SoftwareSerial::isValidGPIOpin(int8_t pin) {
#if defined(ESP8266)
return (pin >= 0 && pin <= 16) && !isFlashInterfacePin(pin);
#elif defined(ESP32)
#ifdef CONFIG_IDF_TARGET_ESP32
return (pin >= 0 && pin <= 5) || (pin >= 12 && pin <= 19) ||
(pin >= 21 && pin <= 23) || (pin >= 25 && pin <= 27) || (pin >= 32 && pin <= 39);

#elif CONFIG_IDF_TARGET_ESP32S2
// See [https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/_images/esp32-s2_saola1-pinout.jpg]
return (pin >= 1 && pin <= 21) || (pin >= 33 && pin <= 44);

#elif CONFIG_IDF_TARGET_ESP32C3
// See [https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/_images/esp32-c3-devkitm-1-v1-pinout.jpg]
return (pin >= 0 && pin <= 1) || (pin >= 3 && pin <= 7) || (pin >= 18 && pin <= 21);
#else
return true;
#endif
#else
return true;
#endif
}

bool SoftwareSerial::isValidTxGPIOpin(int8_t pin) {
return isValidGPIOpin(pin)
#if defined(ESP32)
#ifdef CONFIG_IDF_TARGET_ESP32
&& (pin < 34)

#elif CONFIG_IDF_TARGET_ESP32S2
&& (pin < 45)

#elif CONFIG_IDF_TARGET_ESP32C3
// Nothing to do

#endif
#endif
;
}

Once the above are all applied, the code compiles and works properly and I've had it running successfully at 115200 baud.

Thinking a little further on this, is it really necessary to have the pin restrictions in the code - I know this is done for good intention to stop people tripping over the pins with special functions or limitations, but could this not be better covered in the documentation, rather than a silent error that is even more difficult to diagnose ?

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions