2017-05-10 21:23:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Dan Evans <photonthunder@gmail.com>
|
|
|
|
* Copyright (C) 2017 Travis Griggs <travisgriggs@gmail.com>
|
|
|
|
* Copyright (C) 2017 Dylan Laduranty <dylanladuranty@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
2017-06-22 15:43:17 +02:00
|
|
|
/**
|
|
|
|
* @ingroup cpu_sam0_common
|
|
|
|
* @ingroup drivers_periph_adc
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level ADC driver implementation
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
2017-05-10 21:23:00 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph/gpio.h"
|
|
|
|
#include "periph/adc.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
#include "mutex.h"
|
|
|
|
|
2017-05-15 16:37:55 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
2017-05-10 21:23:00 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
2020-08-07 19:37:12 +02:00
|
|
|
/* The SAMD5x/SAME5x family has two ADCs: ADC0 and ADC1.
|
|
|
|
* Introducing ADC_DEV as alias for the respective device (ADC/ADC0/ADC1). */
|
|
|
|
#ifndef ADC_DEV
|
|
|
|
#ifdef ADC0
|
|
|
|
#define ADC_DEV ADC0
|
|
|
|
#else
|
|
|
|
#define ADC_DEV ADC
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ADC_GCLK_SRC
|
|
|
|
#define ADC_GCLK_SRC SAM0_GCLK_MAIN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ADC_GAIN_FACTOR_DEFAULT
|
|
|
|
#define ADC_GAIN_FACTOR_DEFAULT (0)
|
|
|
|
#endif
|
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Prototypes */
|
|
|
|
static void _adc_poweroff(void);
|
2020-08-07 19:37:12 +02:00
|
|
|
static void _setup_clock(void);
|
|
|
|
static void _setup_calibration(void);
|
2017-05-10 21:23:00 +02:00
|
|
|
static int _adc_configure(adc_res_t res);
|
|
|
|
|
|
|
|
static mutex_t _lock = MUTEX_INIT;
|
|
|
|
|
|
|
|
static inline void _prep(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _done(void)
|
|
|
|
{
|
|
|
|
mutex_unlock(&_lock);
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:37:12 +02:00
|
|
|
static inline void _wait_syncbusy(void)
|
2017-05-10 21:23:00 +02:00
|
|
|
{
|
2020-08-07 19:37:12 +02:00
|
|
|
#ifdef ADC_STATUS_SYNCBUSY
|
|
|
|
while (ADC_DEV->STATUS.reg & ADC_STATUS_SYNCBUSY) {}
|
|
|
|
#else
|
|
|
|
while (ADC_DEV->SYNCBUSY.reg) {}
|
2017-05-10 21:23:00 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _adc_poweroff(void)
|
|
|
|
{
|
2020-08-07 19:37:12 +02:00
|
|
|
_wait_syncbusy();
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Disable */
|
2020-08-07 19:37:12 +02:00
|
|
|
ADC_DEV->CTRLA.reg &= ~ADC_CTRLA_ENABLE;
|
|
|
|
_wait_syncbusy();
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Disable bandgap */
|
2020-08-24 13:57:45 +02:00
|
|
|
#ifdef SYSCTRL_VREF_BGOUTEN
|
2019-10-01 21:42:42 +02:00
|
|
|
if (ADC_REF_DEFAULT == ADC_REFCTRL_REFSEL_INT1V) {
|
2017-05-10 21:23:00 +02:00
|
|
|
SYSCTRL->VREF.reg &= ~SYSCTRL_VREF_BGOUTEN;
|
|
|
|
}
|
2020-08-07 19:37:12 +02:00
|
|
|
#else
|
2019-10-01 21:42:42 +02:00
|
|
|
if (ADC_REF_DEFAULT == ADC_REFCTRL_REFSEL_INTREF) {
|
2017-05-10 21:23:00 +02:00
|
|
|
SUPC->VREF.reg &= ~SUPC_VREF_VREFOE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:37:12 +02:00
|
|
|
static void _setup_clock(void)
|
|
|
|
{
|
|
|
|
/* Enable gclk in case we are the only user */
|
|
|
|
sam0_gclk_enable(ADC_GCLK_SRC);
|
2020-08-24 13:57:45 +02:00
|
|
|
|
|
|
|
#ifdef PM_APBCMASK_ADC
|
2020-08-07 19:37:12 +02:00
|
|
|
/* Power On */
|
|
|
|
PM->APBCMASK.reg |= PM_APBCMASK_ADC;
|
|
|
|
/* GCLK Setup */
|
2020-08-24 13:57:45 +02:00
|
|
|
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN
|
|
|
|
| GCLK_CLKCTRL_GEN(ADC_GCLK_SRC)
|
|
|
|
| GCLK_CLKCTRL_ID(ADC_GCLK_ID);
|
2020-08-07 19:37:12 +02:00
|
|
|
/* Configure prescaler */
|
|
|
|
ADC_DEV->CTRLB.reg = ADC_PRESCALER;
|
|
|
|
#else
|
|
|
|
/* Power on */
|
|
|
|
#ifdef MCLK_APBCMASK_ADC
|
|
|
|
MCLK->APBCMASK.reg |= MCLK_APBCMASK_ADC;
|
|
|
|
#else
|
|
|
|
#ifdef MCLK_APBDMASK_ADC0
|
|
|
|
if (ADC_DEV == ADC0) {
|
|
|
|
MCLK->APBDMASK.reg |= MCLK_APBDMASK_ADC0;
|
|
|
|
} else {
|
|
|
|
MCLK->APBDMASK.reg |= MCLK_APBDMASK_ADC1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
MCLK->APBDMASK.reg |= MCLK_APBDMASK_ADC;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ADC0_GCLK_ID
|
|
|
|
/* GCLK Setup */
|
|
|
|
if (ADC_DEV == ADC0) {
|
|
|
|
GCLK->PCHCTRL[ADC0_GCLK_ID].reg = GCLK_PCHCTRL_CHEN
|
|
|
|
| GCLK_PCHCTRL_GEN(ADC_GCLK_SRC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GCLK->PCHCTRL[ADC1_GCLK_ID].reg = GCLK_PCHCTRL_CHEN
|
|
|
|
| GCLK_PCHCTRL_GEN(ADC_GCLK_SRC);
|
|
|
|
}
|
|
|
|
/* Configure prescaler */
|
|
|
|
ADC_DEV->CTRLA.reg = ADC_PRESCALER;
|
|
|
|
#else
|
|
|
|
/* GCLK Setup */
|
|
|
|
GCLK->PCHCTRL[ADC_GCLK_ID].reg = GCLK_PCHCTRL_CHEN
|
|
|
|
| GCLK_PCHCTRL_GEN(ADC_GCLK_SRC);
|
|
|
|
/* Configure prescaler */
|
|
|
|
ADC_DEV->CTRLB.reg = ADC_PRESCALER;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _setup_calibration(void)
|
|
|
|
{
|
2020-08-24 13:57:45 +02:00
|
|
|
#ifdef ADC_CALIB_BIAS_CAL
|
2020-08-07 19:37:12 +02:00
|
|
|
/* Load the fixed device calibration constants */
|
|
|
|
ADC_DEV->CALIB.reg =
|
|
|
|
ADC_CALIB_BIAS_CAL((*(uint32_t*)ADC_FUSES_BIASCAL_ADDR >>
|
|
|
|
ADC_FUSES_BIASCAL_Pos)) |
|
|
|
|
ADC_CALIB_LINEARITY_CAL((*(uint64_t*)ADC_FUSES_LINEARITY_0_ADDR >>
|
|
|
|
ADC_FUSES_LINEARITY_0_Pos));
|
|
|
|
#else
|
|
|
|
/* Set default calibration from NVM */
|
|
|
|
#ifdef ADC0_FUSES_BIASCOMP_ADDR
|
|
|
|
if (ADC_DEV == ADC0) {
|
|
|
|
ADC_DEV->CALIB.reg =
|
|
|
|
ADC0_FUSES_BIASCOMP((*(uint32_t*)ADC0_FUSES_BIASCOMP_ADDR)) >>
|
|
|
|
ADC_CALIB_BIASCOMP_Pos |
|
|
|
|
ADC0_FUSES_BIASREFBUF((*(uint32_t*)ADC0_FUSES_BIASREFBUF_ADDR) >>
|
|
|
|
ADC0_FUSES_BIASREFBUF_Pos);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ADC_DEV->CALIB.reg =
|
|
|
|
ADC1_FUSES_BIASCOMP((*(uint32_t*)ADC1_FUSES_BIASCOMP_ADDR)) >>
|
|
|
|
ADC_CALIB_BIASCOMP_Pos |
|
|
|
|
ADC1_FUSES_BIASREFBUF((*(uint32_t*)ADC1_FUSES_BIASREFBUF_ADDR) >>
|
|
|
|
ADC1_FUSES_BIASREFBUF_Pos);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ADC_DEV->CALIB.reg =
|
|
|
|
ADC_FUSES_BIASCOMP((*(uint32_t*)ADC_FUSES_BIASCOMP_ADDR)) >>
|
|
|
|
ADC_CALIB_BIASCOMP_Pos |
|
|
|
|
ADC_FUSES_BIASREFBUF((*(uint32_t*)ADC_FUSES_BIASREFBUF_ADDR) >>
|
|
|
|
ADC_FUSES_BIASREFBUF_Pos);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
static int _adc_configure(adc_res_t res)
|
|
|
|
{
|
2017-05-31 18:39:32 +02:00
|
|
|
/* Individual comparison necessary because ADC Resolution Bits are not
|
|
|
|
* numerically in order and 16Bit (averaging - not currently supported)
|
|
|
|
* falls between 12bit and 10bit. See datasheet for details */
|
2019-11-27 21:01:32 +01:00
|
|
|
if (!((res == ADC_RES_8BIT) || (res == ADC_RES_10BIT) ||
|
|
|
|
(res == ADC_RES_12BIT))){
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
_adc_poweroff();
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2020-08-07 19:37:12 +02:00
|
|
|
if (ADC_DEV->CTRLA.reg & ADC_CTRLA_SWRST ||
|
|
|
|
ADC_DEV->CTRLA.reg & ADC_CTRLA_ENABLE ) {
|
2017-05-10 21:23:00 +02:00
|
|
|
DEBUG("adc: not ready\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2020-08-07 19:37:12 +02:00
|
|
|
_setup_clock();
|
|
|
|
_setup_calibration();
|
|
|
|
|
|
|
|
/* Set ADC resolution */
|
|
|
|
#ifdef ADC_CTRLC_RESSEL
|
|
|
|
/* Reset resolution bits in CTRLC */
|
|
|
|
ADC_DEV->CTRLC.reg &= ~ADC_CTRLC_RESSEL_Msk;
|
|
|
|
ADC_DEV->CTRLC.reg |= res;
|
|
|
|
#else
|
|
|
|
/* Reset resolution bits in CTRLB */
|
|
|
|
ADC_DEV->CTRLB.reg &= ~ADC_CTRLB_RESSEL_Msk;
|
|
|
|
ADC_DEV->CTRLB.reg |= res;
|
|
|
|
#endif
|
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Set Voltage Reference */
|
2020-08-07 19:37:12 +02:00
|
|
|
ADC_DEV->REFCTRL.reg = ADC_REF_DEFAULT;
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Disable all interrupts */
|
2020-08-07 19:37:12 +02:00
|
|
|
ADC_DEV->INTENCLR.reg = 0xFF;
|
|
|
|
|
2020-08-24 13:57:45 +02:00
|
|
|
#ifdef SYSCTRL_VREF_BGOUTEN
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Enable bandgap if VREF is internal 1V */
|
2019-10-01 21:42:42 +02:00
|
|
|
if (ADC_REF_DEFAULT == ADC_REFCTRL_REFSEL_INT1V) {
|
2017-05-10 21:23:00 +02:00
|
|
|
SYSCTRL->VREF.reg |= SYSCTRL_VREF_BGOUTEN;
|
|
|
|
}
|
2019-01-21 17:06:58 +01:00
|
|
|
#else
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Enable bandgap if necessary */
|
2019-10-01 21:42:42 +02:00
|
|
|
if (ADC_REF_DEFAULT == ADC_REFCTRL_REFSEL_INTREF) {
|
2017-05-10 21:23:00 +02:00
|
|
|
SUPC->VREF.reg |= SUPC_VREF_VREFOE;
|
|
|
|
}
|
|
|
|
#endif
|
2020-08-07 19:37:12 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Enable ADC Module */
|
2020-08-07 19:37:12 +02:00
|
|
|
ADC_DEV->CTRLA.reg |= ADC_CTRLA_ENABLE;
|
|
|
|
_wait_syncbusy();
|
2017-05-10 21:23:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int adc_init(adc_t line)
|
|
|
|
{
|
2019-02-03 23:17:22 +01:00
|
|
|
if (line >= ADC_NUMOF) {
|
|
|
|
DEBUG("adc: line arg not applicable\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
_prep();
|
|
|
|
gpio_init(adc_channels[line].pin, GPIO_IN);
|
|
|
|
gpio_init_mux(adc_channels[line].pin, GPIO_MUX_B);
|
|
|
|
_done();
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-08 15:31:54 +01:00
|
|
|
int32_t adc_sample(adc_t line, adc_res_t res)
|
2017-05-10 21:23:00 +02:00
|
|
|
{
|
|
|
|
if (line >= ADC_NUMOF) {
|
|
|
|
DEBUG("adc: line arg not applicable\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
_prep();
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
if (_adc_configure(res) != 0) {
|
|
|
|
_done();
|
|
|
|
DEBUG("adc: configuration failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-08-24 13:57:45 +02:00
|
|
|
|
|
|
|
ADC_DEV->INPUTCTRL.reg = ADC_GAIN_FACTOR_DEFAULT
|
|
|
|
| adc_channels[line].muxpos
|
|
|
|
| ADC_NEG_INPUT;
|
2020-08-07 19:37:12 +02:00
|
|
|
_wait_syncbusy();
|
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Start the conversion */
|
2020-08-07 19:37:12 +02:00
|
|
|
ADC_DEV->SWTRIG.reg = ADC_SWTRIG_START;
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
/* Wait for the result */
|
2020-08-07 19:37:12 +02:00
|
|
|
while (!(ADC_DEV->INTFLAG.reg & ADC_INTFLAG_RESRDY)) {}
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2020-08-07 19:37:12 +02:00
|
|
|
int result = ADC_DEV->RESULT.reg;
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
_adc_poweroff();
|
|
|
|
_done();
|
2020-08-24 13:57:45 +02:00
|
|
|
|
2017-05-10 21:23:00 +02:00
|
|
|
return result;
|
|
|
|
}
|