Skip to content

Commit 409fc91

Browse files
Add'l testing and tweaking at 160 and 80mhz
1 parent 923f2f4 commit 409fc91

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

cores/esp8266/core_esp8266_waveform.c

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@
4444
// Maximum delay between IRQs
4545
#define MAXIRQUS (10000)
4646

47-
// If the cycles from now to an event are below this value, perform it anyway since IRQs take longer than this
48-
#define CYCLES_FLUFF (100)
49-
5047
// Set/clear GPIO 0-15 by bitmask
5148
#define SetGPIO(a) do { GPOS = a; } while (0)
5249
#define ClearGPIO(a) do { GPOC = a; } while (0)
@@ -121,13 +118,17 @@ int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS, uint32_t
121118
return false;
122119
}
123120
Waveform *wave = &waveform[pin];
124-
#if F_CPU == 160000000
125-
wave->nextTimeHighCycles = MicrosecondsToCycles(timeHighUS) - 140; // Take out some time for IRQ codepath
126-
wave->nextTimeLowCycles = MicrosecondsToCycles(timeLowUS) - 140; // Take out some time for IRQ codepath
127-
#else
128-
wave->nextTimeHighCycles = MicrosecondsToCycles(timeHighUS) > 280 ? MicrosecondsToCycles(timeHighUS) - 280 : MicrosecondsToCycles(timeHighUS); // Take out some time for IRQ codepath
129-
wave->nextTimeLowCycles = MicrosecondsToCycles(timeLowUS) > 280 ? MicrosecondsToCycles(timeLowUS)- 280 : MicrosecondsToCycles(timeLowUS); // Take out some time for IRQ codepath
121+
// Adjust to shave off some of the IRQ time, approximately
122+
uint32_t delta = 0;
123+
#if F_CPU == 80000000
124+
if (timeHighUS > 2) {
125+
delta = MicrosecondsToCycles(1) + MicrosecondsToCycles(2)/3;
126+
}
127+
#else // 160000000
128+
delta = MicrosecondsToCycles(1) >> 1; // 0.5 us off each edge
130129
#endif
130+
wave->nextTimeHighCycles = MicrosecondsToCycles(timeHighUS) - delta;
131+
wave->nextTimeLowCycles = MicrosecondsToCycles(timeLowUS) - delta;
131132
wave->expiryCycle = runTimeUS ? GetCycleCount() + MicrosecondsToCycles(runTimeUS) : 0;
132133
if (runTimeUS && !wave->expiryCycle) {
133134
wave->expiryCycle = 1; // expiryCycle==0 means no timeout, so avoid setting it
@@ -192,14 +193,17 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
192193
return true;
193194
}
194195

195-
static ICACHE_RAM_ATTR void timer1Interrupt() {
196-
uint32_t nextEventCycles;
197-
#if F_CPU == 160000000
198-
int cnt = 20;
196+
#if F_CPU == 80000000
197+
#define MINCYCLE MicrosecondsToCycles(6)
198+
#define DELTAIRQ MicrosecondsToCycles(5)
199199
#else
200-
int cnt = 10;
200+
#define MINCYCLE MicrosecondsToCycles(4)
201+
#define DELTAIRQ (MicrosecondsToCycles(2) + MicrosecondsToCycles(3)/4)
201202
#endif
202203

204+
static ICACHE_RAM_ATTR void timer1Interrupt() {
205+
uint32_t nextEventCycles;
206+
203207
// Handle enable/disable requests from main app.
204208
waveformEnabled = (waveformEnabled & ~waveformToDisable) | waveformToEnable; // Set the requested waveforms on/off
205209
waveformState &= ~waveformToEnable; // And clear the state of any just started
@@ -253,28 +257,21 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
253257
nextEventCycles = min_u32(nextEventCycles, deltaCycles);
254258
}
255259
}
256-
} while (--cnt && (nextEventCycles < MicrosecondsToCycles(4)));
260+
} while (nextEventCycles < MINCYCLE);
257261

258262
if (timer1CB) {
259263
nextEventCycles = min_u32(nextEventCycles, timer1CB());
264+
if (nextEventCycles < MINCYCLE) {
265+
nextEventCycles = MINCYCLE;
266+
}
260267
}
261-
262-
#if F_CPU == 160000000
263-
if (nextEventCycles < MicrosecondsToCycles(5)) {
264-
nextEventCycles = MicrosecondsToCycles(1);
265-
} else {
266-
nextEventCycles -= MicrosecondsToCycles(4) + MicrosecondsToCycles(1)/2;
267-
}
268-
nextEventCycles = nextEventCycles >> 1;
269-
#else
270-
if (nextEventCycles < MicrosecondsToCycles(8)) {
271-
nextEventCycles = MicrosecondsToCycles(2);
272-
} else {
273-
nextEventCycles -= MicrosecondsToCycles(6);
274-
}
275-
#endif
268+
nextEventCycles -= DELTAIRQ;
276269

277270
// Do it here instead of global function to save time and because we know it's edge-IRQ
278271
TEIE |= TEIE1; //edge int enable
272+
#if F_CPU == 160000000
273+
T1L = nextEventCycles >> 1; // Already know we're in range by MAXIRQUS
274+
#else
279275
T1L = nextEventCycles; // Already know we're in range by MAXIRQUS
276+
#endif
280277
}

0 commit comments

Comments
 (0)