|
44 | 44 | // Maximum delay between IRQs
|
45 | 45 | #define MAXIRQUS (10000)
|
46 | 46 |
|
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 |
| - |
50 | 47 | // Set/clear GPIO 0-15 by bitmask
|
51 | 48 | #define SetGPIO(a) do { GPOS = a; } while (0)
|
52 | 49 | #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
|
121 | 118 | return false;
|
122 | 119 | }
|
123 | 120 | 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 |
130 | 129 | #endif
|
| 130 | + wave->nextTimeHighCycles = MicrosecondsToCycles(timeHighUS) - delta; |
| 131 | + wave->nextTimeLowCycles = MicrosecondsToCycles(timeLowUS) - delta; |
131 | 132 | wave->expiryCycle = runTimeUS ? GetCycleCount() + MicrosecondsToCycles(runTimeUS) : 0;
|
132 | 133 | if (runTimeUS && !wave->expiryCycle) {
|
133 | 134 | wave->expiryCycle = 1; // expiryCycle==0 means no timeout, so avoid setting it
|
@@ -192,14 +193,17 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
|
192 | 193 | return true;
|
193 | 194 | }
|
194 | 195 |
|
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) |
199 | 199 | #else
|
200 |
| - int cnt = 10; |
| 200 | + #define MINCYCLE MicrosecondsToCycles(4) |
| 201 | + #define DELTAIRQ (MicrosecondsToCycles(2) + MicrosecondsToCycles(3)/4) |
201 | 202 | #endif
|
202 | 203 |
|
| 204 | +static ICACHE_RAM_ATTR void timer1Interrupt() { |
| 205 | + uint32_t nextEventCycles; |
| 206 | + |
203 | 207 | // Handle enable/disable requests from main app.
|
204 | 208 | waveformEnabled = (waveformEnabled & ~waveformToDisable) | waveformToEnable; // Set the requested waveforms on/off
|
205 | 209 | waveformState &= ~waveformToEnable; // And clear the state of any just started
|
@@ -253,28 +257,21 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
|
253 | 257 | nextEventCycles = min_u32(nextEventCycles, deltaCycles);
|
254 | 258 | }
|
255 | 259 | }
|
256 |
| - } while (--cnt && (nextEventCycles < MicrosecondsToCycles(4))); |
| 260 | + } while (nextEventCycles < MINCYCLE); |
257 | 261 |
|
258 | 262 | if (timer1CB) {
|
259 | 263 | nextEventCycles = min_u32(nextEventCycles, timer1CB());
|
| 264 | + if (nextEventCycles < MINCYCLE) { |
| 265 | + nextEventCycles = MINCYCLE; |
| 266 | + } |
260 | 267 | }
|
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; |
276 | 269 |
|
277 | 270 | // Do it here instead of global function to save time and because we know it's edge-IRQ
|
278 | 271 | TEIE |= TEIE1; //edge int enable
|
| 272 | +#if F_CPU == 160000000 |
| 273 | + T1L = nextEventCycles >> 1; // Already know we're in range by MAXIRQUS |
| 274 | +#else |
279 | 275 | T1L = nextEventCycles; // Already know we're in range by MAXIRQUS
|
| 276 | +#endif |
280 | 277 | }
|
0 commit comments