@@ -469,8 +469,8 @@ void SoftwareSerial::rxBits() {
469469 // and there was also no next start bit yet, so one word may be pending.
470470 // Check that there was no new ISR data received in the meantime, inserting an
471471 // extraneous stop level bit out of sequence breaks rx.
472- if (m_rxCurBit >= -1 && m_rxCurBit < m_pduBits - m_stopBits ) {
473- const uint32_t detectionCycles = (m_pduBits - m_stopBits - m_rxCurBit) * m_bitCycles;
472+ if (m_rxCurBit >= -1 && m_rxCurBit < m_pduBits - 1 ) {
473+ const uint32_t detectionCycles = (m_pduBits - 1 - m_rxCurBit) * m_bitCycles;
474474 if (!m_isrBuffer->available () && ESP.getCycleCount () - m_isrLastCycle > detectionCycles) {
475475 // Produce faux stop bit level, prevents start bit maldetection
476476 // cycle's LSB is repurposed for the level bit
@@ -479,8 +479,8 @@ void SoftwareSerial::rxBits() {
479479 }
480480}
481481
482- void SoftwareSerial::rxBits (uint32_t isrCycle) {
483- bool level = (m_isrLastCycle & 1 ) ^ m_invert;
482+ void SoftwareSerial::rxBits (const uint32_t isrCycle) {
483+ const bool level = (m_isrLastCycle & 1 ) ^ m_invert;
484484
485485 // error introduced by edge value in LSB of isrCycle is negligible
486486 uint32_t cycles = isrCycle - m_isrLastCycle;
@@ -491,7 +491,7 @@ void SoftwareSerial::rxBits(uint32_t isrCycle) {
491491 while (bits > 0 ) {
492492 // start bit detection
493493 if (m_rxCurBit >= (m_pduBits - 1 )) {
494- // leading edge of start bit
494+ // leading edge of start bit?
495495 if (level) break ;
496496 m_rxCurBit = -1 ;
497497 --bits;
@@ -514,44 +514,35 @@ void SoftwareSerial::rxBits(uint32_t isrCycle) {
514514 continue ;
515515 }
516516 // stop bits
517- if (m_rxCurBit < (m_pduBits - m_stopBits - 1 )) {
518- ++m_rxCurBit;
519- --bits;
520- continue ;
521- }
522- if (m_rxCurBit == (m_pduBits - m_stopBits - 1 )) {
523- // Store the received value in the buffer unless we have an overflow
524- // if not high stop bit level, discard word
525- if (level)
526- {
527- m_rxCurByte >>= (sizeof (uint8_t ) * 8 - m_dataBits);
528- if (!m_buffer->push (m_rxCurByte)) {
529- m_overflow = true ;
530- }
531- else {
532- if (m_parityBuffer)
517+ // Store the received value in the buffer unless we have an overflow
518+ // if not high stop bit level, discard word
519+ if (bits >= static_cast <uint32_t >(m_pduBits - 1 - m_rxCurBit) && level) {
520+ m_rxCurByte >>= (sizeof (uint8_t ) * 8 - m_dataBits);
521+ if (!m_buffer->push (m_rxCurByte)) {
522+ m_overflow = true ;
523+ }
524+ else {
525+ if (m_parityBuffer)
526+ {
527+ if (m_rxCurParity) {
528+ m_parityBuffer->pushpeek () |= m_parityInPos;
529+ }
530+ else {
531+ m_parityBuffer->pushpeek () &= ~m_parityInPos;
532+ }
533+ m_parityInPos <<= 1 ;
534+ if (!m_parityInPos)
533535 {
534- if (m_rxCurParity) {
535- m_parityBuffer->pushpeek () |= m_parityInPos;
536- }
537- else {
538- m_parityBuffer->pushpeek () &= ~m_parityInPos;
539- }
540- m_parityInPos <<= 1 ;
541- if (!m_parityInPos)
542- {
543- m_parityBuffer->push ();
544- m_parityInPos = 1 ;
545- }
536+ m_parityBuffer->push ();
537+ m_parityInPos = 1 ;
546538 }
547539 }
548540 }
549- m_rxCurBit = m_pduBits;
550- // reset to 0 is important for masked bit logic
551- m_rxCurByte = 0 ;
552- m_rxCurParity = false ;
553- break ;
554541 }
542+ m_rxCurBit = m_pduBits - 1 ;
543+ // reset to 0 is important for masked bit logic
544+ m_rxCurByte = 0 ;
545+ m_rxCurParity = false ;
555546 break ;
556547 }
557548}
0 commit comments