From 907db302f27994f6cd00c81fcbd9a7a691407aea Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sat, 30 Oct 2021 08:34:24 +0200 Subject: [PATCH] Add API for selecting Rx GPIO PULLUP at runtime. --- library.json | 2 +- library.properties | 2 +- src/SoftwareSerial.cpp | 16 ++++++++++++++-- src/SoftwareSerial.h | 7 ++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/library.json b/library.json index 3dcf161..ec31f7b 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "EspSoftwareSerial", - "version": "6.13.2", + "version": "6.14.0", "description": "Implementation of the Arduino software serial for ESP8266/ESP32.", "keywords": [ "serial", "io", "softwareserial" diff --git a/library.properties b/library.properties index e49c8b1..73727d1 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=EspSoftwareSerial -version=6.13.2 +version=6.14.0 author=Dirk Kaar, Peter Lerup maintainer=Dirk Kaar sentence=Implementation of the Arduino software serial for ESP8266/ESP32. diff --git a/src/SoftwareSerial.cpp b/src/SoftwareSerial.cpp index f9bb82d..c947871 100644 --- a/src/SoftwareSerial.cpp +++ b/src/SoftwareSerial.cpp @@ -107,6 +107,12 @@ bool SoftwareSerial::hasRxGPIOPullUp(int8_t pin) { #endif } +void SoftwareSerial::setRxGPIOPullUp() { + if (m_rxValid) { + pinMode(m_rxPin, hasRxGPIOPullUp(m_rxPin) && m_rxGPIOPullupEnabled ? INPUT_PULLUP : INPUT); + } +} + void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config, int8_t rxPin, int8_t txPin, bool invert, int bufCapacity, int isrBufCapacity) { @@ -120,6 +126,7 @@ void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config, m_pduBits = m_dataBits + static_cast(m_parityMode) + m_stopBits; m_bitCycles = (ESP.getCpuFreqMHz() * 1000000UL + baud / 2) / baud; m_intTxEnabled = true; + m_rxGPIOPullupEnabled = true; if (isValidRxGPIOpin(m_rxPin)) { m_buffer.reset(new circular_queue((bufCapacity > 0) ? bufCapacity : 64)); if (m_parityMode) @@ -131,7 +138,7 @@ void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config, isrBufCapacity : m_buffer->capacity() * (2 + m_dataBits + static_cast(m_parityMode)))); if (m_buffer && (!m_parityMode || m_parityBuffer) && m_isrBuffer) { m_rxValid = true; - pinMode(m_rxPin, hasRxGPIOPullUp(m_rxPin) ? INPUT_PULLUP : INPUT); + setRxGPIOPullUp(); } } if (isValidTxGPIOpin(m_txPin)) { @@ -177,6 +184,11 @@ void SoftwareSerial::enableIntTx(bool on) { m_intTxEnabled = on; } +void SoftwareSerial::enableRxGPIOPullup(bool on) { + m_rxGPIOPullupEnabled = on; + setRxGPIOPullUp(); +} + void SoftwareSerial::enableTx(bool on) { if (m_txValid && m_oneWire) { if (on) { @@ -185,7 +197,7 @@ void SoftwareSerial::enableTx(bool on) { digitalWrite(m_txPin, !m_invert); } else { - pinMode(m_rxPin, hasRxGPIOPullUp(m_rxPin) ? INPUT_PULLUP : INPUT); + setRxGPIOPullUp(); enableRx(true); } } diff --git a/src/SoftwareSerial.h b/src/SoftwareSerial.h index aa876a3..07fff95 100644 --- a/src/SoftwareSerial.h +++ b/src/SoftwareSerial.h @@ -122,8 +122,10 @@ class SoftwareSerial : public Stream { uint32_t baudRate(); /// Transmit control pin. void setTransmitEnablePin(int8_t txEnablePin); - /// Enable or disable interrupts during tx. + /// Enable (default) or disable interrupts during tx. void enableIntTx(bool on); + /// Enable (default) or disable internal rx GPIO pullup. + void enableRxGPIOPullup(bool on); bool overflow(); @@ -222,6 +224,8 @@ class SoftwareSerial : public Stream { bool isValidTxGPIOpin(int8_t pin); // result is only defined for a valid Rx GPIO pin bool hasRxGPIOPullUp(int8_t pin); + // safely set the pin mode for the Rx GPIO pin + void setRxGPIOPullUp(); /* check m_rxValid that calling is safe */ void rxBits(); void rxBits(const uint32_t& isrCycle); @@ -243,6 +247,7 @@ class SoftwareSerial : public Stream { /// PDU bits include data, parity and stop bits; the start bit is not counted. uint8_t m_pduBits; bool m_intTxEnabled; + bool m_rxGPIOPullupEnabled; SoftwareSerialParity m_parityMode; uint8_t m_stopBits; bool m_lastReadParity;