-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Keep PWM phases constant #7057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep PWM phases constant #7057
Changes from 1 commit
7f2c4e9
64c6f04
7f821ce
d7852d2
5db5df6
4f4b504
c70e835
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,23 +256,24 @@ static ICACHE_RAM_ATTR void timer1Interrupt() { | |
// Check for toggles | ||
int32_t cyclesToGo = wave->nextServiceCycle - now; | ||
if (cyclesToGo < 0) { | ||
cyclesToGo = -((-cyclesToGo) % (wave->nextTimeHighCycles + wave->nextTimeLowCycles)); | ||
waveformState ^= mask; | ||
if (waveformState & mask) { | ||
if (i == 16) { | ||
GP16O |= 1; // GPIO16 write slow as it's RMW | ||
} else { | ||
SetGPIO(mask); | ||
} | ||
wave->nextServiceCycle = now + wave->nextTimeHighCycles; | ||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeHighCycles); | ||
wave->nextServiceCycle = now + wave->nextTimeHighCycles + cyclesToGo; | ||
nextEventCycles = min_u32(nextEventCycles, min_u32(wave->nextTimeHighCycles + cyclesToGo, 1)); | ||
|
||
} else { | ||
if (i == 16) { | ||
GP16O &= ~1; // GPIO16 write slow as it's RMW | ||
} else { | ||
ClearGPIO(mask); | ||
} | ||
wave->nextServiceCycle = now + wave->nextTimeLowCycles; | ||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeLowCycles); | ||
wave->nextServiceCycle = now + wave->nextTimeLowCycles + cyclesToGo; | ||
nextEventCycles = min_u32(nextEventCycles, min_u32(wave->nextTimeLowCycles + cyclesToGo, 1)); | ||
} | ||
} else { | ||
uint32_t deltaCycles = wave->nextServiceCycle - now; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logically, I get what you're trying to here. But it seems like this is a no-op unless we have overshot by an entire waveform cycle, no? If it's been less than a waveform cycle then the mod will be a no-op. The mod operator is a relatively expensive one, might consider just ignoring the case for code speed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, this should be a no-op most of the time. It's a safeguard because if we overshoot one or multiple wafeforms, the PWM will oscillate at 500KHz (1ms up + 1ms down) until the required number of cycles is reached. I don't know if it's even possible, or if 500KHz for a very short amount of time is an issue at all.
We could also add a
while (-cyclesToGo > wave->nextTimeHighCycles + wave->nextTimeLowCycles) { cyclesToGo += wave->nextTimeHighCycles + wave->nextTimeLowCycles)};
but that would also add code size to the precious IRAM.I'm pretty sure this would never occur with PWM, only if someone sets a very short waveform. You can probably drop this for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you comment it out and make a note in the code about why we're skipping, so if something pops up later we can see the reasoning behind the choice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this line of code for nom but leaved the two variants in comments.