Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f00c1c1

Browse files
peteruithovenfacchinm
authored andcommitted
[SoftSerial] Half duplex support
Squash and rebase of arduino/Arduino#4377
1 parent b084848 commit f00c1c1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

libraries/SoftwareSerial/src/SoftwareSerial.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,14 @@ ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
246246
//
247247
// Constructor
248248
//
249-
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) :
249+
SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */, bool full_duplex /* = true */) :
250250
_rx_delay_centering(0),
251251
_rx_delay_intrabit(0),
252252
_rx_delay_stopbit(0),
253253
_tx_delay(0),
254254
_buffer_overflow(false),
255-
_inverse_logic(inverse_logic)
255+
_inverse_logic(inverse_logic),
256+
_full_duplex(full_duplex)
256257
{
257258
setTX(transmitPin);
258259
setRX(receivePin);
@@ -273,7 +274,11 @@ void SoftwareSerial::setTX(uint8_t tx)
273274
// output high. Now, it is input with pullup for a short while, which
274275
// is fine. With inverse logic, either order is fine.
275276
digitalWrite(tx, _inverse_logic ? LOW : HIGH);
276-
pinMode(tx, OUTPUT);
277+
if(_full_duplex)
278+
pinMode(tx, OUTPUT);
279+
else
280+
pinMode(tx, INPUT);
281+
_transmitPin = tx;
277282
_transmitBitMask = digitalPinToBitMask(tx);
278283
uint8_t port = digitalPinToPort(tx);
279284
_transmitPortRegister = portOutputRegister(port);
@@ -418,6 +423,9 @@ size_t SoftwareSerial::write(uint8_t b)
418423
setWriteError();
419424
return 0;
420425
}
426+
427+
if(!_full_duplex)
428+
pinMode(_transmitPin, OUTPUT);
421429

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

472+
if(!_full_duplex){
473+
pinMode(_transmitPin, INPUT);
474+
*reg |= reg_mask; // send 1
475+
}
476+
464477
SREG = oldSREG; // turn interrupts back on
465478
tunedDelay(_tx_delay);
466479

libraries/SoftwareSerial/src/SoftwareSerial.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class SoftwareSerial : public Stream
5454
uint8_t _receivePin;
5555
uint8_t _receiveBitMask;
5656
volatile uint8_t *_receivePortRegister;
57+
uint8_t _transmitPin;
5758
uint8_t _transmitBitMask;
5859
volatile uint8_t *_transmitPortRegister;
5960
volatile uint8_t *_pcint_maskreg;
@@ -67,6 +68,7 @@ class SoftwareSerial : public Stream
6768

6869
uint16_t _buffer_overflow:1;
6970
uint16_t _inverse_logic:1;
71+
uint16_t _full_duplex:1;
7072

7173
// static data
7274
static uint8_t _receive_buffer[_SS_MAX_RX_BUFF];
@@ -89,7 +91,7 @@ class SoftwareSerial : public Stream
8991

9092
public:
9193
// public methods
92-
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
94+
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false, bool full_duplex = true);
9395
~SoftwareSerial();
9496
void begin(long speed);
9597
bool listen();

0 commit comments

Comments
 (0)