Microchip® Advanced Software Framework

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages

Management and configuration functions for the ADC module.

The API functions and definitions can be divided in three groups:

Data Structures

struct  adc_config
 ADC configuration. More...
 

Macros

#define ADC_NR_OF_CHANNELS   4
 Number of channels per ADC. 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_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...
 

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...
 

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

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...
 
#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...
 

ADC module management

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...
 
#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...
 

ADC module configuration

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...
 
#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...
 

#define ADC_CH0   (1U << 0)
#define ADC_CH1   (1U << 1)

ADC channel 1.

#define ADC_CH2   (1U << 2)

ADC channel 2.

#define ADC_CH3   (1U << 3)

ADC channel 3.

#define adc_get_compare_value (   adc)    ((adc)->CMP)

Get compare value directly from ADC.

Gets the compare value directly from the ADC, for quick access while the ADC is enabled.

Parameters
adcPointer to ADC module.
Returns
Current compare value of the ADC. Signedness does not matter.
Note
This macro does not protect the 16-bit read from interrupts. If an interrupt may do a 16-bit read or write to the ADC while this macro is executing, interrupts must be temporarily disabled to avoid corruption of the read.
#define adc_get_config_compare_value (   conf)    (conf->cmp)

Get ADC compare value from configuration.

Parameters
confPointer to ADC module configuration.
#define adc_get_result (   adc,
  ch_mask 
)    (adc_get_channel(adc, ch_mask)->RES)

Get result from ADC channel.

Gets the latest conversion result from the ADC channel.

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (Only a single channel can be given in mask)
Returns
Latest conversion result of ADC channel. Signedness does not matter.
Note
This macro does not protect the 16-bit read from interrupts. If an interrupt may do a 16-bit read or write to the ADC while this macro is executing, interrupts must be temporarily disabled to avoid corruption of the read.

Referenced by differential_signed_average(), get_mean_sample_value(), ISR(), and single_ended_unsigned_average().

#define ADC_INT_BANDGAP   ADC_BANDGAP_bm

Bandgap reference.

#define ADC_INT_TEMPSENSE   ADC_TEMPREF_bm

Temperature sensor.

Referenced by main().

#define ADC_NR_OF_CHANNELS   4

Number of channels per ADC.

Referenced by adc_get_channel(), adc_set_conversion_trigger(), and adc_set_dma_request_group().

#define adc_set_compare_value (   adc,
  val 
)
Value:
do { \
irqflags_t ATPASTE2(adc_flags, __LINE__) = cpu_irq_save(); \
(adc)->CMP = val; \
cpu_irq_restore(ATPASTE2(adc_flags, __LINE__)); \
} \
while (0)
#define ATPASTE2(a, b)
Definition: tpaste.h:79
static irqflags_t cpu_irq_save(void)
Get and clear the global interrupt flags.
Definition: interrupt_avr8.h:98
static void cpu_irq_restore(irqflags_t flags)
Restore global interrupt flags.
Definition: interrupt_avr8.h:105
uint8_t irqflags_t
Type used for holding state of interrupt flag.
Definition: interrupt_avr8.h:96

Set compare value directly to ADC.

Sets the compare value directly to the ADC, for quick access while the ADC is enabled.

Parameters
adcPointer to ADC module.
valCompare value to set, either signed or unsigned.
Note
The ADC must be enabled for this function to have any effect.
#define adc_set_config_compare_value (   conf,
  val 
)
Value:
do { \
conf->cmp = (uint16_t)val; \
} \
while (0)

Set ADC compare value in configuration.

Parameters
confPointer to ADC module configuration.
valCompare value to set.
#define ADCACAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL0)

ADC A, calibration byte 0.

Referenced by adc_get_calibration_data().

#define ADCACAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1)

ADC A, calibration byte 1.

Referenced by adc_get_calibration_data().

#define ADCBCAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL0)

ADC B, calibration byte 0.

Referenced by adc_get_calibration_data().

#define ADCBCAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL1)

ADC B, calibration byte 1.

Referenced by adc_get_calibration_data().

#define CONFIG_ADC_CALLBACK_ENABLE

Configuration symbol to enable callback on ADC interrupts.

Define this symbol in conf_adc.h to enable callbacks on ADC interrupts. A function of type adc_callback_t must be defined by the user, and the driver be configured to use it with adc_set_callback.

#define CONFIG_ADC_CALLBACK_TYPE   uint16_t

Configuration symbol for datatype of result parameter for callback.

Define the datatype of the ADC conversion result parameter for callback functions. This should be defined according to the signedness and resolution of the conversions:

  • int16_t for signed, 12-bit
  • uint16_t for unsigned, 12-bit (the default type)
  • int8_t for signed, 8-bit
  • uint8_t for unsigned, 8-bit

Define this in conf_adc.h if the default datatype is not desired.

#define HOTTEMP   offsetof(NVM_PROD_SIGNATURES_t, HOTTEMP)

Temperature at which TEMPSENSE1/TEMPSENSE0 is measured.

Referenced by adc_get_calibration_data().

#define ROOMTEMP   offsetof(NVM_PROD_SIGNATURES_t, ROOMTEMP)

Temperature at which TEMPSENSE3/TEMPSENSE2 is measured.

Referenced by adc_get_calibration_data().

#define TEMPSENSE0   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE0)

Temperature sensor calibration byte 0.

Referenced by adc_get_calibration_data().

#define TEMPSENSE1   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE1)

Temperature sensor calibration byte 1.

Referenced by adc_get_calibration_data().

#define TEMPSENSE2   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE2)

Temperature sensor calibration byte 2.

Referenced by adc_get_calibration_data().

#define TEMPSENSE3   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE3)

Temperature sensor calibration byte 3.

Referenced by adc_get_calibration_data().

typedef void(* adc_callback_t)(ADC_t *adc, uint8_t ch_mask, adc_result_t res)

ADC interrupt callback function pointer.

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (Only a single channel can be given in mask)
resADC conversion result.

Datatype of ADC conversion result parameter for callback.

ADC calibration data.

Enumerator
ADC_CAL_ADCA 

ADC A pipeline calibration data.

ADC_CAL_ADCB 

ADC B pipeline calibration data.

ADC_CAL_TEMPSENSE 

Temperature sensor calibration data.

Note
12-bit unsigned, measured at 85 degrees Celsius, equivalent to 358.15 kelvin.

For AVR XMEGA E devices, the calibration reading is 12 Bit signed. AVR XMEGA E production signature row contains data for two calibration points. Each calibration point has one byte for storing the temperature at which the internal temperature sensor is measured and two bytes for the corresponding ADC reading.

ADC_CAL_HOTTEMP 

Normally one calibration point is taken at 85 Deg C, but the exact value in Deg C is given in the HOTTEMP signature row.

ADC_CAL_TEMPSENSE2 

ADC reading at ROOMTEMP in stored in TEMPSENSE2.

ADC_CAL_ROOMTEMP 

The room temperature (in Deg C) measured during device manufacturing.

ADC current limit settings.

Enumerator
ADC_CURRENT_LIMIT_NO 

No current limit.

ADC_CURRENT_LIMIT_LOW 

Low current limit, max sampling rate 1.5 MSPS.

ADC_CURRENT_LIMIT_MED 

Medium current limit, max sampling rate 1 MSPS.

ADC_CURRENT_LIMIT_HIGH 

High current limit, max sampling rate 0.5 MSPS.

ADC gain stage impedance settings.

Enumerator
ADC_GAIN_HIGHIMPEDANCE 

High impedance sources.

ADC_GAIN_LOWIMPEDANCE 

Low impedance sources.

ADC reference settings.

Note
The choice in voltage reference varies between device families. Refer to the device manual for detailed information.
Enumerator
ADC_REF_BANDGAP 

Internal 1 V from bandgap reference.

ADC_REF_VCC 

VCC divided by 1.6.

ADC_REF_AREFA 

External reference on AREFA pin.

ADC_REF_AREFB 

External reference on AREFB pin.

ADC_REF_VCCDIV2 

VCC divided by 2.

ADC resolution settings.

Enumerator
ADC_RES_8 

8-bit resolution, right-adjusted.

ADC_RES_12 

12-bit resolution, right-adjusted.

ADC_RES_12_LEFT 

12-bit resolution, left-adjusted.

enum adc_sign

ADC signedness settings.

Enumerator
ADC_SIGN_OFF 

Unsigned conversions.

ADC_SIGN_ON 

Signed conversions.

ADC conversion trigger settings.

Note
The choice in conversion triggers varies between device families. Refer to the device manual for detailed information.
Enumerator
ADC_TRIG_MANUAL 

Manually triggered conversions.

ADC_TRIG_FREERUN 

Freerun mode conversion.

ADC_TRIG_EVENT_SINGLE 

Event-triggered conversions on individual channels Pairs each event channel with an ADC channel.

Note
The maximum base event channel that can be used is determined by the number of channels to trigger conversions on.
ADC_TRIG_FREERUN_SWEEP 

Freerunning conversion sweeps.

Note
These will start as soon as the ADC is enabled.
ADC_TRIG_EVENT_SWEEP 

Event-triggered conversion sweeps.

Note
Only the base event channel is used in this mode.
ADC_TRIG_EVENT_SYNCSWEEP 

Event-triggered, synchronized conversion sweeps.

Note
Only the base event channel is used in this mode.

static void adc_clear_interrupt_flag ( ADC_t *  adc,
uint8_t  ch_mask 
)
inlinestatic

Clear interrupt flag of ADC channel(s)

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)
Note
The ADC must be enabled for this function to have any effect.

Referenced by adc_wait_for_interrupt_flag().

void adc_disable ( ADC_t *  adc)

Disable ADC.

Disables the ADC and unlocks IDLE mode for the sleep manager.

Parameters
adcPointer to ADC module

References adc_disable_clock(), cpu_irq_restore(), cpu_irq_save(), SLEEPMGR_IDLE, and sleepmgr_unlock_mode().

Referenced by differential_signed_average(), and single_ended_unsigned_average().

static void adc_disable_internal_input ( struct adc_config conf,
uint8_t  int_inp 
)
inlinestatic

Disable internal ADC input.

Parameters
confPointer to ADC module configuration.
int_inpInternal input to disable:
  • ADC_INT_TEMPSENSE for temperature sensor.
  • ADC_INT_BANDGAP for bandgap reference.

References adc_config::refctrl.

void adc_enable ( ADC_t *  adc)

Enable ADC.

Enables the ADC and locks IDLE mode for the sleep manager.

Parameters
adcPointer to ADC module
Note
To ensure accurate conversions, please wait for at least the specified start-up time between enabling the ADC module, and starting a conversion. For most XMEGA devices the start-up time is specified to be a maximum of 24 ADC clock cycles. Please verify the start-up time for the device in use.

References adc_enable_clock(), cpu_irq_restore(), cpu_irq_save(), SLEEPMGR_IDLE, and sleepmgr_lock_mode().

Referenced by differential_signed_average(), main(), and single_ended_unsigned_average().

static void adc_enable_internal_input ( struct adc_config conf,
uint8_t  int_inp 
)
inlinestatic

Enable internal ADC input.

Parameters
confPointer to ADC module configuration.
int_inpInternal input to enable:
  • ADC_INT_TEMPSENSE for temperature sensor.
  • ADC_INT_BANDGAP for bandgap reference.

References adc_config::refctrl.

Referenced by main().

static void adc_flush ( ADC_t *  adc)
inlinestatic

Flush the ADC.

Forces the ADC to abort any ongoing conversions and restart its clock on the next peripheral clock cycle. Pending conversions are started after the clock reset.

Parameters
adcPointer to ADC module.
Note
The ADC must be enabled for this function to have any effect.

References cpu_irq_restore(), and cpu_irq_save().

static uint16_t adc_get_calibration_data ( enum adc_calibration_data  cal)
inlinestatic
static __always_inline ADC_CH_t* adc_get_channel ( ADC_t *  adc,
uint8_t  ch_mask 
)
static

Get ADC channel pointer from channel mask.

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (Only a single channel can be given in mask)
Returns
Pointer to ADC channel

References ADC_NR_OF_CHANNELS, and Assert.

Referenced by adc_get_signed_result(), adc_get_unsigned_result(), adcch_read_configuration(), and adcch_write_configuration().

static uint8_t adc_get_interrupt_flag ( ADC_t *  adc,
uint8_t  ch_mask 
)
inlinestatic

Get interrupt flag of ADC channel(s)

Returns the interrupt flag of the masked channels. The meaning of the interrupt flag depends on what mode the individual channels are in.

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)
Returns
Mask with interrupt flags.

Referenced by adc_wait_for_interrupt_flag().

static int16_t adc_get_signed_compare_value ( ADC_t *  adc)
inlinestatic

Get signed compare value directly from ADC.

Gets the signed compare value directly from the ADC, with interrupt protection of the 16-bit read, for quick access while the ADC is enabled.

Parameters
adcPointer to ADC module.

References cpu_irq_restore(), and cpu_irq_save().

static int16_t adc_get_signed_result ( ADC_t *  adc,
uint8_t  ch_mask 
)
inlinestatic

Get signed result from ADC channel.

Returns the latest conversion result from the ADC channel as a signed type, with interrupt protection of the 16-bit read.

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (Only a single channel can be given in mask)
Returns
Latest conversion result of ADC channel, as signed 16-bit integer.

References adc_get_channel(), cpu_irq_restore(), and cpu_irq_save().

static uint16_t adc_get_unsigned_compare_value ( ADC_t *  adc)
inlinestatic

Get unsigned compare value directly from ADC.

Gets the unsigned compare value directly from the ADC, with interrupt protection of the 16-bit read, for quick access while the ADC is enabled.

Parameters
adcPointer to ADC module.

References cpu_irq_restore(), and cpu_irq_save().

static uint16_t adc_get_unsigned_result ( ADC_t *  adc,
uint8_t  ch_mask 
)
inlinestatic

Get unsigned result from ADC channel.

Returns the latest conversion result from the ADC channel as an unsigned type, with interrupt protection of the 16-bit read.

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (Only a single channel can be given in mask)
Returns
Latest conversion result of ADC channel, as unsigned 16-bit integer.

References adc_get_channel(), cpu_irq_restore(), and cpu_irq_save().

bool adc_is_enabled ( ADC_t *  adc)

Check if the ADC is enabled.

Parameters
adcPointer to ADC module.
Return values
trueif ADC is enabled.
falseif ADC is disabled.

References Assert.

void adc_set_callback ( ADC_t *  adc,
adc_callback_t  callback 
)

Set ADC interrupt callback function.

Sets a new callback function for interrupts on the specified ADC.

Parameters
adcPointer to ADC module.
callbackPointer to the callback function to set.

References Assert, cpu_irq_restore(), and cpu_irq_save().

Referenced by main().

static void adc_set_clock_rate ( struct adc_config conf,
uint32_t  clk_adc 
)
inlinestatic

Set ADC prescaler to get desired clock rate.

Sets the ADC prescaling so that its clock rate becomes at most clk_adc_hz. This is done by computing the ratio of the peripheral clock rate to the desired ADC clock rate, and rounding it upward to the nearest prescaling factor.

Parameters
confPointer to ADC module configuration.
clk_adcDesired ADC clock rate.
Note
The sample rate is not determined solely by the ADC clock rate for all devices. Setting the current limit mode on some devices will also affect the maximum ADC sampling rate. Refer to the device manual for detailed information on conversion timing and/or the current limitation mode.

References Assert, adc_config::prescaler, and sysclk_get_per_hz().

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().

static void adc_set_conversion_parameters ( struct adc_config conf,
enum adc_sign  sign,
enum adc_resolution  res,
enum adc_reference  ref 
)
inlinestatic

Set ADC conversion parameters.

Sets the signedness, resolution and voltage reference for conversions in the ADC module configuration.

Parameters
confPointer to ADC module configuration.
signConversion signedness.
resResolution of conversions.
refVoltage reference to use.

References adc_config::ctrlb, and adc_config::refctrl.

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().

static void adc_set_conversion_trigger ( struct adc_config conf,
enum adc_trigger  trig,
uint8_t  nr_of_ch,
uint8_t  base_ev_ch 
)
inlinestatic

Set ADC conversion trigger.

Configures the conversion triggering of the ADC.

For automatic triggering modes, the number of channels to start conversions on must be specified with nr_of_ch. The channel selection for these modes is incrementally inclusive, always starting with channel 0.

For event triggered modes, the base event channel must also be specified with base_ev_ch. The event channels are assigned to the ADC channels in an incremental fashion without wrap-around (in single-trigger event mode). This means that the maximum base event channel that can be used is determined by the number of ADC channels to start conversions on, i.e., nr_of_ch.

Parameters
confPointer to ADC module configuration.
trigConversion trigger to set.
nr_of_chNumber of ADC channels to trigger conversions on:
  • 1 - ADC_NR_OF_CHANNELS (must be non-zero).
base_ev_chBase event channel, if used.

References ADC_EVACT_SYNCSWEEP_gc, ADC_NR_OF_CHANNELS, ADC_TRIG_EVENT_SINGLE, ADC_TRIG_EVENT_SWEEP, ADC_TRIG_EVENT_SYNCSWEEP, ADC_TRIG_FREERUN, ADC_TRIG_FREERUN_SWEEP, ADC_TRIG_MANUAL, Assert, adc_config::ctrlb, and adc_config::evctrl.

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().

static void adc_set_current_limit ( struct adc_config conf,
enum adc_current_limit  currlimit 
)
inlinestatic

Set ADC current limit.

Set the current limit mode for the ADC module. This setting affects the max sampling rate of the ADC.

Note
See the device datasheet and manual for detailed information about current consumption and sample rate limit.
Parameters
confPointer to ADC module configuration.
currlimitCurrent limit setting.
Note
Not all device families feature this setting.

References ADC_CURRENT_LIMIT_HIGH, ADC_CURRENT_LIMIT_LOW, ADC_CURRENT_LIMIT_MED, ADC_CURRENT_LIMIT_NO, ADC_CURRLIMIT_gm, ADC_CURRLIMIT_HIGH_gc, ADC_CURRLIMIT_LOW_gc, ADC_CURRLIMIT_MED_gc, ADC_CURRLIMIT_NO_gc, Assert, and adc_config::ctrlb.

static void adc_set_dma_request_group ( struct adc_config conf,
uint8_t  nr_of_ch 
)
inlinestatic

Set DMA request group.

Configures the DMA group request for the specified number of ADC channels. The channel group selection is incrementally inclusive, always starting with channel 0.

Parameters
confPointer to ADC module configuration.
nr_of_chNumber of channels for group request:
  • 0 to disable.
  • 2, 3 or 4 to enable.
Note
The number of channels in the DMA request group cannot be 1.
Not all device families feature this setting.

References ADC_NR_OF_CHANNELS, Assert, and adc_config::ctrla.

static void adc_set_gain_impedance_mode ( struct adc_config conf,
enum adc_gainstage_impmode  impmode 
)
inlinestatic

Set ADC gain stage impedance mode.

Parameters
confPointer to ADC module configuration.
impmodeGain stage impedance mode.
Note
Not all device families feature this setting.

References ADC_GAIN_HIGHIMPEDANCE, ADC_GAIN_LOWIMPEDANCE, Assert, and adc_config::ctrlb.

static void adc_start_conversion ( ADC_t *  adc,
uint8_t  ch_mask 
)
inlinestatic

Start one-shot conversion on ADC channel(s)

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)
Note
The ADC must be enabled for this function to have any effect.

References cpu_irq_restore(), and cpu_irq_save().

Referenced by adc_handler(), differential_signed_average(), get_mean_sample_value(), ISR(), main(), and single_ended_unsigned_average().

static void adc_wait_for_interrupt_flag ( ADC_t *  adc,
uint8_t  ch_mask 
)
inlinestatic

Wait for interrupt flag of ADC channel(s)

Waits for the interrupt flag of the specified channel(s) to be set, then clears it before returning. If several channels are masked, the function will wait for all interrupt flags to be set.

Parameters
adcPointer to ADC module.
ch_maskMask of ADC channel(s):
  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)

References adc_clear_interrupt_flag(), and adc_get_interrupt_flag().

Referenced by differential_signed_average(), get_mean_sample_value(), main(), and single_ended_unsigned_average().

void adc_write_configuration ( ADC_t *  adc,
const struct adc_config conf 
)

Write configuration to ADC module.

Disables the ADC and flushes its pipeline before writing the specified configuration and factory calibration value to it. If the ADC was enabled upon entry of the function, it is enabled upon function return.

Parameters
adcPointer to ADC module.
confPointer to ADC module configuration.

References ADC_CAL_ADCA, ADC_CAL_ADCB, adc_disable_clock(), adc_enable_clock(), adc_get_calibration_data(), Assert, adc_config::cmp, cpu_irq_restore(), cpu_irq_save(), adc_config::ctrla, adc_config::ctrlb, adc_config::evctrl, adc_config::prescaler, and adc_config::refctrl.

Referenced by main(), run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), run_single_channel_8bit_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().