@@ -161,8 +161,8 @@ int startWaveformClockCycles(uint8_t pin, uint32_t highCcys, uint32_t lowCcys,
161
161
// sanity checks, including mixed signed/unsigned arithmetic safety
162
162
if ((pin > 16 ) || isFlashInterfacePin (pin) || (alignPhase > 16 ) ||
163
163
static_cast <int32_t >(periodCcys) <= 0 ||
164
- (periodCcys >> 6 ) == 0 ||
165
- highCcys > periodCcys ) {
164
+ static_cast < int32_t >(highCcys) < 0 || static_cast < int32_t >(lowCcys) < 0 ||
165
+ (periodCcys >> 6 ) == 0 ) {
166
166
return false ;
167
167
}
168
168
Waveform& wave = waveform.pins [pin];
@@ -313,46 +313,41 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
313
313
now = ESP.getCycleCount ();
314
314
} while (static_cast <int32_t >(waveform.nextEventCcy - now) > 0 );
315
315
waveform.nextEventCcy = now + MAXIRQTICKSCCYS;
316
+
316
317
do {
317
318
// If it's not on, ignore
318
319
if (!(waveform.enabled & (1UL << pin)))
319
320
continue ;
320
321
321
322
Waveform& wave = waveform.pins [pin];
322
323
323
- if (static_cast <int32_t >(now - wave.nextEventCcy ) >= 0 ) {
324
+ const uint32_t overshootCcys = now - wave.nextEventCcy ;
325
+ if (static_cast <int32_t >(overshootCcys) >= 0 ) {
324
326
if (WaveformMode::EXPIRES == wave.mode && wave.nextEventCcy == wave.expiryCcy ) {
325
327
// Disable any waveforms that are done
326
328
waveform.enabled ^= 1UL << pin;
327
329
}
328
330
else {
329
- const uint32_t idleCcys = wave.periodCcys - wave.dutyCcys ;
330
- // get true accumulated overshoot, guaranteed >= 0 in this spot
331
- const uint32_t overshootCcys = now - ((waveform.states & (1UL << pin)) ? wave.endDutyCcy : wave.nextPeriodCcy );
332
- const uint32_t fwdPeriods = static_cast <uint32_t >(overshootCcys) >= idleCcys ?
333
- ((overshootCcys + wave.dutyCcys ) / wave.periodCcys ) : 0 ;
331
+ const uint32_t fwdPeriods = (overshootCcys + wave.dutyCcys ) / wave.periodCcys ;
334
332
uint32_t nextEdgeCcy;
335
333
if (waveform.states & (1UL << pin)) {
336
- // up to and including this period 100% duty
337
- const bool endOfPeriod = wave.nextPeriodCcy == wave.endDutyCcy ;
338
- // active configuration and forward 100% duty
339
- if (!idleCcys) {
340
- wave.nextPeriodCcy += fwdPeriods * wave.periodCcys ;
341
- wave.endDutyCcy = wave.nextPeriodCcy ;
342
- nextEdgeCcy = wave.nextPeriodCcy ;
334
+ // active configuration and forward are 100% duty
335
+ if (wave.periodCcys == wave.dutyCcys ) {
336
+ wave.nextPeriodCcy += wave.periodCcys ;
337
+ nextEdgeCcy = wave.endDutyCcy = wave.nextPeriodCcy ;
343
338
}
344
- else if (endOfPeriod ) {
345
- // preceeding period had zero idle cycle, continue direct into new duty cycle
346
- if (fwdPeriods >= 2 ) {
339
+ else if (wave. nextPeriodCcy == wave. endDutyCcy ) {
340
+ // preceeding period had zero idle cycle, continue directly into new duty cycle
341
+ if (fwdPeriods > 1 ) {
347
342
const uint32_t fwdPeriodCcys = (fwdPeriods - 1 ) * wave.periodCcys ;
348
343
wave.nextPeriodCcy += fwdPeriodCcys;
349
344
// adapt expiry such that it occurs during intended cycle
350
- if (WaveformMode::EXPIRES == wave.mode )
345
+ if (WaveformMode::EXPIRES == wave.mode ) {
351
346
wave.expiryCcy += fwdPeriodCcys;
347
+ }
352
348
}
353
- wave.endDutyCcy = wave.nextPeriodCcy + wave.dutyCcys ;
349
+ nextEdgeCcy = wave.endDutyCcy = wave.nextPeriodCcy + wave.dutyCcys ;
354
350
wave.nextPeriodCcy += wave.periodCcys ;
355
- nextEdgeCcy = wave.endDutyCcy ;
356
351
}
357
352
else if (wave.autoPwm &&
358
353
overshootCcys >= (wave.periodCcys >> 6 ) &&
@@ -367,9 +362,9 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
367
362
nextEdgeCcy = wave.endDutyCcy + adjPeriods * wave.dutyCcys ;
368
363
}
369
364
E0CD
else {
370
- waveform.states ^= 1UL << pin;
371
365
nextEdgeCcy = wave.nextPeriodCcy ;
372
- if (pin == 16 ) {
366
+ waveform.states &= ~(1UL << pin);
367
+ if (16 == pin) {
373
368
GP16O = 0 ;
374
369
}
375
370
else {
@@ -379,11 +374,10 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
379
374
}
380
375
else {
381
376
if (!wave.dutyCcys ) {
382
- wave.nextPeriodCcy += (fwdPeriods + 1 ) * wave.periodCcys ;
377
+ wave.nextPeriodCcy += wave.periodCcys ;
383
378
wave.endDutyCcy = wave.nextPeriodCcy ;
384
379
}
385
380
else {
386
- waveform.states ^= 1UL << pin;
387
381
wave.nextPeriodCcy += wave.periodCcys ;
388
382
wave.endDutyCcy = now + wave.dutyCcys ;
389
383
if (fwdPeriods) {
@@ -397,7 +391,8 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
397
391
if (WaveformMode::EXPIRES == wave.mode )
398
392
wave.expiryCcy += fwdPeriodCcys;
399
393
}
400
- if (pin == 16 ) {
394
+ waveform.states |= 1UL << pin;
395
+ if (16 == pin) {
401
396
GP16O = 1 ;
402
397
}
403
398
else {
0 commit comments