Skip to content

Commit b977f86

Browse files
committed
Bit writing computation is now concurrent to output timing, improves write error rate tremendously.
1 parent 904e02a commit b977f86

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/SoftwareSerial.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,27 @@ void ICACHE_RAM_ATTR SoftwareSerial::preciseDelay(bool asyn, uint32_t savedPS) {
188188
if (asyn)
189189
{
190190
resetPeriodStart();
191-
m_periodDuration = 0;
192191
}
193192
if (asyn && !m_intTxEnabled) { savedPS = xt_rsil(15); }
194193
}
195194

196195
void ICACHE_RAM_ATTR SoftwareSerial::writePeriod(
197196
uint32_t dutyCycle, uint32_t offCycle, bool withStopBit, uint32_t savedPS) {
197+
preciseDelay(false, savedPS);
198198
if (dutyCycle) {
199199
digitalWrite(m_txPin, HIGH);
200200
m_periodDuration += dutyCycle;
201201
bool asyn = withStopBit && !m_invert;
202202
// Reenable interrupts while delaying to avoid other tasks piling up
203-
preciseDelay(asyn, savedPS);
203+
if (asyn || offCycle) preciseDelay(asyn, savedPS);
204204
// Disable interrupts again
205205
}
206206
if (offCycle) {
207207
digitalWrite(m_txPin, LOW);
208208
m_periodDuration += offCycle;
209209
bool asyn = withStopBit && m_invert;
210210
// Reenable interrupts while delaying to avoid other tasks piling up
211-
preciseDelay(asyn, savedPS);
211+
if (asyn) preciseDelay(asyn, savedPS);
212212
// Disable interrupts again
213213
}
214214
}
@@ -235,7 +235,6 @@ size_t ICACHE_RAM_ATTR SoftwareSerial::write(const uint8_t * buffer, size_t size
235235
savedPS = xt_rsil(15);
236236
}
237237
resetPeriodStart();
238-
m_periodDuration = 0;
239238
const uint32_t dataMask = ((1UL << m_dataBits) - 1);
240239
for (size_t cnt = 0; cnt < size; ++cnt, ++buffer) {
241240
bool withStopBit = true;

src/SoftwareSerial.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ class SoftwareSerial : public Stream {
105105
private:
106106
void resetPeriodStart()
107107
{
108+
m_periodDuration = 0;
108109
#if defined(ESP8266)
109-
m_periodStart = ESP.getCycleCount() - 64;
110+
m_periodStart = ESP.getCycleCount() - 50;
110111
#elif defined(ESP32)
111-
m_periodStart = ESP.getCycleCount() - 64;
112+
m_periodStart = ESP.getCycleCount() - 32;
112113
#else
113114
m_periodStart = ESP.getCycleCount();
114115
#endif

0 commit comments

Comments
 (0)