Skip to content

Move SoftwareSerial TX and RX pin setup from constructor to begin #4092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,16 @@ ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
//
// Constructor
//
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
SoftwareSerial::SoftwareSerial(int receivePin, int transmitPin, bool inverse_logic /* = false */) :
_receivePin(receivePin),
_transmitPin(transmitPin),
_rx_delay_centering(0),
_rx_delay_intrabit(0),
_rx_delay_stopbit(0),
_tx_delay(0),
_buffer_overflow(false),
_inverse_logic(inverse_logic)
{
setTX(transmitPin);
setRX(receivePin);
}

//
Expand All @@ -266,27 +266,26 @@ SoftwareSerial::~SoftwareSerial()
end();
}

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

void SoftwareSerial::setRX(uint8_t rx)
void SoftwareSerial::setupRX()
{
pinMode(rx, INPUT);
pinMode(_receivePin, INPUT);
if (!_inverse_logic)
digitalWrite(rx, HIGH); // pullup for normal logic!
_receivePin = rx;
_receiveBitMask = digitalPinToBitMask(rx);
uint8_t port = digitalPinToPort(rx);
digitalWrite(_receivePin, HIGH); // pullup for normal logic!
_receiveBitMask = digitalPinToBitMask(_receivePin);
uint8_t port = digitalPinToPort(_receivePin);
_receivePortRegister = portInputRegister(port);
}

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

void SoftwareSerial::begin(long speed)
{
setupTX();
setupRX();

_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;

// Precalculate the various delays, in number of 4-cycle delays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class SoftwareSerial : public Stream
{
private:
// per object data
uint8_t _receivePin;
int _receivePin;
int _transmitPin;
uint8_t _receiveBitMask;
volatile uint8_t *_receivePortRegister;
uint8_t _transmitBitMask;
Expand All @@ -74,8 +75,8 @@ class SoftwareSerial : public Stream
// private methods
inline void recv() __attribute__((__always_inline__));
uint8_t rx_pin_read();
void setTX(uint8_t transmitPin);
void setRX(uint8_t receivePin);
void setupTX();
void setupRX();
inline void setRxIntMsk(bool enable) __attribute__((__always_inline__));

// Return num - sub, or 1 if the result would be < 1
Expand All @@ -86,7 +87,7 @@ class SoftwareSerial : public Stream

public:
// public methods
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
SoftwareSerial(int receivePin, int transmitPin, bool inverse_logic = false);
~SoftwareSerial();
void begin(long speed);
bool listen();
Expand Down