From 0d48f5a4a8431905119b7ca8dd0cce60a9dc5568 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 20 Apr 2020 12:27:29 +0200 Subject: [PATCH] Add ESP32 dual UART to TasmotaSerial --- .../README.md | 1 + .../examples/swsertest/swsertest.ino | 0 .../keywords.txt | 2 +- .../library.json | 4 +- .../library.properties | 4 +- .../src/TasmotaSerial.cpp | 61 ++++++++++++++++--- .../src/TasmotaSerial.h | 8 +++ 7 files changed, 65 insertions(+), 15 deletions(-) rename lib/{TasmotaSerial-2.4.1 => TasmotaSerial-3.0.0}/README.md (86%) rename lib/{TasmotaSerial-2.4.1 => TasmotaSerial-3.0.0}/examples/swsertest/swsertest.ino (100%) rename lib/{TasmotaSerial-2.4.1 => TasmotaSerial-3.0.0}/keywords.txt (96%) rename lib/{TasmotaSerial-2.4.1 => TasmotaSerial-3.0.0}/library.json (83%) rename lib/{TasmotaSerial-2.4.1 => TasmotaSerial-3.0.0}/library.properties (82%) rename lib/{TasmotaSerial-2.4.1 => TasmotaSerial-3.0.0}/src/TasmotaSerial.cpp (91%) rename lib/{TasmotaSerial-2.4.1 => TasmotaSerial-3.0.0}/src/TasmotaSerial.h (96%) diff --git a/lib/TasmotaSerial-2.4.1/README.md b/lib/TasmotaSerial-3.0.0/README.md similarity index 86% rename from lib/TasmotaSerial-2.4.1/README.md rename to lib/TasmotaSerial-3.0.0/README.md index 019aafc07..d2196ed4c 100644 --- a/lib/TasmotaSerial-2.4.1/README.md +++ b/lib/TasmotaSerial-3.0.0/README.md @@ -1,6 +1,7 @@ # TasmotaSerial Implementation of software serial with hardware serial fallback library for the ESP8266 +Implementation of dual UART hardware serial for the ESP32 Allows for several instances to be active at the same time. diff --git a/lib/TasmotaSerial-2.4.1/examples/swsertest/swsertest.ino b/lib/TasmotaSerial-3.0.0/examples/swsertest/swsertest.ino similarity index 100% rename from lib/TasmotaSerial-2.4.1/examples/swsertest/swsertest.ino rename to lib/TasmotaSerial-3.0.0/examples/swsertest/swsertest.ino diff --git a/lib/TasmotaSerial-2.4.1/keywords.txt b/lib/TasmotaSerial-3.0.0/keywords.txt similarity index 96% rename from lib/TasmotaSerial-2.4.1/keywords.txt rename to lib/TasmotaSerial-3.0.0/keywords.txt index 9cf6d825c..f9bde9254 100644 --- a/lib/TasmotaSerial-2.4.1/keywords.txt +++ b/lib/TasmotaSerial-3.0.0/keywords.txt @@ -1,6 +1,6 @@ ####################################### # Syntax Coloring Map for TasmotaSerial -# (esp8266) +# (esp8266 and esp32) ####################################### ####################################### diff --git a/lib/TasmotaSerial-2.4.1/library.json b/lib/TasmotaSerial-3.0.0/library.json similarity index 83% rename from lib/TasmotaSerial-2.4.1/library.json rename to lib/TasmotaSerial-3.0.0/library.json index 64cde09c9..0ebba7f95 100644 --- a/lib/TasmotaSerial-2.4.1/library.json +++ b/lib/TasmotaSerial-3.0.0/library.json @@ -1,10 +1,10 @@ { "name": "TasmotaSerial", - "version": "2.4.1", + "version": "3.0.0", "keywords": [ "serial", "io", "TasmotaSerial" ], - "description": "Implementation of software serial with hardware serial fallback for ESP8266.", + "description": "Implementation of software serial with hardware serial fallback for ESP8266 and ESP32.", "repository": { "type": "git", diff --git a/lib/TasmotaSerial-2.4.1/library.properties b/lib/TasmotaSerial-3.0.0/library.properties similarity index 82% rename from lib/TasmotaSerial-2.4.1/library.properties rename to lib/TasmotaSerial-3.0.0/library.properties index b326d7404..e2d92a4e8 100644 --- a/lib/TasmotaSerial-2.4.1/library.properties +++ b/lib/TasmotaSerial-3.0.0/library.properties @@ -1,8 +1,8 @@ name=TasmotaSerial -version=2.4.1 +version=3.0.0 author=Theo Arends maintainer=Theo Arends -sentence=Implementation of software serial with hardware serial fallback for ESP8266. +sentence=Implementation of software serial with hardware serial fallback for ESP8266 and ESP32. paragraph= category=Signal Input/Output url= diff --git a/lib/TasmotaSerial-2.4.1/src/TasmotaSerial.cpp b/lib/TasmotaSerial-3.0.0/src/TasmotaSerial.cpp similarity index 91% rename from lib/TasmotaSerial-2.4.1/src/TasmotaSerial.cpp rename to lib/TasmotaSerial-3.0.0/src/TasmotaSerial.cpp index 967fe5992..947e9dcc8 100644 --- a/lib/TasmotaSerial-2.4.1/src/TasmotaSerial.cpp +++ b/lib/TasmotaSerial-3.0.0/src/TasmotaSerial.cpp @@ -79,6 +79,10 @@ static void (*ISRList[16])() = { tms_isr_15 }; +#ifdef ESP32 +static uint8_t tasmota_serial_index = 3; +#endif + TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fallback, int nwmode, int buffer_size) { m_valid = false; @@ -161,12 +165,29 @@ bool TasmotaSerial::begin(long speed, int stop_bits) { Serial.swap(); } #else // ESP32 - Serial2.flush(); - if (2 == m_stop_bits) { - Serial2.begin(speed, SERIAL_8N2, m_rx_pin, m_tx_pin); - } else { - Serial2.begin(speed, SERIAL_8N1, m_rx_pin, m_tx_pin); + tasmota_serial_index--; + if (2 == tasmota_serial_index) { + m_uart = 2; + Serial2.flush(); + if (2 == m_stop_bits) { + Serial2.begin(speed, SERIAL_8N2, m_rx_pin, m_tx_pin); + } else { + Serial2.begin(speed, SERIAL_8N1, m_rx_pin, m_tx_pin); + } } + else if (1 == tasmota_serial_index) { + m_uart = 1; + Serial1.flush(); + if (2 == m_stop_bits) { + Serial1.begin(speed, SERIAL_8N2, m_rx_pin, m_tx_pin); + } else { + Serial1.begin(speed, SERIAL_8N1, m_rx_pin, m_tx_pin); + } + } + else { + m_valid = false; + } + Serial.printf("TSR: Using UART%d\n", m_uart); #endif // ESP8266 - ESP32 } else { // Use getCycleCount() loop to get as exact timing as possible @@ -195,7 +216,11 @@ void TasmotaSerial::flush() { #ifdef ESP8266 Serial.flush(); #else - Serial2.flush(); + if (2 == m_uart) { + Serial2.flush(); + } else { + Serial1.flush(); + } #endif } else { m_in_pos = m_out_pos = 0; @@ -207,7 +232,11 @@ int TasmotaSerial::peek() { #ifdef ESP8266 return Serial.peek(); #else - return Serial2.peek(); + if (2 == m_uart) { + return Serial2.peek(); + } else{ + return Serial1.peek(); + } #endif } else { if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) return -1; @@ -221,7 +250,11 @@ int TasmotaSerial::read() #ifdef ESP8266 return Serial.read(); #else - return Serial2.read(); + if (2 == m_uart) { + return Serial2.read(); + } else { + return Serial1.read(); + } #endif } else { if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) return -1; @@ -237,7 +270,11 @@ int TasmotaSerial::available() #ifdef ESP8266 return Serial.available(); #else - return Serial2.available(); + if (2 == m_uart) { + return Serial2.available(); + } else { + return Serial1.available(); + } #endif } else { int avail = m_in_pos - m_out_pos; @@ -286,7 +323,11 @@ size_t TasmotaSerial::write(uint8_t b) #ifdef ESP8266 return Serial.write(b); #else - return Serial2.write(b); + if (2 == m_uart) { + return Serial2.write(b); + } else { + return Serial1.write(b); + } #endif } else { if (-1 == m_tx_pin) return 0; diff --git a/lib/TasmotaSerial-2.4.1/src/TasmotaSerial.h b/lib/TasmotaSerial-3.0.0/src/TasmotaSerial.h similarity index 96% rename from lib/TasmotaSerial-2.4.1/src/TasmotaSerial.h rename to lib/TasmotaSerial-3.0.0/src/TasmotaSerial.h index 3ef4ee43b..d52660a54 100644 --- a/lib/TasmotaSerial-2.4.1/src/TasmotaSerial.h +++ b/lib/TasmotaSerial-3.0.0/src/TasmotaSerial.h @@ -55,6 +55,10 @@ class TasmotaSerial : public Stream { uint32_t getLoopReadMetric(void) const { return m_bit_follow_metric; } +#ifdef ESP32 + uint32_t getUart(void) const { return m_uart; } +#endif + using Print::write; private: @@ -82,6 +86,10 @@ class TasmotaSerial : public Stream { bool m_very_high_speed = false; // above 100000 bauds uint8_t *m_buffer; +#ifdef ESP32 + uint8_t m_uart = 0; +#endif + void _fast_write(uint8_t b); // IRAM minimized version };