Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae1c946

Browse files
committed
Increase resolution by keeping reference time moving forward earlier.
1 parent 9bc12a1 commit ae1c946

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

cores/esp8266/core_esp8266_waveform.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,13 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
269269
wave.nextPeriodCcy = waveform.pins[wave.alignPhase].nextPeriodCcy + wave.nextPeriodCcy;
270270
if (static_cast<int32_t>(waveform.nextEventCcy - wave.nextPeriodCcy) > 0) {
271271
waveform.nextEventCcy = wave.nextPeriodCcy;
272+
waveform.nextPin = waveform.toSet;
272273
}
273274
}
274275
else {
275-
wave.nextPeriodCcy = ESP.getCycleCount();
276+
wave.nextPeriodCcy = isrStartCcy;
276277
waveform.nextEventCcy = wave.nextPeriodCcy;
278+
waveform.nextPin = waveform.toSet;
277279
}
278280
wave.nextEventCcy = wave.nextPeriodCcy;
279281
if (!wave.expiryCcy) {
@@ -293,23 +295,32 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
293295

294296
// Exit the loop if the next event, if any, is sufficiently distant.
295297
const uint32_t isrTimeoutCcy = isrStartCcy + ISRTIMEOUTCCYS;
298+
uint32_t isrNextEventCcy;
296299
uint32_t busyPins = waveform.enabled;
297-
if (busyPins) {
300+
if (waveform.enabled) {
298301
if (static_cast<int32_t>(waveform.nextEventCcy - isrTimeoutCcy) >= 0) {
299302
busyPins = 0;
300303
}
301304
else {
302-
waveform.nextEventCcy = ESP.getCycleCount() + MAXIRQTICKSCCYS;
303-
}
304-
if (!(waveform.enabled & (1UL << waveform.nextPin))) {
305-
waveform.nextPin = waveform.startPin;
305+
isrNextEventCcy = waveform.nextEventCcy;
306+
waveform.nextEventCcy = isrStartCcy + MAXIRQTICKSCCYS;
307+
if (!(waveform.enabled & (1UL << waveform.nextPin))) {
308+
waveform.nextPin = waveform.startPin;
309+
}
306310
}
307311
}
308-
while (busyPins) {
309-
int stopPin = waveform.nextPin;
310-
int pin = waveform.nextPin;
311-
uint32_t now = ESP.getCycleCount();
312+
else {
313+
waveform.nextEventCcy = isrStartCcy + MAXIRQTICKSCCYS;
314+
}
312315

316+
const int stopPin = waveform.nextPin;
317+
int pin = stopPin;
318+
while (busyPins) {
319+
uint32_t now;
320+
do {
321+
now = ESP.getCycleCount();
322+
} while (static_cast<int32_t>(isrNextEventCcy - now) > 0);
323+
isrNextEventCcy = isrTimeoutCcy;
313324
do {
314325
// If it's not on, ignore
315326
if (!(busyPins & (1UL << pin)))
@@ -395,7 +406,11 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
395406
waveform.nextPin = pin;
396407
}
397408
}
409+
else if (static_cast<int32_t>(isrNextEventCcy - wave.nextEventCcy) > 0) {
410+
isrNextEventCcy = wave.nextEventCcy;
411+
}
398412

413+
now = ESP.getCycleCount();
399414
} while ((pin = (pin < waveform.endPin) ? pin + 1 : waveform.startPin) != stopPin);
400415
}
401416

0 commit comments

Comments
 (0)