Skip to content

Commit

Permalink
Merge pull request #8094 from s-hadinger/pwm_flicker2
Browse files Browse the repository at this point in the history
Fix PWM flickering during wifi connection (#8046)
  • Loading branch information
arendst authored Apr 8, 2020
2 parents 8b7aca3 + 11746d9 commit 8d97bcd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions tasmota/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Add command ``SetOption91 1`` to enable fading at startup / power on
- Add command ``SetOption41 <x>`` to force sending gratuitous ARP every <x> seconds
- Add quick wifi reconnect using saved AP parameters when ``SetOption56 0`` (#3189)
- Fix PWM flickering during wifi connection (#8046)

### 8.2.0.2 20200328

Expand Down
24 changes: 19 additions & 5 deletions tasmota/core_esp8266_waveform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,38 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
// cyclesToGo = -((-cyclesToGo) % (wave->nextTimeHighCycles + wave->nextTimeLowCycles));
//
// Alternative version with lower CPU impact:
// while (-cyclesToGo > wave->nextTimeHighCycles + wave->nextTimeLowCycles) { cyclesToGo += wave->nextTimeHighCycles + wave->nextTimeLowCycles)};
// while (-cyclesToGo > wave->nextTimeHighCycles + wave->nextTimeLowCycles) { cyclesToGo += wave->nextTimeHighCycles + wave->nextTimeLowCycles); }
//
// detect interrupt storm, for example during wifi connection.
// if we overshoot the cycle by more than 25%, we forget phase and keep PWM duration
int32_t overshoot = (-cyclesToGo) > ((wave->nextTimeHighCycles + wave->nextTimeLowCycles) >> 2);
waveformState ^= mask;
if (waveformState & mask) {
if (i == 16) {
GP16O |= 1; // GPIO16 write slow as it's RMW
} else {
SetGPIO(mask);
}
wave->nextServiceCycle += wave->nextTimeHighCycles;
nextEventCycles = min_u32(nextEventCycles, max_32(wave->nextTimeHighCycles + cyclesToGo, microsecondsToClockCycles(1)));
if (overshoot) {
wave->nextServiceCycle = now + wave->nextTimeHighCycles;
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeHighCycles);
} else {
wave->nextServiceCycle += wave->nextTimeHighCycles;
nextEventCycles = min_u32(nextEventCycles, max_32(wave->nextTimeHighCycles + cyclesToGo, microsecondsToClockCycles(1)));
}
} else {
if (i == 16) {
GP16O &= ~1; // GPIO16 write slow as it's RMW
} else {
ClearGPIO(mask);
}
wave->nextServiceCycle += wave->nextTimeLowCycles;
nextEventCycles = min_u32(nextEventCycles, max_32(wave->nextTimeLowCycles + cyclesToGo, microsecondsToClockCycles(1)));
if (overshoot) {
wave->nextServiceCycle = now + wave->nextTimeLowCycles;
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeLowCycles);
} else {
wave->nextServiceCycle += wave->nextTimeLowCycles;
nextEventCycles = min_u32(nextEventCycles, max_32(wave->nextTimeLowCycles + cyclesToGo, microsecondsToClockCycles(1)));
}
}
} else {
uint32_t deltaCycles = wave->nextServiceCycle - now;
Expand Down

0 comments on commit 8d97bcd

Please sign in to comment.