Repository navigation
raspberrypi: reject 732 Hz in analogbufio.BufferedIn - #11910
Merged
Merged
Conversation
At 732 Hz the ADC divider is 48000000 / 732 - 1 = 65572.8, which does not fit in the 16-bit integer part of DIV. The value wraps and the ADC runs at its full 500 kHz instead. Start the valid range at 733 Hz.
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.
On raspberrypi, BufferedIn accepts sample rates from ADC_CLOCK_INPUT / ADC_MAX_CLOCK_DIV = 732 Hz up to 500 kHz. The ADC divider is calculated as 48000000 / sample_rate - 1. At 732 Hz, that gives 65572.8, which does not fit in the 16-bit integer part of the DIV register. adc_set_clkdiv() checks this with invalid_params_if, but that check is off in normal builds. The value then wraps to a divider below 96, so the ADC runs at its full 500 kHz. BufferedIn(pin, sample_rate=732) therefore samples about 680 times faster than requested, while 733 Hz works.
This raises the lower bound to ADC_CLOCK_INPUT / (ADC_MAX_CLOCK_DIV + 1) + 1, which is 733 Hz. I checked every allowed rate using a small C model of the divider calculation and the DIV register. Only 732 Hz came out wrong. Every rate from 733 Hz to 500 kHz is within 1 % of the requested rate. I haven't tried it on a board.