Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4638911

Browse filesBrowse files
committed
Persist next event cycle across ISR invocations, like initPin was before.
1 parent 0dacfc0 commit 4638911
Copy full SHA for 4638911

File tree

Expand file treeCollapse file tree

1 file changed

+30
-23
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+30
-23
lines changed

‎cores/esp8266/core_esp8266_waveform.cpp

Copy file name to clipboardExpand all lines: cores/esp8266/core_esp8266_waveform.cpp
+30-23Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ namespace {
103103
int startPin = 0;
104104
int endPin = 0;
105105
int nextPin = 0;
106+
uint32_t nextEventCcy;
106107
} waveform;
107108

108109
}
@@ -259,15 +260,21 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
259260
waveform.toDisable = -1;
260261
}
261262

262-
uint32_t now = ESP.getCycleCount();
263-
264263
if (toSetMask) {
265264
Waveform& wave = waveform.pins[waveform.toSet];
266265
switch (wave.mode) {
267266
case WaveformMode::INIT:
268267
waveform.states &= ~toSetMask; // Clear the state of any just started
269-
wave.nextPeriodCcy = (wave.alignPhase >= 0 && waveform.enabled & (1UL << wave.alignPhase)) ?
270-
waveform.pins[wave.alignPhase].nextPeriodCcy + wave.nextPeriodCcy : now;
268+
if (wave.alignPhase >= 0 && waveform.enabled & (1UL << wave.alignPhase)) {
269+
wave.nextPeriodCcy = waveform.pins[wave.alignPhase].nextPeriodCcy + wave.nextPeriodCcy;
270+
if (static_cast<int32_t>(waveform.nextEventCcy - wave.nextPeriodCcy) > 0) {
271+
waveform.nextEventCcy = wave.nextPeriodCcy;
272+
}
273+
}
274+
else {
275+
wave.nextPeriodCcy = ESP.getCycleCount();
276+
waveform.nextEventCcy = wave.nextPeriodCcy;
277+
}
271278
wave.nextEventCcy = wave.nextPeriodCcy;
272279
if (!wave.expiryCcy) {
273280
wave.mode = WaveformMode::INFINITE;
@@ -286,18 +293,25 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
286293

287294
// Exit the loop if the next event, if any, is sufficiently distant.
288295
const uint32_t isrTimeoutCcy = isrStartCcy + ISRTIMEOUTCCYS;
289-
uint32_t nextTimerCcy;
290-
bool busy = waveform.enabled;
291-
if (!busy) {
292-
nextTimerCcy = now + MAXIRQCCYS;
296+
bool busy;
297+
if (!waveform.enabled) {
298+
busy = false;
299+
waveform.nextEventCcy = ESP.getCycleCount() + MAXIRQCCYS;
293300
}
294-
else if (!(waveform.enabled & (1UL << waveform.nextPin))) {
295-
waveform.nextPin = waveform.startPin;
301+
else {
302+
busy = static_cast<int32_t>(isrTimeoutCcy - waveform.nextEventCcy) > 0;
303+
if (!(waveform.enabled & (1UL << waveform.nextPin))) {
304+
waveform.nextPin = waveform.startPin;
305+
}
296306
}
297307
while (busy) {
298-
nextTimerCcy = now + MAXIRQCCYS;
299308
int stopPin = waveform.nextPin;
300309
int pin = waveform.nextPin;
310+
uint32_t now;
311+
do {
312+
now = ESP.getCycleCount();
313+
} while (static_cast<int32_t>(waveform.nextEventCcy - now) > 0);
314+
waveform.nextEventCcy = now + MAXIRQCCYS;
301315
do {
302316
// If it's not on, ignore
303317
if (!(waveform.enabled & (1UL << pin)))
@@ -394,33 +408,26 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
394408
}
395409
}
396410

397-
if (static_cast<int32_t>(nextTimerCcy - wave.nextEventCcy) > 0) {
398-
nextTimerCcy = wave.nextEventCcy;
411+
if (static_cast<int32_t>(waveform.nextEventCcy - wave.nextEventCcy) > 0) {
412+
waveform.nextEventCcy = wave.nextEventCcy;
399413
waveform.nextPin = pin;
400414
}
401-
now = ESP.getCycleCount();
402415
} while ((pin = (pin < waveform.endPin) ? pin + 1 : waveform.startPin, pin != stopPin));
403416

404-
const int32_t timerMarginCcys = isrTimeoutCcy - nextTimerCcy;
405-
busy = timerMarginCcys > 0;
406-
if (busy) {
407-
while (static_cast<int32_t>(nextTimerCcy - now) > 0) {
408-
now = ESP.getCycleCount();
409-
}
410-
}
417+
busy = static_cast<int32_t>(isrTimeoutCcy - waveform.nextEventCcy) > 0;
411418
}
412419

413420
int32_t nextTimerCcys;
414421
if (waveform.timer1CB) {
415422
int32_t callbackCcys = microsecondsToClockCycles(waveform.timer1CB());
416423
// Account for unknown duration of timer1CB().
417-
nextTimerCcys = nextTimerCcy - ESP.getCycleCount();
424+
nextTimerCcys = waveform.nextEventCcy - ESP.getCycleCount();
418425
if (nextTimerCcys > callbackCcys) {
419426
nextTimerCcys = callbackCcys;
420427
}
421428
}
422429
else {
423-
nextTimerCcys = nextTimerCcy - now;
430+
nextTimerCcys = waveform.nextEventCcy - ESP.getCycleCount();
424431
}
425432

426433
// Firing timer too soon, the NMI occurs before ISR has returned.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.