Atmel Software Framework

adc.h File Reference

AVR XMEGA Analog to Digital Converter driver.

Copyright (C) 2010-2017 Atmel Corporation. All rights reserved.

#include <compiler.h>
#include <conf_adc.h>
#include <nvm.h>
#include <parts.h>
#include <sleepmgr.h>
#include <sysclk.h>

Data Structures

struct  adc_channel_config
 ADC channel configuration. More...
 
struct  adc_config
 ADC configuration. More...
 

Macros

#define ADC_CH_GAIN_DIV2_gc   (0x07 << 2)
 
#define ADC_CH_OFFSET_gp   4 /* Positive MUX setting offset group position. */
 
#define ADC_CURRLIMIT_gm   0x60 /* Current limit group mask. */
 
#define ADC_CURRLIMIT_HIGH_gc   (0x03 << 5)
 
#define ADC_CURRLIMIT_LOW_gc   (0x01 << 5)
 
#define ADC_CURRLIMIT_MED_gc   (0x02 << 5)
 
#define ADC_CURRLIMIT_NO_gc   (0x00 << 5)
 
#define ADC_EVACT_SYNCSWEEP_gc   (0x06 << 0)
 
#define ADC_NR_OF_CHANNELS   4
 Number of channels per ADC. More...
 
#define ADC_REFSEL_INTVCC_gc   (0x01 << 4)
 
#define ADC_REFSEL_VCCDIV2_gc   (0x04 << 4)
 
#define CONFIG_ADC_INTLVL   ADC_CH_INTLVL_LO_gc
 Default ADC channel interrupt level. More...
 
Calibration data addresses
Note
The temperature sensor calibration is sampled at 85 degrees Celsius with unsigned, 12-bit conversion.
#define ADCACAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL0)
 ADC A, calibration byte 0. More...
 
#define ADCACAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1)
 ADC A, calibration byte 1. More...
 
#define ADCBCAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL0)
 ADC B, calibration byte 0. More...
 
#define ADCBCAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL1)
 ADC B, calibration byte 1. More...
 
#define TEMPSENSE0   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE0)
 Temperature sensor calibration byte 0. More...
 
#define TEMPSENSE1   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE1)
 Temperature sensor calibration byte 1. More...
 
#define HOTTEMP   offsetof(NVM_PROD_SIGNATURES_t, HOTTEMP)
 Temperature at which TEMPSENSE1/TEMPSENSE0 is measured. More...
 
#define ROOMTEMP   offsetof(NVM_PROD_SIGNATURES_t, ROOMTEMP)
 Temperature at which TEMPSENSE3/TEMPSENSE2 is measured. More...
 
#define TEMPSENSE2   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE2)
 Temperature sensor calibration byte 2. More...
 
#define TEMPSENSE3   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE3)
 Temperature sensor calibration byte 3. More...
 
ADC channel masks
#define ADC_CH0   (1U << 0)
 ADC channel 0. More...
 
#define ADC_CH1   (1U << 1)
 ADC channel 1. More...
 
#define ADC_CH2   (1U << 2)
 ADC channel 2. More...
 
#define ADC_CH3   (1U << 3)
 ADC channel 3. More...
 
Internal ADC input masks
#define ADC_INT_TEMPSENSE   ADC_TEMPREF_bm
 Temperature sensor. More...
 
#define ADC_INT_BANDGAP   ADC_BANDGAP_bm
 Bandgap reference. More...
 

Enumerations

enum  adc_calibration_data {
  ADC_CAL_ADCA,
  ADC_CAL_ADCB,
  ADC_CAL_TEMPSENSE,
  ADC_CAL_HOTTEMP,
  ADC_CAL_TEMPSENSE2,
  ADC_CAL_ROOMTEMP
}
 ADC calibration data. More...
 
enum  ADC_CH_MUXNEG_MODE10_enum {
  ADC_CH_MUXNEG_MODE10_PIN0_gc = (0x00<<0),
  ADC_CH_MUXNEG_MODE10_PIN1_gc = (0x01<<0),
  ADC_CH_MUXNEG_MODE10_PIN2_gc = (0x02<<0),
  ADC_CH_MUXNEG_MODE10_PIN3_gc = (0x03<<0),
  ADC_CH_MUXNEG_MODE10_GND_gc = (0x05<<0),
  ADC_CH_MUXNEG_MODE10_INTGND_gc = (0x07<<0)
}
 
enum  ADC_CH_MUXNEG_MODE11_enum {
  ADC_CH_MUXNEG_MODE11_PIN4_gc = (0x00<<0),
  ADC_CH_MUXNEG_MODE11_PIN5_gc = (0x01<<0),
  ADC_CH_MUXNEG_MODE11_PIN6_gc = (0x02<<0),
  ADC_CH_MUXNEG_MODE11_PIN7_gc = (0x03<<0),
  ADC_CH_MUXNEG_MODE11_INTGND_gc = (0x04<<0),
  ADC_CH_MUXNEG_MODE11_GND_gc = (0x07<<0)
}
 
enum  adc_reference {
  ADC_REF_BANDGAP = ADC_REFSEL_INT1V_gc,
  ADC_REF_VCC = ADC_REFSEL_INTVCC_gc,
  ADC_REF_AREFA = ADC_REFSEL_AREFA_gc,
  ADC_REF_AREFB = ADC_REFSEL_AREFB_gc,
  ADC_REF_VCCDIV2 = ADC_REFSEL_VCCDIV2_gc
}
 ADC reference settings. More...
 
enum  adc_resolution {
  ADC_RES_8 = ADC_RESOLUTION_8BIT_gc,
  ADC_RES_12 = ADC_RESOLUTION_12BIT_gc,
  ADC_RES_12_LEFT = ADC_RESOLUTION_LEFT12BIT_gc
}
 ADC resolution settings. More...
 
enum  adc_sign {
  ADC_SIGN_OFF,
  ADC_SIGN_ON = ADC_CONMODE_bm
}
 ADC signedness settings. More...
 
enum  adc_trigger {
  ADC_TRIG_MANUAL,
  ADC_TRIG_FREERUN,
  ADC_TRIG_EVENT_SINGLE,
  ADC_TRIG_FREERUN_SWEEP,
  ADC_TRIG_EVENT_SWEEP,
  ADC_TRIG_EVENT_SYNCSWEEP
}
 ADC conversion trigger settings. More...
 
enum  adcch_mode {
  ADCCH_MODE_COMPLETE = ADC_CH_INTMODE_COMPLETE_gc,
  ADCCH_MODE_BELOW = ADC_CH_INTMODE_BELOW_gc,
  ADCCH_MODE_ABOVE = ADC_CH_INTMODE_ABOVE_gc
}
 ADC channel interrupt modes. More...
 
enum  adcch_negative_input {
  ADCCH_NEG_PIN0,
  ADCCH_NEG_PIN1,
  ADCCH_NEG_PIN2,
  ADCCH_NEG_PIN3,
  ADCCH_NEG_PIN4,
  ADCCH_NEG_PIN5,
  ADCCH_NEG_PIN6,
  ADCCH_NEG_PIN7,
  ADCCH_NEG_PAD_GND,
  ADCCH_NEG_INTERNAL_GND,
  ADCCH_NEG_NONE
}
 ADC channel negative input. More...
 
enum  adcch_positive_input {
  ADCCH_POS_PIN0,
  ADCCH_POS_PIN1,
  ADCCH_POS_PIN2,
  ADCCH_POS_PIN3,
  ADCCH_POS_PIN4,
  ADCCH_POS_PIN5,
  ADCCH_POS_PIN6,
  ADCCH_POS_PIN7,
  ADCCH_POS_PIN8,
  ADCCH_POS_PIN9,
  ADCCH_POS_PIN10,
  ADCCH_POS_PIN11,
  ADCCH_POS_PIN12,
  ADCCH_POS_PIN13,
  ADCCH_POS_PIN14,
  ADCCH_POS_PIN15,
  ADCCH_POS_TEMPSENSE,
  ADCCH_POS_BANDGAP,
  ADCCH_POS_SCALED_VCC,
  ADCCH_POS_DAC
}
 ADC channel positive input. More...
 

Functions

Internal functions for driver
static __always_inline ADC_CH_t * adc_get_channel (ADC_t *adc, uint8_t ch_mask)
 Get ADC channel pointer from channel mask. More...
 

ADC interrupt callback function

#define CONFIG_ADC_CALLBACK_ENABLE
 Configuration symbol to enable callback on ADC interrupts. More...
 
#define CONFIG_ADC_CALLBACK_TYPE   uint16_t
 Configuration symbol for datatype of result parameter for callback. More...
 
typedef CONFIG_ADC_CALLBACK_TYPE adc_result_t
 Datatype of ADC conversion result parameter for callback. More...
 
typedef void(* adc_callback_t )(ADC_t *adc, uint8_t ch_mask, adc_result_t res)
 ADC interrupt callback function pointer. More...
 
void adc_set_callback (ADC_t *adc, adc_callback_t callback)
 Set ADC interrupt callback function. More...
 

ADC module management

#define adc_get_result(adc, ch_mask)   (adc_get_channel(adc, ch_mask)->RES)
 Get result from ADC channel. More...
 
#define adc_set_compare_value(adc, val)
 Set compare value directly to ADC. More...
 
#define adc_get_compare_value(adc)   ((adc)->CMP)
 Get compare value directly from ADC. More...
 
void adc_enable (ADC_t *adc)
 Enable ADC. More...
 
void adc_disable (ADC_t *adc)
 Disable ADC. More...
 
bool adc_is_enabled (ADC_t *adc)
 Check if the ADC is enabled. More...
 
static void adc_start_conversion (ADC_t *adc, uint8_t ch_mask)
 Start one-shot conversion on ADC channel(s) More...
 
static int16_t adc_get_signed_result (ADC_t *adc, uint8_t ch_mask)
 Get signed result from ADC channel. More...
 
static uint16_t adc_get_unsigned_result (ADC_t *adc, uint8_t ch_mask)
 Get unsigned result from ADC channel. More...
 
static uint8_t adc_get_interrupt_flag (ADC_t *adc, uint8_t ch_mask)
 Get interrupt flag of ADC channel(s) More...
 
static void adc_clear_interrupt_flag (ADC_t *adc, uint8_t ch_mask)
 Clear interrupt flag of ADC channel(s) More...
 
static void adc_wait_for_interrupt_flag (ADC_t *adc, uint8_t ch_mask)
 Wait for interrupt flag of ADC channel(s) More...
 
static void adc_flush (ADC_t *adc)
 Flush the ADC. More...
 
static int16_t adc_get_signed_compare_value (ADC_t *adc)
 Get signed compare value directly from ADC. More...
 
static uint16_t adc_get_unsigned_compare_value (ADC_t *adc)
 Get unsigned compare value directly from ADC. More...
 
static uint16_t adc_get_calibration_data (enum adc_calibration_data cal)
 Get calibration data. More...
 

ADC module configuration

#define adc_set_config_compare_value(conf, val)
 Set ADC compare value in configuration. More...
 
#define adc_get_config_compare_value(conf)   (conf->cmp)
 Get ADC compare value from configuration. More...
 
enum  adc_gainstage_impmode {
  ADC_GAIN_HIGHIMPEDANCE,
  ADC_GAIN_LOWIMPEDANCE
}
 ADC gain stage impedance settings. More...
 
enum  adc_current_limit {
  ADC_CURRENT_LIMIT_NO,
  ADC_CURRENT_LIMIT_LOW,
  ADC_CURRENT_LIMIT_MED,
  ADC_CURRENT_LIMIT_HIGH
}
 ADC current limit settings. More...
 
void adc_write_configuration (ADC_t *adc, const struct adc_config *conf)
 Write configuration to ADC module. More...
 
void adc_read_configuration (ADC_t *adc, struct adc_config *conf)
 Read configuration from ADC module. More...
 
static void adc_set_clock_rate (struct adc_config *conf, uint32_t clk_adc)
 Set ADC prescaler to get desired clock rate. More...
 
static void adc_set_conversion_parameters (struct adc_config *conf, enum adc_sign sign, enum adc_resolution res, enum adc_reference ref)
 Set ADC conversion parameters. More...
 
static void adc_set_conversion_trigger (struct adc_config *conf, enum adc_trigger trig, uint8_t nr_of_ch, uint8_t base_ev_ch)
 Set ADC conversion trigger. More...
 
static void adc_set_dma_request_group (struct adc_config *conf, uint8_t nr_of_ch)
 Set DMA request group. More...
 
static void adc_enable_internal_input (struct adc_config *conf, uint8_t int_inp)
 Enable internal ADC input. More...
 
static void adc_disable_internal_input (struct adc_config *conf, uint8_t int_inp)
 Disable internal ADC input. More...
 
static void adc_set_gain_impedance_mode (struct adc_config *conf, enum adc_gainstage_impmode impmode)
 Set ADC gain stage impedance mode. More...
 
static void adc_set_current_limit (struct adc_config *conf, enum adc_current_limit currlimit)
 Set ADC current limit. More...
 

ADC channel configuration

#define ADCCH_FORCE_1X_GAINSTAGE   0xff
 Force enabling of gainstage with unity gain. More...
 
void adcch_write_configuration (ADC_t *adc, uint8_t ch_mask, const struct adc_channel_config *ch_conf)
 Write configuration to ADC channel. More...
 
void adcch_read_configuration (ADC_t *adc, uint8_t ch_mask, struct adc_channel_config *ch_conf)
 Read configuration from ADC channel. More...
 
static uint8_t adcch_get_gain_setting (uint8_t gain)
 Get ADC channel setting for specified gain. More...
 
static void adcch_set_input (struct adc_channel_config *ch_conf, enum adcch_positive_input pos, enum adcch_negative_input neg, uint8_t gain)
 Set ADC channel input mode, multiplexing and gain. More...
 
static void adcch_set_pin_scan (struct adc_channel_config *ch_conf, uint8_t start_offset, uint8_t max_offset)
 Set ADC channel 0 pin scan. More...
 
static void adcch_set_interrupt_mode (struct adc_channel_config *ch_conf, enum adcch_mode mode)
 Set ADC channel interrupt mode. More...
 
static void adcch_enable_interrupt (struct adc_channel_config *ch_conf)
 Enable interrupts on ADC channel. More...
 
static void adcch_disable_interrupt (struct adc_channel_config *ch_conf)
 Disable interrupts on ADC channel. More...
 

#define ADC_CH_GAIN_DIV2_gc   (0x07 << 2)

Referenced by adcch_get_gain_setting().

#define ADC_CH_OFFSET_gp   4 /* Positive MUX setting offset group position. */

Referenced by adcch_set_pin_scan().

#define ADC_CURRLIMIT_gm   0x60 /* Current limit group mask. */

Referenced by adc_set_current_limit().

#define ADC_CURRLIMIT_HIGH_gc   (0x03 << 5)

Referenced by adc_set_current_limit().

#define ADC_CURRLIMIT_LOW_gc   (0x01 << 5)

Referenced by adc_set_current_limit().

#define ADC_CURRLIMIT_MED_gc   (0x02 << 5)

Referenced by adc_set_current_limit().

#define ADC_CURRLIMIT_NO_gc   (0x00 << 5)

Referenced by adc_set_current_limit().

#define ADC_EVACT_SYNCSWEEP_gc   (0x06 << 0)
#define ADC_REFSEL_INTVCC_gc   (0x01 << 4)

Referenced by adc_foul_callback().

#define ADC_REFSEL_VCCDIV2_gc   (0x04 << 4)

Enumerator
ADC_CH_MUXNEG_MODE10_PIN0_gc 
ADC_CH_MUXNEG_MODE10_PIN1_gc 
ADC_CH_MUXNEG_MODE10_PIN2_gc 
ADC_CH_MUXNEG_MODE10_PIN3_gc 
ADC_CH_MUXNEG_MODE10_GND_gc 
ADC_CH_MUXNEG_MODE10_INTGND_gc 
Enumerator
ADC_CH_MUXNEG_MODE11_PIN4_gc 
ADC_CH_MUXNEG_MODE11_PIN5_gc 
ADC_CH_MUXNEG_MODE11_PIN6_gc 
ADC_CH_MUXNEG_MODE11_PIN7_gc 
ADC_CH_MUXNEG_MODE11_INTGND_gc 
ADC_CH_MUXNEG_MODE11_GND_gc