Skip to content

Commit 90ca393

Browse files
Prevent low pulse on TX initialization in SoftwareSerial
Previously, the TX pin would be set to output first and then written high (assuming non-inverted logic). When the pin was previously configured for input without pullup (which is normal reset state), this results in driving the pin low for a short when initializing. This could accidenttally be seen as a stop bit by the receiving side. By first writing HIGH and then setting the mode to OUTPUT, the pin will have its pullup enabled for a short while, which is harmless.
1 parent 9cf3740 commit 90ca393

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,12 @@ SoftwareSerial::~SoftwareSerial()
266266

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

0 commit comments

Comments
 (0)