Skip to content

Commit 8c8b78c

Browse files
committed
Move SoftwareSerial TX and RX pin setup from constructor to begin as per #3041
1 parent 7380750 commit 8c8b78c

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@ ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
247247
// Constructor
248248
//
249249
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
250+
_receivePin(receivePin),
251+
_transmitPin(transmitPin),
250252
_rx_delay_centering(0),
251253
_rx_delay_intrabit(0),
252254
_rx_delay_stopbit(0),
253255
_tx_delay(0),
254256
_buffer_overflow(false),
255257
_inverse_logic(inverse_logic)
256258
{
257-
setTX(transmitPin);
258-
setRX(receivePin);
259259
}
260260

261261
//
@@ -266,27 +266,26 @@ SoftwareSerial::~SoftwareSerial()
266266
end();
267267
}
268268

269-
void SoftwareSerial::setTX(uint8_t tx)
269+
void SoftwareSerial::setupTX()
270270
{
271271
// First write, then set output. If we do this the other way around,
272272
// the pin would be output low for a short while before switching to
273273
// output hihg. Now, it is input with pullup for a short while, which
274274
// is fine. With inverse logic, either order is fine.
275-
digitalWrite(tx, _inverse_logic ? LOW : HIGH);
276-
pinMode(tx, OUTPUT);
277-
_transmitBitMask = digitalPinToBitMask(tx);
278-
uint8_t port = digitalPinToPort(tx);
275+
digitalWrite(_transmitPin, _inverse_logic ? LOW : HIGH);
276+
pinMode(_transmitPin, OUTPUT);
277+
_transmitBitMask = digitalPinToBitMask(_transmitPin);
278+
uint8_t port = digitalPinToPort(_transmitPin);
279279
_transmitPortRegister = portOutputRegister(port);
280280
}
281281

282-
void SoftwareSerial::setRX(uint8_t rx)
282+
void SoftwareSerial::setupRX()
283283
{
284-
pinMode(rx, INPUT);
284+
pinMode(_receivePin, INPUT);
285285
if (!_inverse_logic)
286-
digitalWrite(rx, HIGH); // pullup for normal logic!
287-
_receivePin = rx;
288-
_receiveBitMask = digitalPinToBitMask(rx);
289-
uint8_t port = digitalPinToPort(rx);
286+
digitalWrite(_receivePin, HIGH); // pullup for normal logic!
287+
_receiveBitMask = digitalPinToBitMask(_receivePin);
288+
uint8_t port = digitalPinToPort(_receivePin);
290289
_receivePortRegister = portInputRegister(port);
291290
}
292291

@@ -303,6 +302,9 @@ uint16_t SoftwareSerial::subtract_cap(uint16_t num, uint16_t sub) {
303302

304303
void SoftwareSerial::begin(long speed)
305304
{
305+
setupTX();
306+
setupRX();
307+
306308
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
307309

308310
// Precalculate the various delays, in number of 4-cycle delays

hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SoftwareSerial : public Stream
4949
private:
5050
// per object data
5151
uint8_t _receivePin;
52+
uint8_t _transmitPin;
5253
uint8_t _receiveBitMask;
5354
volatile uint8_t *_receivePortRegister;
5455
uint8_t _transmitBitMask;
@@ -74,8 +75,8 @@ class SoftwareSerial : public Stream
7475
// private methods
7576
inline void recv() __attribute__((__always_inline__));
7677
uint8_t rx_pin_read();
77-
void setTX(uint8_t transmitPin);
78-
void setRX(uint8_t receivePin);
78+
void setupTX();
79+
void setupRX();
7980
inline void setRxIntMsk(bool enable) __attribute__((__always_inline__));
8081

8182
// Return num - sub, or 1 if the result would be < 1

0 commit comments

Comments
 (0)