Skip to content

raspberrypi: pick the closer pacing timer fraction in audiopwmio - #11925

Merged
dhalbert merged 1 commit into
adafruit:mainfrom
emincan2000:rp2-pacing-limit-denominator
Nov 19, 2026
Merged

dhalbert merged 1 commit into
adafruit:mainfrom
emincan2000:rp2-pacing-limit-denominator

Conversation

@emincan2000

Copy link
Copy Markdown

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.

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.

@dhalbert dhalbert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@dhalbert
dhalbert merged commit b18f559 into adafruit:main Nov 19, 2026
171 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants