From 405485d479231ebcabd614015c23e6f1451a54ed Mon Sep 17 00:00:00 2001 From: EminY <57416756+emincan2000@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:08:47 +0000 Subject: [PATCH] raspberrypi: reject 732 Hz in analogbufio.BufferedIn 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. --- ports/raspberrypi/common-hal/analogbufio/BufferedIn.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c index fe74f321392..255ef90f784 100644 --- a/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c +++ b/ports/raspberrypi/common-hal/analogbufio/BufferedIn.c @@ -32,8 +32,9 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s raise_ValueError_invalid_pin(); } - // Validate sample rate here - sample_rate = (uint32_t)mp_arg_validate_int_range(sample_rate, ADC_CLOCK_INPUT / ADC_MAX_CLOCK_DIV, ADC_CLOCK_INPUT / 96, MP_QSTR_sample_rate); + // Validate sample rate here. The divider is ADC_CLOCK_INPUT / sample_rate - 1 and must + // stay below ADC_MAX_CLOCK_DIV, so the lowest rate is 733 Hz, not 732. + sample_rate = (uint32_t)mp_arg_validate_int_range(sample_rate, ADC_CLOCK_INPUT / (ADC_MAX_CLOCK_DIV + 1) + 1, ADC_CLOCK_INPUT / 96, MP_QSTR_sample_rate); // Set pin and channel self->pin = pin;