Skip to content

Commit 12a4c65

Browse files
committed
Handle time-of-flight in the right spot.
1 parent bc7d279 commit 12a4c65

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

cores/esp8266/core_esp8266_waveform.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ constexpr int32_t MAXIRQTICKSCCYS = microsecondsToClockCycles(10000);
5151
// Maximum servicing time for any single IRQ
5252
constexpr uint32_t ISRTIMEOUTCCYS = microsecondsToClockCycles(18);
5353
// The latency between in-ISR rearming of the timer and the earliest firing
54-
constexpr int32_t IRQLATENCYCCYS = ISCPUFREQ160MHZ ?
55-
microsecondsToClockCycles(2) >> 1 : microsecondsToClockCycles(2);
54+
constexpr int32_t IRQLATENCYCCYS = microsecondsToClockCycles(2);
5655
// The SDK and hardware take some time to actually get to our NMI code
5756
constexpr int32_t DELTAIRQCCYS = ISCPUFREQ160MHZ ?
5857
microsecondsToClockCycles(2) >> 1 : microsecondsToClockCycles(2);
@@ -256,7 +255,7 @@ static inline ICACHE_RAM_ATTR int32_t scaleCcys(const int32_t ccys, const bool i
256255

257256
static ICACHE_RAM_ATTR void timer1Interrupt() {
258257
const uint32_t isrStartCcy = ESP.getCycleCount();
259-
int32_t clockDrift = isrStartCcy - waveform.nextEventCcy - DELTAIRQCCYS;
258+
int32_t clockDrift = isrStartCcy - waveform.nextEventCcy;
260259
const bool isCPU2X = CPU2X & 1;
261260
if ((waveform.toSetBits && !(waveform.enabled & waveform.toSetBits)) || waveform.toDisableBits) {
262261
// Handle enable/disable requests from main app.
@@ -415,14 +414,21 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
415414
}
416415

417416
// Timer is 80MHz fixed. 160MHz CPU frequency need scaling.
417+
int32_t deltaIrqCcys = DELTAIRQCCYS;
418+
int32_t irqLatencyCcys = IRQLATENCYCCYS;
418419
if (isCPU2X) {
419420
nextEventCcys >>= 1;
421+
deltaIrqCcys >>= 1;
422+
irqLatencyCcys >>= 1;
420423
}
421424

422425
// Firing timer too soon, the NMI occurs before ISR has returned.
423-
if (nextEventCcys < IRQLATENCYCCYS) {
424-
waveform.nextEventCcy = now + IRQLATENCYCCYS;
425-
nextEventCcys = IRQLATENCYCCYS;
426+
if (nextEventCcys < irqLatencyCcys + deltaIrqCcys) {
427+
waveform.nextEventCcy = now + IRQLATENCYCCYS + DELTAIRQCCYS;
428+
nextEventCcys = irqLatencyCcys;
429+
}
430+
else {
431+
nextEventCcys -= deltaIrqCcys;
426432
}
427433

428434
// Register access is fast and edge IRQ was configured before.

0 commit comments

Comments
 (0)