Skip to content

Commit 907db30

Browse files
committed
Add API for selecting Rx GPIO PULLUP at runtime.
1 parent bdcbe74 commit 907db30

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "EspSoftwareSerial",
3-
"version": "6.13.2",
3+
"version": "6.14.0",
44
"description": "Implementation of the Arduino software serial for ESP8266/ESP32.",
55
"keywords": [
66
"serial", "io", "softwareserial"

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=EspSoftwareSerial
2-
version=6.13.2
2+
version=6.14.0
33
author=Dirk Kaar, Peter Lerup
44
maintainer=Dirk Kaar <dok@dok-net.net>
55
sentence=Implementation of the Arduino software serial for ESP8266/ESP32.

src/SoftwareSerial.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ bool SoftwareSerial::hasRxGPIOPullUp(int8_t pin) {
107107
#endif
108108
}
109109

110+
void SoftwareSerial::setRxGPIOPullUp() {
111+
if (m_rxValid) {
112+
pinMode(m_rxPin, hasRxGPIOPullUp(m_rxPin) && m_rxGPIOPullupEnabled ? INPUT_PULLUP : INPUT);
113+
}
114+
}
115+
110116
void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config,
111117
int8_t rxPin, int8_t txPin,
112118
bool invert, int bufCapacity, int isrBufCapacity) {
@@ -120,6 +126,7 @@ void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config,
120126
m_pduBits = m_dataBits + static_cast<bool>(m_parityMode) + m_stopBits;
121127
m_bitCycles = (ESP.getCpuFreqMHz() * 1000000UL + baud / 2) / baud;
122128
m_intTxEnabled = true;
129+
m_rxGPIOPullupEnabled = true;
123130
if (isValidRxGPIOpin(m_rxPin)) {
124131
m_buffer.reset(new circular_queue<uint8_t>((bufCapacity > 0) ? bufCapacity : 64));
125132
if (m_parityMode)
@@ -131,7 +138,7 @@ void SoftwareSerial::begin(uint32_t baud, SoftwareSerialConfig config,
131138
isrBufCapacity : m_buffer->capacity() * (2 + m_dataBits + static_cast<bool>(m_parityMode))));
132139
if (m_buffer && (!m_parityMode || m_parityBuffer) && m_isrBuffer) {
133140
m_rxValid = true;
134-
pinMode(m_rxPin, hasRxGPIOPullUp(m_rxPin) ? INPUT_PULLUP : INPUT);
141+
setRxGPIOPullUp();
135142
}
136143
}
137144
if (isValidTxGPIOpin(m_txPin)) {
@@ -177,6 +184,11 @@ void SoftwareSerial::enableIntTx(bool on) {
177184
m_intTxEnabled = on;
178185
}
179186

187+
void SoftwareSerial::enableRxGPIOPullup(bool on) {
188+
m_rxGPIOPullupEnabled = on;
189+
setRxGPIOPullUp();
190+
}
191+
180192
void SoftwareSerial::enableTx(bool on) {
181193
if (m_txValid && m_oneWire) {
182194
if (on) {
@@ -185,7 +197,7 @@ void SoftwareSerial::enableTx(bool on) {
185197
digitalWrite(m_txPin, !m_invert);
186198
}
187199
else {
188-
pinMode(m_rxPin, hasRxGPIOPullUp(m_rxPin) ? INPUT_PULLUP : INPUT);
200+
setRxGPIOPullUp();
189201
enableRx(true);
190202
}
191203
}

src/SoftwareSerial.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ class SoftwareSerial : public Stream {
122122
uint32_t baudRate();
123123
/// Transmit control pin.
124124
void setTransmitEnablePin(int8_t txEnablePin);
125-
/// Enable or disable interrupts during tx.
125+
/// Enable (default) or disable interrupts during tx.
126126
void enableIntTx(bool on);
127+
/// Enable (default) or disable internal rx GPIO pullup.
128+
void enableRxGPIOPullup(bool on);
127129

128130
bool overflow();
129131

@@ -222,6 +224,8 @@ class SoftwareSerial : public Stream {
222224
bool isValidTxGPIOpin(int8_t pin);
223225
// result is only defined for a valid Rx GPIO pin
224226
bool hasRxGPIOPullUp(int8_t pin);
227+
// safely set the pin mode for the Rx GPIO pin
228+
void setRxGPIOPullUp();
225229
/* check m_rxValid that calling is safe */
226230
void rxBits();
227231
void rxBits(const uint32_t& isrCycle);
@@ -243,6 +247,7 @@ class SoftwareSerial : public Stream {
243247
/// PDU bits include data, parity and stop bits; the start bit is not counted.
244248
uint8_t m_pduBits;
245249
bool m_intTxEnabled;
250+
bool m_rxGPIOPullupEnabled;
246251
SoftwareSerialParity m_parityMode;
247252
uint8_t m_stopBits;
248253
bool m_lastReadParity;

0 commit comments

Comments
 (0)