From dc21efe446e5ce2f916a0a8e04ba6d52d1f39c22 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 27 Aug 2024 11:01:46 -0500 Subject: [PATCH] espressif: Validate sample rate The reported code used an invalid sample rate. This improves the error message so that it is not a generic IDFError. The new error message says: ``` ValueError: sample_rate must be 20000-2000000 ``` I verified at a sample rate of 20000 that I could read both 0 and 65535 using a jumper wire to another GPIO pin configured as a digital output. Closes #9504 --- ports/espressif/common-hal/analogbufio/BufferedIn.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/analogbufio/BufferedIn.c b/ports/espressif/common-hal/analogbufio/BufferedIn.c index f22f89d7f8e7..44cf9b7a4e77 100644 --- a/ports/espressif/common-hal/analogbufio/BufferedIn.c +++ b/ports/espressif/common-hal/analogbufio/BufferedIn.c @@ -71,6 +71,8 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s } #endif + mp_arg_validate_int_range(sample_rate, SOC_ADC_SAMPLE_FREQ_THRES_LOW, SOC_ADC_SAMPLE_FREQ_THRES_HIGH, MP_QSTR_sample_rate); + common_hal_mcu_pin_claim(pin); } @@ -109,7 +111,7 @@ static void start_dma(analogbufio_bufferedin_obj_t *self, adc_digi_convert_mode_ }; #if defined(DEBUG_ANALOGBUFIO) - mp_printf(&mp_plat_print, "pin:%d, ADC channel:%d, ADC index:%d, adc1_chan_mask:0x%x, adc2_chan_mask:0x%x\n", pin->number, pin->adc_channel, pin->adc_index, adc1_chan_mask, adc2_chan_mask); + mp_printf(&mp_plat_print, "pin:%d, ADC channel:%d, ADC index:%d\n", pin->number, pin->adc_channel, pin->adc_index); #endif // DEBUG_ANALOGBUFIO esp_err_t err = adc_continuous_new_handle(&adc_dma_config, &self->handle); if (ESP_OK != err) {