From 35588d46bdd2e6752b8dbdc6fb5b0d7bea36ccd0 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 2 May 2022 16:23:49 +0200 Subject: [PATCH] cpu/sam0_common: adc: Automatically configure extref pin --- cpu/sam0_common/include/periph_cpu_common.h | 17 +++++++++++++++++ cpu/sam0_common/periph/adc.c | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cpu/sam0_common/include/periph_cpu_common.h b/cpu/sam0_common/include/periph_cpu_common.h index 1fbee3702d..2ac09a5053 100644 --- a/cpu/sam0_common/include/periph_cpu_common.h +++ b/cpu/sam0_common/include/periph_cpu_common.h @@ -779,6 +779,23 @@ typedef struct { #endif } adc_conf_chan_t; +/** + * @brief Pin that can be used for external voltage reference A + */ +#define ADC_REFSEL_AREFA_PIN GPIO_PIN(PA, 3) + +/** + * @brief Pin that can be used for external voltage reference B + */ +#define ADC_REFSEL_AREFB_PIN GPIO_PIN(PA, 4) + +#if defined(ADC_REFCTRL_REFSEL_AREFC) || DOXYGEN +/** + * @brief Pin that can be used for external voltage reference C + */ +#define ADC_REFSEL_AREFC_PIN GPIO_PIN(PA, 6) +#endif + /** * @name Ethernet peripheral parameters * @{ diff --git a/cpu/sam0_common/periph/adc.c b/cpu/sam0_common/periph/adc.c index 8de70b0fa5..cf6871d3e0 100644 --- a/cpu/sam0_common/periph/adc.c +++ b/cpu/sam0_common/periph/adc.c @@ -223,6 +223,21 @@ static int _adc_configure(Adc *dev, adc_res_t res) SUPC->VREF.reg |= SUPC_VREF_VREFOE; } #endif +#ifdef ADC_REFCTRL_REFSEL_AREFA + if (ADC_REF_DEFAULT == ADC_REFCTRL_REFSEL_AREFA) { + gpio_init_mux(ADC_REFSEL_AREFA_PIN, GPIO_MUX_B); + } +#endif +#ifdef ADC_REFCTRL_REFSEL_AREFB + if (ADC_REF_DEFAULT == ADC_REFCTRL_REFSEL_AREFB) { + gpio_init_mux(ADC_REFSEL_AREFB_PIN, GPIO_MUX_B); + } +#endif +#ifdef ADC_REFCTRL_REFSEL_AREFC + if (ADC_REF_DEFAULT == ADC_REFCTRL_REFSEL_AREFC) { + gpio_init_mux(ADC_REFSEL_AREFC_PIN, GPIO_MUX_B); + } +#endif /* Enable ADC Module */ dev->CTRLA.reg |= ADC_CTRLA_ENABLE;