Repository navigation
raspberrypi: pick the closer pacing timer fraction in audiopwmio - #11925
Merged
Merged
Conversation
limit_denominator() returned the farther of its two bounds, the opposite of the Python code it is based on. For rates between half and all of system_clock / 65535 that bound has a zero numerator, which stops the DMA pacing timer. Also return the reduced fraction when its denominator equals max_denominator instead of running the loop into a division by zero.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
limit_denominator() in the RP2 audiopwmio code follows Python's Fraction.limit_denominator(), but its final comparison is reversed. Python returns bound2 when it is at least as close as bound1. This code returns bound2 when bound1 is closer, so it always picks the worse of the two candidates for the DMA pacing timer.
Usually the difference is only a few ppm. At 125 MHz, 11025 Hz comes out 11.6 ppm slow instead of 6.0 ppm fast, and 44100 Hz is off by 1.2 ppm instead of 0.2 ppm. The bigger problem is a sample rate between half and all of system_clock / 65535, about 954 to 1907 Hz at 125 MHz and 1145 to 2288 Hz at 150 MHz. In that range, bound2 is 0/1 and the pacing timer gets X = 0. According to the datasheet, that never asserts TREQ, so playback would not advance at all. I haven't seen this on a board. This is based on the register value.
This change swaps the comparison and prevents the function from returning a zero numerator. Rates below system_clock / 65535 therefore get the slowest possible rate, as they mostly did before. It also returns early when the reduced denominator is equal to max_denominator, matching Python. With <, the loop would keep going until d is 0 and then divide by zero. That can't happen with the usual clocks, but the check was off by one.
I compiled the function from this file on Linux and compared it with Python's limit_denominator(0xffff). I tested every sample rate from 1000 to 50000 Hz and every 7 Hz up to 192000 Hz at 48, 125, 133, 150, 200 and 250 MHz. The old code was worse than Python in 398244 of the 407622 cases that fit in the range, by up to 30 ppm, and returned a zero numerator in 6500 cases. The new code returns no zero numerators and differs from Python in 5662 cases by at most 0.2 ppm because of the float comparison.