From 9eb646085eac4cbfe97454ed78d7154efc86467d Mon Sep 17 00:00:00 2001 From: Jason2866 <24528715+Jason2866@users.noreply.github.com> Date: Thu, 27 Feb 2020 09:52:48 +0100 Subject: [PATCH] Reduce CPU load during interrupt handler See https://github.com/esp8266/Arduino/pull/7057#issuecomment-591632232 --- .../dependencies/arduino/core_esp8266_waveform.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/wled00/src/dependencies/arduino/core_esp8266_waveform.cpp b/wled00/src/dependencies/arduino/core_esp8266_waveform.cpp index 6a52b1b3..57b3c315 100644 --- a/wled00/src/dependencies/arduino/core_esp8266_waveform.cpp +++ b/wled00/src/dependencies/arduino/core_esp8266_waveform.cpp @@ -224,8 +224,8 @@ static ICACHE_RAM_ATTR void timer1Interrupt() { endPin = 32 - __builtin_clz(waveformEnabled); } - if (waveformEnabled) { bool done = false; + if (waveformEnabled) { do { nextEventCycles = microsecondsToClockCycles(MAXIRQUS); for (int i = startPin; i <= endPin; i++) { @@ -257,7 +257,13 @@ static ICACHE_RAM_ATTR void timer1Interrupt() { // Check for toggles int32_t cyclesToGo = wave->nextServiceCycle - now; if (cyclesToGo < 0) { - cyclesToGo = -((-cyclesToGo) % (wave->nextTimeHighCycles + wave->nextTimeLowCycles)); + // See #7057 + // The following is a no-op unless we have overshot by an entire waveform cycle. + // As modulus is an expensive operation, this code is removed for now: + // cyclesToGo = -((-cyclesToGo) % (wave->nextTimeHighCycles + wave->nextTimeLowCycles)); + // + // Alternative version with lower CPU impact: + // while (-cyclesToGo > wave->nextTimeHighCycles + wave->nextTimeLowCycles) { cyclesToGo += wave->nextTimeHighCycles + wave->nextTimeLowCycles)}; waveformState ^= mask; if (waveformState & mask) { if (i == 16) { @@ -309,4 +315,4 @@ static ICACHE_RAM_ATTR void timer1Interrupt() { } }; -#endif \ No newline at end of file +#endif