Skip to content

Commit

Permalink
Add API for selecting Rx GPIO PULLUP at runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
dok-net committed Oct 30, 2021
1 parent bdcbe74 commit 907db30
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=EspSoftwareSerial
version=6.13.2
version=6.14.0
author=Dirk Kaar, Peter Lerup
maintainer=Dirk Kaar <[email protected]>
sentence=Implementation of the Arduino software serial for ESP8266/ESP32.
Expand Down
16 changes: 14 additions & 2 deletions src/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -120,6 +126,7 @@ void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config,
m_pduBits = m_dataBits + static_cast<bool>(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<uint8_t>((bufCapacity > 0) ? bufCapacity : 64));
if (m_parityMode)
Expand All @@ -131,7 +138,7 @@ void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config,
isrBufCapacity : m_buffer->capacity() * (2 + m_dataBits + static_cast<bool>(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)) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 907db30

Please sign in to comment.