-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What this and the other branch seem to be doing is to be preserving the waveform phase at expense of period jitter. Could it be that the pulsing you're seeing on some LED boards is due to periods of some pins slightly increasing (when the total on-time is so small that a few 10s of 80mhz instructions would actually increase the %on-time appreciably)? And this, by introducing a feedback on the period makes things average out by continually adding jitter to the last-checked pin so that over large amounts of time the I think it's a fair thing to do, given that when cyclesToGo < 0 then bad stuff has already happened, actually. Feels like a good idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, if Missing one cycle is not visible, whereas shifting the phase creates a visible artefact. Also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to interfere, but from my work on the concurring PR #7022 I believe recomputing the nextServiceCycle based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @s-hadinger I'm sorry, though mitigated by fix and feature, the IRAM size is actually larger in my PR, from master
to PR #7022
A prominent fix is that 100% duty or off cycle now work without pulsating on the output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @s-hadinger I think this PR should not be merged, on the grounds that
is near 100% of the time 1, but otherwise always 0.
which more or less revokes the effect of the change in this PR a few CPU cycles later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Err, how would a 100% duty cycle actually get into this section of code, though? The Tone is by definition a 50% cycle and analogWrite checks for 100% duty cycle and just does
That would be a breaking change. The original code special-cased it and there are probably HW installations out there depending on this behavior. A 3.0 tweak, not a 2.x one.
The API specifies a frequency and has no guarantees about phase relationships. You can't know what phases there are, in general, since the app code is running async to the timer...
As @dok-net says, this does always equal 0 or 1. I think you meant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I know is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @s-hadinger a much reworked version is in #7022 now. Changing duty cycle maintains phase if period duration remains the same. Runtime is honored precisely, not only on next incidental timer event. Overall stability, correctness and performance fixes, "works for me" (buzzer, servo, PWM PC fan control). I think that's good for a start. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
First, in the case of direct use of waveform :-) Second, loss of phase for
Hem. |
||
} 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.