Skip to content

Commit

Permalink
Non-functional refactoring; drop redundant check for illegal values o…
Browse files Browse the repository at this point in the history
…f m_rxLastBit.
  • Loading branch information
dok-net committed Nov 14, 2021
1 parent d017e7e commit cff4deb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "EspSoftwareSerial",
"version": "6.15.0",
"version": "6.15.1",
"description": "Implementation of the Arduino software serial for ESP8266/ESP32.",
"keywords": [
"serial", "io", "softwareserial"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=EspSoftwareSerial
version=6.15.0
version=6.15.1
author=Dirk Kaar, Peter Lerup
maintainer=Dirk Kaar <[email protected]>
sentence=Implementation of the Arduino software serial for ESP8266/ESP32.
Expand Down
24 changes: 12 additions & 12 deletions src/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void SoftwareSerial::enableTx(bool on) {
void SoftwareSerial::enableRx(bool on) {
if (m_rxValid) {
if (on) {
m_rxCurBit = m_pduBits - 1;
m_rxLastBit = m_pduBits - 1;
// Init to stop bit level and current cycle
m_isrLastCycle = (ESP.getCycleCount() | 1) ^ m_invert;
if (m_bitCycles >= (ESP.getCpuFreqMHz() * 1000000UL) / 74880UL)
Expand Down Expand Up @@ -469,8 +469,8 @@ void SoftwareSerial::rxBits() {
// and there was also no next start bit yet, so one word may be pending.
// Check that there was no new ISR data received in the meantime, inserting an
// extraneous stop level bit out of sequence breaks rx.
if (m_rxCurBit >= -1 && m_rxCurBit < m_pduBits - 1) {
const uint32_t detectionCycles = (m_pduBits - 1 - m_rxCurBit) * m_bitCycles;
if (m_rxLastBit < m_pduBits - 1) {
const uint32_t detectionCycles = (m_pduBits - 1 - m_rxLastBit) * m_bitCycles;
if (!m_isrBuffer->available() && ESP.getCycleCount() - m_isrLastCycle > detectionCycles) {
// Produce faux stop bit level, prevents start bit maldetection
// cycle's LSB is repurposed for the level bit
Expand All @@ -490,33 +490,33 @@ void SoftwareSerial::rxBits(const uint32_t isrCycle) {
if (cycles % m_bitCycles > (m_bitCycles >> 1)) ++bits;
while (bits > 0) {
// start bit detection
if (m_rxCurBit >= (m_pduBits - 1)) {
if (m_rxLastBit >= (m_pduBits - 1)) {
// leading edge of start bit?
if (level) break;
m_rxCurBit = -1;
m_rxLastBit = -1;
--bits;
continue;
}
// data bits
if (m_rxCurBit >= -1 && m_rxCurBit < (m_dataBits - 1)) {
uint8_t dataBits = min(bits, static_cast<uint32_t>(m_dataBits - 1 - m_rxCurBit));
m_rxCurBit += dataBits;
if (m_rxLastBit < (m_dataBits - 1)) {
uint8_t dataBits = min(bits, static_cast<uint32_t>(m_dataBits - 1 - m_rxLastBit));
m_rxLastBit += dataBits;
bits -= dataBits;
m_rxCurByte >>= dataBits;
if (level) { m_rxCurByte |= (BYTE_ALL_BITS_SET << (8 - dataBits)); }
continue;
}
// parity bit
if (m_parityMode && m_rxCurBit == (m_dataBits - 1)) {
++m_rxCurBit;
if (m_parityMode && m_rxLastBit == (m_dataBits - 1)) {
++m_rxLastBit;
--bits;
m_rxCurParity = level;
continue;
}
// stop bits
// Store the received value in the buffer unless we have an overflow
// if not high stop bit level, discard word
if (bits >= static_cast<uint32_t>(m_pduBits - 1 - m_rxCurBit) && level) {
if (bits >= static_cast<uint32_t>(m_pduBits - 1 - m_rxLastBit) && level) {
m_rxCurByte >>= (sizeof(uint8_t) * 8 - m_dataBits);
if (!m_buffer->push(m_rxCurByte)) {
m_overflow = true;
Expand All @@ -539,7 +539,7 @@ void SoftwareSerial::rxBits(const uint32_t isrCycle) {
}
}
}
m_rxCurBit = m_pduBits - 1;
m_rxLastBit = m_pduBits - 1;
// reset to 0 is important for masked bit logic
m_rxCurByte = 0;
m_rxCurParity = false;
Expand Down
2 changes: 1 addition & 1 deletion src/SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class SoftwareSerial : public Stream {
uint32_t m_bitCycles;
uint8_t m_parityInPos;
uint8_t m_parityOutPos;
int8_t m_rxCurBit; // 0 thru (m_pduBits - m_stopBits - 1): data/parity bits. -1: start bit. (m_pduBits - 1): stop bit.
int8_t m_rxLastBit; // 0 thru (m_pduBits - m_stopBits - 1): data/parity bits. -1: start bit. (m_pduBits - 1): stop bit.
uint8_t m_rxCurByte = 0;
std::unique_ptr<circular_queue<uint8_t> > m_buffer;
std::unique_ptr<circular_queue<uint8_t> > m_parityBuffer;
Expand Down

0 comments on commit cff4deb

Please sign in to comment.