Skip to content

Half duplex support #4377

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 1 commit 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
19 changes: 16 additions & 3 deletions hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@ ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
//
// Constructor
//
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */, bool full_duplex /* = true */) :
_rx_delay_centering(0),
_rx_delay_intrabit(0),
_rx_delay_stopbit(0),
_tx_delay(0),
_buffer_overflow(false),
_inverse_logic(inverse_logic)
_inverse_logic(inverse_logic),
_full_duplex(full_duplex)
{
setTX(transmitPin);
setRX(receivePin);
Expand All @@ -273,7 +274,11 @@ void SoftwareSerial::setTX(uint8_t tx)
// 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);
if(_full_duplex)
pinMode(tx, OUTPUT);
else
pinMode(tx, INPUT);
_transmitPin = tx;
_transmitBitMask = digitalPinToBitMask(tx);
uint8_t port = digitalPinToPort(tx);
_transmitPortRegister = portOutputRegister(port);
Expand Down Expand Up @@ -418,6 +423,9 @@ size_t SoftwareSerial::write(uint8_t b)
setWriteError();
return 0;
}

if(!_full_duplex)
pinMode(_transmitPin, OUTPUT);

// By declaring these as local variables, the compiler will put them
// in registers _before_ disabling interrupts and entering the
Expand Down Expand Up @@ -461,6 +469,11 @@ size_t SoftwareSerial::write(uint8_t b)
else
*reg |= reg_mask;

if(!_full_duplex){
pinMode(_transmitPin, INPUT);
*reg |= reg_mask; // send 1
}

SREG = oldSREG; // turn interrupts back on
tunedDelay(_tx_delay);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SoftwareSerial : public Stream
uint8_t _receivePin;
uint8_t _receiveBitMask;
volatile uint8_t *_receivePortRegister;
uint8_t _transmitPin;
uint8_t _transmitBitMask;
volatile uint8_t *_transmitPortRegister;
volatile uint8_t *_pcint_maskreg;
Expand All @@ -64,6 +65,7 @@ class SoftwareSerial : public Stream

uint16_t _buffer_overflow:1;
uint16_t _inverse_logic:1;
uint16_t _full_duplex:1;

// static data
static char _receive_buffer[_SS_MAX_RX_BUFF];
Expand All @@ -86,7 +88,7 @@ class SoftwareSerial : public Stream

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