Repository navigation
raspberrypi: round the rp2pio clock divider up when the division is not exact - #11933
Merged
Merged
Conversation
…ot exact The check used frequency256 % div256 instead of the remainder of the division that produced div256, so some frequencies kept the rounded down divider and ran faster than requested.
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.
rp2pio computes the PIO clock divider as div256 = frequency256 / frequency, then rounds it up to keep the actual frequency at or below the requested one, as the frequency docs promise. The rounding check uses frequency256 % div256 > 0. It should check the remainder from the division that produced div256, frequency256 % frequency. If div256 divides frequency256 evenly, the divider stays rounded down even when the original division wasn't exact, so the state machine runs slightly fast. At 125 MHz, requesting 399999 Hz gives 400000 Hz, and 312499 Hz gives 312500 Hz.
This changes the check to frequency256 % frequency in both rp2pio_statemachine_construct() and common_hal_rp2pio_statemachine_set_frequency().
I compiled both versions of the calculation on Linux and tested every frequency from 1908 Hz up to the system clock at 125, 150 and 200 MHz. The old check produced a frequency above the requested one for 1614450, 3451310 and 2583194 of them, respectively. The new check did so for none. The difference was small, a few Hz at most in the cases I looked at. I haven't run it on a board.