8000 Code simplifications. · dok-net/arduino-esp8266@cc10ceb · GitHub
[go: up one dir, main page]

Skip to content

Commit cc10ceb

Browse files
committed
Code simplifications.
1 parent 60336ba commit cc10ceb

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

cores/esp8266/core_esp8266_waveform.cpp

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ int startWaveformClockCycles(uint8_t pin, uint32_t highCcys, uint32_t lowCcys,
161161
// sanity checks, including mixed signed/unsigned arithmetic safety
162162
if ((pin > 16) || isFlashInterfacePin(pin) || (alignPhase > 16) ||
163163
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) {
166166
return false;
167167
}
168168
Waveform& wave = waveform.pins[pin];
@@ -313,46 +313,41 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
313313
now = ESP.getCycleCount();
314314
} while (static_cast<int32_t>(waveform.nextEventCcy - now) > 0);
315315
waveform.nextEventCcy = now + MAXIRQTICKSCCYS;
316+
316317
do {
317318
// If it's not on, ignore
318319
if (!(waveform.enabled & (1UL << pin)))
319320
continue;
320321

321322
Waveform& wave = waveform.pins[pin];
322323

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) {
324326
if (WaveformMode::EXPIRES == wave.mode && wave.nextEventCcy == wave.expiryCcy) {
325327
// Disable any waveforms that are done
326328
waveform.enabled ^= 1UL << pin;
327329
}
328330
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;
334332
uint32_t nextEdgeCcy;
335333
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;
343338
}
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) {
347342
const uint32_t fwdPeriodCcys = (fwdPeriods - 1) * wave.periodCcys;
348343
wave.nextPeriodCcy += fwdPeriodCcys;
349344
// adapt expiry such that it occurs during intended cycle
350-
if (WaveformMode::EXPIRES == wave.mode)
345+
if (WaveformMode::EXPIRES == wave.mode) {
351346
wave.expiryCcy += fwdPeriodCcys;
347+
}
352348
}
353-
wave.endDutyCcy = wave.nextPeriodCcy + wave.dutyCcys;
349+
nextEdgeCcy = wave.endDutyCcy = wave.nextPeriodCcy + wave.dutyCcys;
354350
wave.nextPeriodCcy += wave.periodCcys;
355-
nextEdgeCcy = wave.endDutyCcy;
356351
}
357352
else if (wave.autoPwm &&
358353
overshootCcys >= (wave.periodCcys >> 6) &&
@@ -367,9 +362,9 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
367362
nextEdgeCcy = wave.endDutyCcy + adjPeriods * wave.dutyCcys;
368363
}
369364 E0CD
else {
370-
waveform.states ^= 1UL << pin;
371365
nextEdgeCcy = wave.nextPeriodCcy;
372-
if (pin == 16) {
366+
waveform.states &= ~(1UL << pin);
367+
if (16 == pin) {
373368
GP16O = 0;
374369
}
375370
else {
@@ -379,11 +374,10 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
379374
}
380375
else {
381376
if (!wave.dutyCcys) {
382-
wave.nextPeriodCcy += (fwdPeriods + 1) * wave.periodCcys;
377+
wave.nextPeriodCcy += wave.periodCcys;
383378
wave.endDutyCcy = wave.nextPeriodCcy;
384379
}
385380
else {
386-
waveform.states ^= 1UL << pin;
387381
wave.nextPeriodCcy += wave.periodCcys;
388382
wave.endDutyCcy = now + wave.dutyCcys;
389383
if (fwdPeriods) {
@@ -397,7 +391,8 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
397391
if (WaveformMode::EXPIRES == wave.mode)
398392
wave.expiryCcy += fwdPeriodCcys;
399393
}
400-
if (pin == 16) {
394+
waveform.states |= 1UL << pin;
395+
if (16 == pin) {
401396
GP16O = 1;
402397
}
403398
else {

0 commit comments

Comments
 (0)
0