From 115a9ba9508d5b30f88e06fbf42aeb5cc609c707 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 3 Dec 2022 17:51:34 +0100 Subject: [PATCH] samd/ADC: Apply the channel's VRef setting at each read. Signed-off-by: robert-hh --- ports/samd/machine_adc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ports/samd/machine_adc.c b/ports/samd/machine_adc.c index 854260927b..4b1c38797b 100644 --- a/ports/samd/machine_adc.c +++ b/ports/samd/machine_adc.c @@ -208,6 +208,7 @@ static mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_ // flag the device/channel as being in use. ch_busy_flags |= (1 << (self->adc_config.device * 16 + self->adc_config.channel)); + device_mgmt[self->adc_config.device].init = false; self->dma_channel = -1; self->tc_index = -1; @@ -225,6 +226,8 @@ static mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { mp_raise_OSError(MP_EBUSY); } + // Set the reference voltage. Default: external AREFA. + adc->REFCTRL.reg = adc_vref_table[self->vref]; // Set Input channel and resolution // Select the pin as positive input and gnd as negative input reference, non-diff mode by default adc->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND | self->adc_config.channel; @@ -261,6 +264,8 @@ static void machine_adc_read_timed(mp_obj_t self_in, mp_obj_t values, mp_obj_t f if (self->tc_index == -1) { self->tc_index = allocate_tc_instance(); } + // Set the reference voltage. Default: external AREFA. + adc->REFCTRL.reg = adc_vref_table[self->vref]; // Set Input channel and resolution // Select the pin as positive input and gnd as negative input reference, non-diff mode by default adc->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND | self->adc_config.channel; @@ -407,7 +412,7 @@ static void adc_init(machine_adc_obj_t *self) { // Divide a 48MHz clock by 32 to obtain 1.5 MHz clock to adc adc->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV32; // Select external AREFA as reference voltage. - adc->REFCTRL.reg = self->vref; + adc->REFCTRL.reg = adc_vref_table[self->vref]; // Average: Accumulate samples and scale them down accordingly adc->AVGCTRL.reg = self->avg | ADC_AVGCTRL_ADJRES(self->avg); // Enable ADC and wait to be ready