Skip to content

Commit 548e151

Browse files
committed
Parity calculation for odd data bit counts was inverted. Fixes #170
1 parent 27f0804 commit 548e151

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/SoftwareSerial.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,11 @@ size_t ICACHE_RAM_ATTR SoftwareSerial::write(const uint8_t* buffer, size_t size,
300300
m_periodDuration = 0;
301301
m_periodStart = ESP.getCycleCount();
302302
for (size_t cnt = 0; cnt < size; ++cnt) {
303-
uint8_t byte = ~buffer[cnt] & dataMask;
303+
uint8_t byte = buffer[cnt] & dataMask;
304304
// push LSB start-data-parity-stop bit pattern into uint32_t
305305
// Stop bits: HIGH
306306
uint32_t word = ~0UL;
307-
// parity bit, if any
307+
// inverted parity bit, performance tweak for xor all-bits-set word
308308
if (parity && m_parityMode)
309309
{
310310
uint32_t parityBit;
@@ -325,18 +325,19 @@ size_t ICACHE_RAM_ATTR SoftwareSerial::write(const uint8_t* buffer, size_t size,
325325
parityBit = (0x6996 >> parityBit) & 1;
326326
break;
327327
case SWSERIAL_PARITY_MARK:
328-
parityBit = false;
328+
parityBit = 0;
329329
break;
330330
case SWSERIAL_PARITY_SPACE:
331331
// suppresses warning parityBit uninitialized
332332
default:
333-
parityBit = true;
333+
parityBit = 1;
334334
break;
335335
}
336-
word ^= parityBit << m_dataBits;
336+
word ^= parityBit;
337337
}
338-
word ^= byte;
339-
// Stop bit: LOW
338+
word <<= m_dataBits;
339+
word |= byte;
340+
// Start bit: LOW
340341
word <<= 1;
341342
if (m_invert) word = ~word;
342343
for (int i = 0; i <= m_pduBits; ++i) {

0 commit comments

Comments
 (0)