Skip to content

Commit

Permalink
Bit writing computation is now concurrent to output timing, improves …
Browse files Browse the repository at this point in the history
…write error rate tremendously.
  • Loading branch information
dok-net committed Nov 7, 2019
1 parent 904e02a commit b977f86
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/SoftwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,27 @@ void ICACHE_RAM_ATTR SoftwareSerial::preciseDelay(bool asyn, uint32_t savedPS) {
if (asyn)
{
resetPeriodStart();
m_periodDuration = 0;
}
if (asyn && !m_intTxEnabled) { savedPS = xt_rsil(15); }
}

void ICACHE_RAM_ATTR SoftwareSerial::writePeriod(
uint32_t dutyCycle, uint32_t offCycle, bool withStopBit, uint32_t savedPS) {
preciseDelay(false, savedPS);
if (dutyCycle) {
digitalWrite(m_txPin, HIGH);
m_periodDuration += dutyCycle;
bool asyn = withStopBit && !m_invert;
// Reenable interrupts while delaying to avoid other tasks piling up
preciseDelay(asyn, savedPS);
if (asyn || offCycle) preciseDelay(asyn, savedPS);
// Disable interrupts again
}
if (offCycle) {
digitalWrite(m_txPin, LOW);
m_periodDuration += offCycle;
bool asyn = withStopBit && m_invert;
// Reenable interrupts while delaying to avoid other tasks piling up
preciseDelay(asyn, savedPS);
if (asyn) preciseDelay(asyn, savedPS);
// Disable interrupts again
}
}
Expand All @@ -235,7 +235,6 @@ size_t ICACHE_RAM_ATTR SoftwareSerial::write(const uint8_t * buffer, size_t size
savedPS = xt_rsil(15);
}
resetPeriodStart();
m_periodDuration = 0;
const uint32_t dataMask = ((1UL << m_dataBits) - 1);
for (size_t cnt = 0; cnt < size; ++cnt, ++buffer) {
bool withStopBit = true;
Expand Down
5 changes: 3 additions & 2 deletions src/SoftwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ class SoftwareSerial : public Stream {
private:
void resetPeriodStart()
{
m_periodDuration = 0;
#if defined(ESP8266)
m_periodStart = ESP.getCycleCount() - 64;
m_periodStart = ESP.getCycleCount() - 50;
#elif defined(ESP32)
m_periodStart = ESP.getCycleCount() - 64;
m_periodStart = ESP.getCycleCount() - 32;
#else
m_periodStart = ESP.getCycleCount();
#endif
Expand Down

0 comments on commit b977f86

Please sign in to comment.