Skip to content

Commit c3c5c9c

Browse files
committed
Improved performance of loop.
1 parent 05df6eb commit c3c5c9c

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

cores/esp8266/core_esp8266_waveform.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ namespace {
9191

9292
bool timer1Running = false;
9393

94-
// Optimize the NMI inner loop by keeping track of the min and max GPIO that we
95-
// are generating. In the common case (1 PWM) these may be the same pin and
96-
// we can avoid looking at the other pins.
97-
int startPin = 0;
98-
int endPin = 0;
9994
uint32_t nextEventCcy;
10095
} waveform;
10196

@@ -266,9 +261,6 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
266261
// Handle enable/disable requests from main app.
267262
waveform.enabled = (waveform.enabled & ~waveform.toDisableBits) | waveform.toSetBits; // Set the requested waveforms on/off
268263
// Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t)
269-
waveform.startPin = __builtin_ffs(waveform.enabled) - 1;
270-
// Find the last bit by subtracting off GCC's count-leading-zeros (no offset in this one)
271-
waveform.endPin = 32 - __builtin_clz(waveform.enabled);
272264
waveform.toDisableBits = 0;
273265
}
274266

@@ -319,11 +311,11 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
319311
break;
320312
}
321313
isrNextEventCcy = waveform.nextEventCcy;
322-
for (int pin = waveform.startPin; pin <= waveform.endPin; ++pin) {
314+
uint32_t loopPins = busyPins;
315+
while (loopPins) {
316+
const int pin = __builtin_ffsl(loopPins) - 1;
323317
const uint32_t pinBit = 1UL << pin;
324-
// If it's not on, ignore
325-
if (!(busyPins & pinBit))
326-
continue;
318+
loopPins ^= pinBit;
327319

328320
Waveform& wave = waveform.pins[pin];
329321

0 commit comments

Comments
 (0)