Atmel Software Framework

adc2.h File Reference

ADC Controller driver.

Copyright (c) 2013-2015 Atmel Corporation. All rights reserved.

#include "compiler.h"
#include "status_codes.h"

Data Structures

struct  adc_config
 ADC Enhanced configuration structure. More...
 
struct  adc_last_channel_config
 ADC Last Channel Specific Measurement configuration structure. More...
 
struct  adc_temp_sensor_config
 ADC Temperature Sensor configuration structure. More...
 

Macros

#define ADC_WPMR_WPKEY_PASSWD   (0x414443u << 8)
 Write Protect Key. More...
 
#define NO_TEMP_SENSOR
 
#define SLEEP_MODE_ADC   SLEEPMGR_SLEEP_WFI
 
#define SLEEP_MODE_ADC   SLEEPMGR_ACTIVE
 
#define TEMP_SENSOR
 

Typedefs

typedef void(* adc_callback_t) (void)
 

Enumerations

enum  adc_channel_num {
  ADC_CHANNEL_0 = 0,
  ADC_CHANNEL_1,
  ADC_CHANNEL_2,
  ADC_CHANNEL_3,
  ADC_CHANNEL_4,
  ADC_CHANNEL_5,
  ADC_CHANNEL_6,
  ADC_CHANNEL_7,
  ADC_CHANNEL_8,
  ADC_CHANNEL_9,
  ADC_CHANNEL_10,
  ADC_CHANNEL_11,
  ADC_CHANNEL_12,
  ADC_CHANNEL_13,
  ADC_CHANNEL_14,
  ADC_CHANNEL_15,
  ADC_TEMPERATURE_SENSOR,
  ADC_CHANNEL_ALL = 0xFFFF
}
 Definitions for ADC channel number. More...
 
enum  adc_cmp_mode {
  ADC_CMP_MODE_0 = ADC_EMR_CMPMODE_LOW,
  ADC_CMP_MODE_1 = ADC_EMR_CMPMODE_HIGH,
  ADC_CMP_MODE_2 = ADC_EMR_CMPMODE_IN,
  ADC_CMP_MODE_3 = ADC_EMR_CMPMODE_OUT
}
 Definitions for Comparison Mode. More...
 
enum  adc_interrupt_source {
  ADC_INTERRUPT_EOC_0 = 0,
  ADC_INTERRUPT_EOC_1,
  ADC_INTERRUPT_EOC_2,
  ADC_INTERRUPT_EOC_3,
  ADC_INTERRUPT_EOC_4,
  ADC_INTERRUPT_EOC_5,
  ADC_INTERRUPT_EOC_6,
  ADC_INTERRUPT_EOC_7,
  ADC_INTERRUPT_EOC_8,
  ADC_INTERRUPT_EOC_9,
  ADC_INTERRUPT_EOC_10,
  ADC_INTERRUPT_EOC_11,
  ADC_INTERRUPT_EOC_12,
  ADC_INTERRUPT_EOC_13,
  ADC_INTERRUPT_EOC_14,
  ADC_INTERRUPT_EOC_15,
  ADC_INTERRUPT_EOC_16,
  ADC_INTERRUPT_TEMP_CHANGE,
  ADC_INTERRUPT_END_CAL,
  ADC_INTERRUPT_DATA_READY,
  ADC_INTERRUPT_OVERRUN_ERROR,
  ADC_INTERRUPT_COMP_ERROR,
  ADC_INTERRUPT_END_RXBUF,
  ADC_INTERRUPT_RXBUF_FULL,
  ADC_INTERRUPT_ALL = 0xFFFFFFFF
}
 ADC interrupt source type. More...
 
enum  adc_last_channel_cmp_mode {
  ADC_LAST_CHANNEL_CMP_MODE_0 = ADC_LCTMR_CMPMOD_LOW,
  ADC_LAST_CHANNEL_CMP_MODE_1 = ADC_LCTMR_CMPMOD_HIGH,
  ADC_LAST_CHANNEL_CMP_MODE_2 = ADC_LCTMR_CMPMOD_IN,
  ADC_LAST_CHANNEL_CMP_MODE_3 = ADC_LCTMR_CMPMOD_OUT
}
 Definitions for Last Channel Specific Measurement Comparison Mode. More...
 
enum  adc_power_mode {
  ADC_POWER_MODE_0 = 0,
  ADC_POWER_MODE_1
}
 Definitions for ADC power mode. More...
 
enum  adc_refer_voltage_source {
  ADC_REFER_VOL_EXTERNAL = 0,
  ADC_REFER_VOL_STUCK_AT_MIN,
  ADC_REFER_VOL_VDDANA,
  ADC_REFER_VOL_IRVS
}
 Definitions for Reference Voltage Selection. More...
 
enum  adc_resolution {
  ADC_12_BITS = ADC_EMR_OSR_NO_AVERAGE,
  ADC_13_BITS = ADC_EMR_OSR_OSR4,
  ADC_14_BITS = ADC_EMR_OSR_OSR16,
  ADC_15_BITS = ADC_EMR_OSR_OSR64,
  ADC_16_BITS = ADC_EMR_OSR_OSR256
}
 Definitions for ADC resolution. More...
 
enum  adc_startup_time {
  ADC_STARTUP_TIME_0 = ADC_MR_STARTUP_SUT0,
  ADC_STARTUP_TIME_1 = ADC_MR_STARTUP_SUT8,
  ADC_STARTUP_TIME_2 = ADC_MR_STARTUP_SUT16,
  ADC_STARTUP_TIME_3 = ADC_MR_STARTUP_SUT24,
  ADC_STARTUP_TIME_4 = ADC_MR_STARTUP_SUT64,
  ADC_STARTUP_TIME_5 = ADC_MR_STARTUP_SUT80,
  ADC_STARTUP_TIME_6 = ADC_MR_STARTUP_SUT96,
  ADC_STARTUP_TIME_7 = ADC_MR_STARTUP_SUT112,
  ADC_STARTUP_TIME_8 = ADC_MR_STARTUP_SUT512,
  ADC_STARTUP_TIME_9 = ADC_MR_STARTUP_SUT576,
  ADC_STARTUP_TIME_10 = ADC_MR_STARTUP_SUT640,
  ADC_STARTUP_TIME_11 = ADC_MR_STARTUP_SUT704,
  ADC_STARTUP_TIME_12 = ADC_MR_STARTUP_SUT768,
  ADC_STARTUP_TIME_13 = ADC_MR_STARTUP_SUT832,
  ADC_STARTUP_TIME_14 = ADC_MR_STARTUP_SUT896,
  ADC_STARTUP_TIME_15 = ADC_MR_STARTUP_SUT960
}
 Definitions for ADC Start Up Time. More...
 
enum  adc_temp_cmp_mode {
  ADC_TEMP_CMP_MODE_0 = ADC_TEMPMR_TEMPCMPMOD_LOW,
  ADC_TEMP_CMP_MODE_1 = ADC_TEMPMR_TEMPCMPMOD_HIGH,
  ADC_TEMP_CMP_MODE_2 = ADC_TEMPMR_TEMPCMPMOD_IN,
  ADC_TEMP_CMP_MODE_3 = ADC_TEMPMR_TEMPCMPMOD_OUT
}
 Definitions for Temperature Comparison Mode. More...
 
enum  adc_trigger {
  ADC_TRIG_SW = ADC_MR_TRGEN_DIS,
  ADC_TRIG_EXT = ADC_MR_TRGSEL_ADC_TRIG0 | ADC_MR_TRGEN,
  ADC_TRIG_TIO_CH_0 = ADC_MR_TRGSEL_ADC_TRIG1 | ADC_MR_TRGEN,
  ADC_TRIG_TIO_CH_1 = ADC_MR_TRGSEL_ADC_TRIG2 | ADC_MR_TRGEN,
  ADC_TRIG_TIO_CH_2 = ADC_MR_TRGSEL_ADC_TRIG3 | ADC_MR_TRGEN,
  ADC_TRIG_RTC_0 = ADC_MR_TRGSEL_ADC_TRIG4 | ADC_MR_TRGEN,
  ADC_TRIG_RTT = ADC_MR_TRGSEL_ADC_TRIG5 | ADC_MR_TRGEN,
  ADC_TRIG_FREERUN = 0xFF
}
 Definitions for ADC trigger. More...
 

Functions

static void adc_average_on_multi_trigger (Adc *const adc)
 Set ADC averaging on serval trigger events. More...
 
static void adc_average_on_single_trigger (Adc *const adc)
 Set ADC averaging on single trigger event. More...
 
static void adc_ch_sanity_check (Adc *const adc, const enum adc_channel_num channel)
 ADC channel sanity check. More...
 
static void adc_channel_disable (Adc *const adc, const enum adc_channel_num adc_ch)
 Disable the specified ADC channel. More...
 
static void adc_channel_enable (Adc *const adc, const enum adc_channel_num adc_ch)
 Enable the specified ADC channel. More...
 
static uint32_t adc_channel_get_status (Adc *const adc, const enum adc_channel_num adc_ch)
 Get the ADC channel status. More...
 
static uint32_t adc_channel_get_value (Adc *const adc, enum adc_channel_num adc_ch)
 Read the Converted Data of the selected channel. More...
 
void adc_configure_sequence (Adc *const adc, const enum adc_channel_num ch_list[], const uint8_t uc_num)
 Configure conversion sequence. More...
 
void adc_disable (void)
 Disable ADC Module. More...
 
void adc_disable_interrupt (Adc *const adc, enum adc_interrupt_source interrupt_source)
 Disable ADC interrupts. More...
 
void adc_enable (void)
 Enable ADC Module. More...
 
void adc_enable_interrupt (Adc *const adc, enum adc_interrupt_source interrupt_source)
 Enable ADC interrupts. More...
 
static enum adc_cmp_mode adc_get_comparison_mode (Adc *const adc)
 Get comparison mode. More...
 
void adc_get_config_defaults (struct adc_config *const cfg)
 Get the ADC default configurations. More...
 
static uint32_t adc_get_interrupt_mask (Adc *const adc)
 Get ADC interrupt mask. More...
 
static uint32_t adc_get_interrupt_status (Adc *const adc)
 Get ADC interrupt status. More...
 
static uint32_t adc_get_latest_chan_num (Adc *const adc)
 Get the Last Converted Channel Number. More...
 
static uint32_t adc_get_latest_value (Adc *const adc)
 Get the Last Data Converted. More...
 
static uint32_t adc_get_overrun_status (Adc *const adc)
 Get ADC overrun error status. More...
 
static Pdc * adc_get_pdc_base (Adc *const adc)
 Get PDC registers base address. More...
 
static uint32_t adc_get_writeprotect_status (Adc *const adc)
 Indicate write protect status. More...
 
enum status_code adc_init (Adc *const adc, struct adc_config *const config)
 Initialize the ADC Module. More...
 
void adc_last_channel_get_config_defaults (struct adc_last_channel_config *const cfg)
 
void adc_last_channel_set_config (Adc *const adc, struct adc_last_channel_config *config)
 
static void adc_ref_vol_sel (Adc *const adc, enum adc_refer_voltage_source adc_ref_src, uint8_t irvs)
 ADC Reference Voltage Selection. More...
 
static void adc_select_clock_source_mck (Adc *const adc)
 Configure ADC clock to mck. More...
 
static void adc_select_clock_source_pck (Adc *const adc)
 Configure ADC clock to pck. More...
 
void adc_set_callback (Adc *const adc, enum adc_interrupt_source source, adc_callback_t callback, uint8_t irq_level)
 Set callback for ADC. More...
 
void adc_set_comparison_mode (Adc *const adc, const enum adc_cmp_mode mode, const enum adc_channel_num channel, uint8_t cmp_filter)
 Configure comparison mode. More...
 
static void adc_set_comparison_window (Adc *const adc, const uint16_t us_low_threshold, const uint16_t us_high_threshold)
 Configure ADC compare window. More...
 
void adc_set_power_mode (Adc *const adc, const enum adc_power_mode mode)
 Configure ADC power mode. More...
 
void adc_set_resolution (Adc *const adc, const enum adc_resolution res)
 Configure conversion resolution. More...
 
static void adc_set_trigger (Adc *const adc, const enum adc_trigger trigger)
 Configure conversion trigger and free run mode. More...
 
static void adc_set_writeprotect (Adc *const adc, const bool is_enable)
 Enable or disable write protection of ADC registers. More...
 
static enum status_code adc_start_calibration (Adc *const adc)
 Launch an automatic calibration of the ADC on next sequence. More...
 
static void adc_start_software_conversion (Adc *const adc)
 Start analog-to-digital conversion. More...
 
void adc_temp_sensor_get_config_defaults (struct adc_temp_sensor_config *const cfg)
 
void adc_temp_sensor_set_config (Adc *const adc, struct adc_temp_sensor_config *config)
 

#define ADC_WPMR_WPKEY_PASSWD   (0x414443u << 8)

Write Protect Key.

Referenced by adc_set_writeprotect().

#define NO_TEMP_SENSOR
#define SLEEP_MODE_ADC   SLEEPMGR_SLEEP_WFI

Referenced by adc_disable(), and adc_enable().

#define SLEEP_MODE_ADC   SLEEPMGR_ACTIVE
#define TEMP_SENSOR

Referenced by adc_ch_sanity_check().

typedef void(* adc_callback_t) (void)

Definitions for ADC channel number.

Enumerator
ADC_CHANNEL_0 
ADC_CHANNEL_1 
ADC_CHANNEL_2 
ADC_CHANNEL_3 
ADC_CHANNEL_4 
ADC_CHANNEL_5 
ADC_CHANNEL_6 
ADC_CHANNEL_7 
ADC_CHANNEL_8 
ADC_CHANNEL_9 
ADC_CHANNEL_10 
ADC_CHANNEL_11 
ADC_CHANNEL_12 
ADC_CHANNEL_13 
ADC_CHANNEL_14 
ADC_CHANNEL_15 
ADC_TEMPERATURE_SENSOR 
ADC_CHANNEL_ALL 

Definitions for Comparison Mode.

Enumerator
ADC_CMP_MODE_0 
ADC_CMP_MODE_1 
ADC_CMP_MODE_2 
ADC_CMP_MODE_3 

ADC interrupt source type.

Enumerator
ADC_INTERRUPT_EOC_0 
ADC_INTERRUPT_EOC_1 
ADC_INTERRUPT_EOC_2 
ADC_INTERRUPT_EOC_3 
ADC_INTERRUPT_EOC_4 
ADC_INTERRUPT_EOC_5 
ADC_INTERRUPT_EOC_6 
ADC_INTERRUPT_EOC_7 
ADC_INTERRUPT_EOC_8 
ADC_INTERRUPT_EOC_9 
ADC_INTERRUPT_EOC_10 
ADC_INTERRUPT_EOC_11 
ADC_INTERRUPT_EOC_12 
ADC_INTERRUPT_EOC_13 
ADC_INTERRUPT_EOC_14 
ADC_INTERRUPT_EOC_15 
ADC_INTERRUPT_EOC_16 
ADC_INTERRUPT_TEMP_CHANGE 
ADC_INTERRUPT_END_CAL 
ADC_INTERRUPT_DATA_READY 
ADC_INTERRUPT_OVERRUN_ERROR 
ADC_INTERRUPT_COMP_ERROR 
ADC_INTERRUPT_END_RXBUF 
ADC_INTERRUPT_RXBUF_FULL 
ADC_INTERRUPT_ALL 

Definitions for Last Channel Specific Measurement Comparison Mode.

Enumerator
ADC_LAST_CHANNEL_CMP_MODE_0 
ADC_LAST_CHANNEL_CMP_MODE_1 
ADC_LAST_CHANNEL_CMP_MODE_2 
ADC_LAST_CHANNEL_CMP_MODE_3 

Definitions for ADC power mode.

Enumerator
ADC_POWER_MODE_0 
ADC_POWER_MODE_1 

Definitions for Reference Voltage Selection.

Enumerator
ADC_REFER_VOL_EXTERNAL 
ADC_REFER_VOL_STUCK_AT_MIN 
ADC_REFER_VOL_VDDANA 
ADC_REFER_VOL_IRVS 

Definitions for ADC resolution.

Enumerator
ADC_12_BITS 
ADC_13_BITS 
ADC_14_BITS 
ADC_15_BITS 
ADC_16_BITS 

Definitions for ADC Start Up Time.

Enumerator
ADC_STARTUP_TIME_0 
ADC_STARTUP_TIME_1 
ADC_STARTUP_TIME_2 
ADC_STARTUP_TIME_3 
ADC_STARTUP_TIME_4 
ADC_STARTUP_TIME_5 
ADC_STARTUP_TIME_6 
ADC_STARTUP_TIME_7 
ADC_STARTUP_TIME_8 
ADC_STARTUP_TIME_9 
ADC_STARTUP_TIME_10 
ADC_STARTUP_TIME_11 
ADC_STARTUP_TIME_12 
ADC_STARTUP_TIME_13 
ADC_STARTUP_TIME_14 
ADC_STARTUP_TIME_15 

Definitions for Temperature Comparison Mode.

Enumerator
ADC_TEMP_CMP_MODE_0 
ADC_TEMP_CMP_MODE_1 
ADC_TEMP_CMP_MODE_2 
ADC_TEMP_CMP_MODE_3 

Definitions for ADC trigger.

Enumerator
ADC_TRIG_SW 
ADC_TRIG_EXT 
ADC_TRIG_TIO_CH_0 
ADC_TRIG_TIO_CH_1 
ADC_TRIG_TIO_CH_2 
ADC_TRIG_RTC_0 
ADC_TRIG_RTT 
ADC_TRIG_FREERUN 

static void adc_average_on_multi_trigger ( Adc *const  adc)
inlinestatic

Set ADC averaging on serval trigger events.

Parameters
adcBase address of the ADC.
static void adc_average_on_single_trigger ( Adc *const  adc)
inlinestatic

Set ADC averaging on single trigger event.

Parameters
adcBase address of the ADC.

Referenced by set_adc_resolution().

static void adc_ch_sanity_check ( Adc *const  adc,
const enum adc_channel_num  channel 
)
inlinestatic

ADC channel sanity check.

Parameters
adcBase address of the ADC.
channelAdc channel number.

References ADC_TEMPERATURE_SENSOR, Assert, TEMP_SENSOR, and UNUSED.

Referenced by adc_channel_disable(), adc_channel_enable(), adc_channel_get_status(), adc_channel_get_value(), and adc_set_comparison_mode().

static void adc_channel_disable ( Adc *const  adc,
const enum adc_channel_num  adc_ch 
)
inlinestatic

Disable the specified ADC channel.

Parameters
adcBase address of the ADC.
adc_chAdc channel number.

References adc_ch_sanity_check(), and ADC_CHANNEL_ALL.

static void adc_channel_enable ( Adc *const  adc,
const enum adc_channel_num  adc_ch 
)
inlinestatic

Enable the specified ADC channel.

Parameters
adcBase address of the ADC.
adc_chAdc channel number.

References adc_ch_sanity_check(), and ADC_CHANNEL_ALL.

Referenced by main().

static uint32_t adc_channel_get_status ( Adc *const  adc,
const enum adc_channel_num  adc_ch 
)
inlinestatic

Get the ADC channel status.

Parameters
adcBase address of the ADC.
adc_chAdc channel number.
Return values
1if channel is enabled.
0if channel is disabled.

References adc_ch_sanity_check().

static uint32_t adc_channel_get_value ( Adc *const  adc,
enum adc_channel_num  adc_ch 
)
inlinestatic

Read the Converted Data of the selected channel.

Parameters
adcBase address of the ADC.
adc_chAdc channel number.
Returns
ADC converted value of the selected channel.

References adc_ch_sanity_check().

Referenced by adc_end_conversion().

static enum adc_cmp_mode adc_get_comparison_mode ( Adc *const  adc)
inlinestatic

Get comparison mode.

Parameters
adcBase address of the ADC.
Return values
Comparemode value.
static uint32_t adc_get_interrupt_mask ( Adc *const  adc)
inlinestatic

Get ADC interrupt mask.

Parameters
adcBase address of the ADC.
Returns
The interrupt mask value.
static uint32_t adc_get_interrupt_status ( Adc *const  adc)
inlinestatic

Get ADC interrupt status.

Parameters
adcBase address of the ADC.
Returns
The interrupt status value.

Referenced by adc_init(), and adc_process_callback().

static uint32_t adc_get_latest_chan_num ( Adc *const  adc)
inlinestatic

Get the Last Converted Channel Number.

Parameters
adcBase address of the ADC.
Returns
ADC Last Converted Channel Number.

References adc_disable_interrupt(), and adc_enable_interrupt().

static uint32_t adc_get_latest_value ( Adc *const  adc)
inlinestatic

Get the Last Data Converted.

Parameters
adcBase address of the ADC.
Returns
ADC latest converted value.
static uint32_t adc_get_overrun_status ( Adc *const  adc)
inlinestatic

Get ADC overrun error status.

Parameters
adcBase address of the ADC.
Returns
ADC overrun error status.
static Pdc* adc_get_pdc_base ( Adc *const  adc)
inlinestatic

Get PDC registers base address.

Parameters
adcBase address of the ADC.
Returns
Adc Pdc register base address.
static uint32_t adc_get_writeprotect_status ( Adc *const  adc)
inlinestatic

Indicate write protect status.

Parameters
adcBase address of the ADC.
Returns
0 if no write protect violation occurred, or 16-bit write protect violation source.
void adc_last_channel_get_config_defaults ( struct adc_last_channel_config *const  cfg)

Referenced by adc_set_config().

void adc_last_channel_set_config ( Adc *const  adc,
struct adc_last_channel_config config 
)

Referenced by adc_set_config().

static void adc_ref_vol_sel ( Adc *const  adc,
enum adc_refer_voltage_source  adc_ref_src,
uint8_t  irvs 
)
inlinestatic

ADC Reference Voltage Selection.

Parameters
adcBase address of the ADC.
adc_ref_srcThe source selection for ADC reference voltage, ADC_REFER_VOL_EXTERNAL - the external pin ADVREF defines the voltage reference. ADC_REFER_VOL_STUCK_AT_MIN - the internal reference voltage is stuck at the minimum value ADC_REFER_VOL_VDDANA - the internal voltage reference is forced to VDDANA. Effective only if ONREF is 1. ADC_REFER_VOL_IRVS - the internal reference voltage is defined by field IRVS See the product electrical characteristics for further details.
irvsInternal reference volatage selection, only be effective when adc_ref_src equals to ADC_REFER_VOL_IRVS

References ADC_REFER_VOL_EXTERNAL, ADC_REFER_VOL_IRVS, ADC_REFER_VOL_STUCK_AT_MIN, and ADC_REFER_VOL_VDDANA.

static void adc_select_clock_source_mck ( Adc *const  adc)
inlinestatic

Configure ADC clock to mck.

Parameters
adcBase address of the ADC.

Referenced by main().

static void adc_select_clock_source_pck ( Adc *const  adc)
inlinestatic

Configure ADC clock to pck.

Parameters
adcBase address of the ADC.
static void adc_set_comparison_window ( Adc *const  adc,
const uint16_t  us_low_threshold,
const uint16_t  us_high_threshold 
)
inlinestatic

Configure ADC compare window.

Parameters
adcBase address of the ADC.
us_low_thresholdLow threshold of compare window.
us_high_thresholdHigh threshold of compare window.
static void adc_set_trigger ( Adc *const  adc,
const enum adc_trigger  trigger 
)
inlinestatic

Configure conversion trigger and free run mode.

Parameters
adcBase address of the ADC.
triggerConversion trigger.

References adc_set_comparison_mode(), adc_set_resolution(), and ADC_TRIG_FREERUN.

Referenced by main().

static void adc_set_writeprotect ( Adc *const  adc,
const bool  is_enable 
)
inlinestatic

Enable or disable write protection of ADC registers.

Parameters
adcBase address of the ADC.
is_enable1 to enable, 0 to disable.

References ADC_WPMR_WPKEY_PASSWD.

static enum status_code adc_start_calibration ( Adc *const  adc)
inlinestatic

Launch an automatic calibration of the ADC on next sequence.

Parameters
adcBase address of the ADC.
Return values
STATUS_OKAn automatic calibration is launched.
STATUS_ERR_BUSYAutomatic calibration can not be launched because the ADC is in freerun mode.

References STATUS_ERR_BUSY, and STATUS_OK.

Referenced by main().

static void adc_start_software_conversion ( Adc *const  adc)
inlinestatic

Start analog-to-digital conversion.

Note
If one of the hardware event is selected as ADC trigger, this function can NOT start analog to digital conversion.
Parameters
adcBase address of the ADC.

References adc_set_power_mode().

void adc_temp_sensor_get_config_defaults ( struct adc_temp_sensor_config *const  cfg)

Referenced by adc_set_config().

void adc_temp_sensor_set_config ( Adc *const  adc,
struct adc_temp_sensor_config config 
)

Referenced by adc_set_config().