Microchip® Advanced Software Framework

afec.h File Reference

Analog-Front-End Controller driver for SAM.

Copyright (c) 2013-2018 Microchip Technology Inc. and its subsidiaries.

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

Data Structures

struct  afec_ch_config
 AFEC channel configuration structure. More...
 
struct  afec_config
 Analog-Front-End Controller configuration structure. More...
 
struct  afec_temp_sensor_config
 AFEC Temperature Sensor configuration structure. More...
 

Typedefs

typedef void(* afec_callback_t )(void)
 

Enumerations

enum  afec_channel_num {
  AFEC_CHANNEL_0 = 0,
  AFEC_CHANNEL_1,
  AFEC_CHANNEL_2,
  AFEC_CHANNEL_3,
  AFEC_CHANNEL_4,
  AFEC_CHANNEL_5,
  AFEC_CHANNEL_6,
  AFEC_CHANNEL_7,
  AFEC_CHANNEL_8,
  AFEC_CHANNEL_9,
  AFEC_CHANNEL_10,
  AFEC_CHANNEL_11,
  AFEC_CHANNEL_12,
  AFEC_CHANNEL_13,
  AFEC_CHANNEL_14,
  AFEC_TEMPERATURE_SENSOR,
  AFEC_CHANNEL_ALL = 0xFFFF
}
 Definitions for AFEC channel number. More...
 
enum  afec_cmp_mode {
  AFEC_CMP_MODE_0 = AFEC_EMR_CMPMODE_LOW,
  AFEC_CMP_MODE_1 = AFEC_EMR_CMPMODE_HIGH,
  AFEC_CMP_MODE_2 = AFEC_EMR_CMPMODE_IN,
  AFEC_CMP_MODE_3 = AFEC_EMR_CMPMODE_OUT
}
 Definitions for Comparison Mode. More...
 
enum  afec_gainvalue {
  AFEC_GAINVALUE_0 = 0,
  AFEC_GAINVALUE_1 = 1,
  AFEC_GAINVALUE_2 = 2,
  AFEC_GAINVALUE_3 = 3
}
 Definitions for AFEC gain value. More...
 
enum  afec_interrupt_source {
  AFEC_INTERRUPT_EOC_0 = 0,
  AFEC_INTERRUPT_EOC_1,
  AFEC_INTERRUPT_EOC_2,
  AFEC_INTERRUPT_EOC_3,
  AFEC_INTERRUPT_EOC_4,
  AFEC_INTERRUPT_EOC_5,
  AFEC_INTERRUPT_EOC_6,
  AFEC_INTERRUPT_EOC_7,
  AFEC_INTERRUPT_EOC_8,
  AFEC_INTERRUPT_EOC_9,
  AFEC_INTERRUPT_EOC_10,
  AFEC_INTERRUPT_EOC_11,
  AFEC_INTERRUPT_EOC_12,
  AFEC_INTERRUPT_EOC_13,
  AFEC_INTERRUPT_EOC_14,
  AFEC_INTERRUPT_EOC_15,
  AFEC_INTERRUPT_DATA_READY,
  AFEC_INTERRUPT_OVERRUN_ERROR,
  AFEC_INTERRUPT_COMP_ERROR,
  AFEC_INTERRUPT_END_RXBUF,
  AFEC_INTERRUPT_RXBUF_FULL,
  AFEC_INTERRUPT_TEMP_CHANGE,
  AFEC_INTERRUPT_END_CAL,
  _AFEC_NUM_OF_INTERRUPT_SOURCE,
  AFEC_INTERRUPT_ALL = 0xDF00FFFF
}
 AFEC interrupt source type. More...
 
enum  afec_power_mode {
  AFEC_POWER_MODE_0 = 0,
  AFEC_POWER_MODE_1,
  AFEC_POWER_MODE_2
}
 Definitions for AFEC power mode. More...
 
enum  afec_resolution {
  AFEC_12_BITS = AFEC_EMR_RES_NO_AVERAGE,
  AFEC_13_BITS = AFEC_EMR_RES_OSR4,
  AFEC_14_BITS = AFEC_EMR_RES_OSR16,
  AFEC_15_BITS = AFEC_EMR_RES_OSR64,
  AFEC_16_BITS = AFEC_EMR_RES_OSR256
}
 Definitions for AFEC resolution. More...
 
enum  afec_sample_hold_mode {
  AFEC_SAMPLE_HOLD_MODE_0,
  AFEC_SAMPLE_HOLD_MODE_1
}
 
enum  afec_settling_time {
  AFEC_SETTLING_TIME_0 = AFEC_MR_SETTLING_AST3,
  AFEC_SETTLING_TIME_1 = AFEC_MR_SETTLING_AST5,
  AFEC_SETTLING_TIME_2 = AFEC_MR_SETTLING_AST9,
  AFEC_SETTLING_TIME_3 = AFEC_MR_SETTLING_AST17
}
 Definitions for AFEC analog settling time. More...
 
enum  afec_startup_time {
  AFEC_STARTUP_TIME_0 = AFEC_MR_STARTUP_SUT0,
  AFEC_STARTUP_TIME_1 = AFEC_MR_STARTUP_SUT8,
  AFEC_STARTUP_TIME_2 = AFEC_MR_STARTUP_SUT16,
  AFEC_STARTUP_TIME_3 = AFEC_MR_STARTUP_SUT24,
  AFEC_STARTUP_TIME_4 = AFEC_MR_STARTUP_SUT64,
  AFEC_STARTUP_TIME_5 = AFEC_MR_STARTUP_SUT80,
  AFEC_STARTUP_TIME_6 = AFEC_MR_STARTUP_SUT96,
  AFEC_STARTUP_TIME_7 = AFEC_MR_STARTUP_SUT112,
  AFEC_STARTUP_TIME_8 = AFEC_MR_STARTUP_SUT512,
  AFEC_STARTUP_TIME_9 = AFEC_MR_STARTUP_SUT576,
  AFEC_STARTUP_TIME_10 = AFEC_MR_STARTUP_SUT640,
  AFEC_STARTUP_TIME_11 = AFEC_MR_STARTUP_SUT704,
  AFEC_STARTUP_TIME_12 = AFEC_MR_STARTUP_SUT768,
  AFEC_STARTUP_TIME_13 = AFEC_MR_STARTUP_SUT832,
  AFEC_STARTUP_TIME_14 = AFEC_MR_STARTUP_SUT896,
  AFEC_STARTUP_TIME_15 = AFEC_MR_STARTUP_SUT960
}
 Definitions for AFEC Start Up Time. More...
 
enum  afec_temp_cmp_mode {
  AFEC_TEMP_CMP_MODE_0 = AFEC_TEMPMR_TEMPCMPMOD_LOW,
  AFEC_TEMP_CMP_MODE_1 = AFEC_TEMPMR_TEMPCMPMOD_HIGH,
  AFEC_TEMP_CMP_MODE_2 = AFEC_TEMPMR_TEMPCMPMOD_IN,
  AFEC_TEMP_CMP_MODE_3 = AFEC_TEMPMR_TEMPCMPMOD_OUT
}
 Definitions for Temperature Comparison Mode. More...
 
enum  afec_trigger {
  AFEC_TRIG_SW = AFEC_MR_TRGEN_DIS,
  AFEC_TRIG_EXT = AFEC_MR_TRGSEL_AFEC_TRIG0 | AFEC_MR_TRGEN,
  AFEC_TRIG_TIO_CH_0 = AFEC_MR_TRGSEL_AFEC_TRIG1 | AFEC_MR_TRGEN,
  AFEC_TRIG_TIO_CH_1 = AFEC_MR_TRGSEL_AFEC_TRIG2 | AFEC_MR_TRGEN,
  AFEC_TRIG_TIO_CH_2 = AFEC_MR_TRGSEL_AFEC_TRIG3 | AFEC_MR_TRGEN,
  AFEC_TRIG_PWM_EVENT_LINE_0 = AFEC_MR_TRGSEL_AFEC_TRIG4 | AFEC_MR_TRGEN,
  AFEC_TRIG_PWM_EVENT_LINE_1 = AFEC_MR_TRGSEL_AFEC_TRIG5 | AFEC_MR_TRGEN,
  AFEC_TRIG_ANALOG_COMPARATOR = AFEC_MR_TRGSEL_AFEC_TRIG6 | AFEC_MR_TRGEN,
  AFEC_TRIG_FREERUN = 0xFF
}
 Definitions for AFEC trigger. More...
 

Functions

void afec_ch_get_config_defaults (struct afec_ch_config *const cfg)
 Get the AFEC channel default configurations. More...
 
static void afec_ch_sanity_check (Afec *const afec, const enum afec_channel_num channel)
 AFEC channel sanity check. More...
 
void afec_ch_set_config (Afec *const afec, const enum afec_channel_num channel, struct afec_ch_config *config)
 Configure the AFEC channel. More...
 
static void afec_channel_disable (Afec *const afec, const enum afec_channel_num afec_ch)
 Disable the specified AFEC channel. More...
 
static void afec_channel_enable (Afec *const afec, const enum afec_channel_num afec_ch)
 Enable the specified AFEC channel. More...
 
static uint32_t afec_channel_get_status (Afec *const afec, const enum afec_channel_num afec_ch)
 Get the AFEC channel status. More...
 
static uint32_t afec_channel_get_value (Afec *const afec, enum afec_channel_num afec_ch)
 Read the Converted Data of the selected channel. More...
 
static void afec_channel_set_analog_offset (Afec *const afec, enum afec_channel_num afec_ch, uint16_t aoffset)
 Set analog offset to be used for channel CSEL. More...
 
void afec_configure_auto_error_correction (Afec *const afec, const enum afec_channel_num channel, int16_t offsetcorr, uint16_t gaincorr)
 
void afec_configure_sequence (Afec *const afec, const enum afec_channel_num ch_list[], const uint8_t uc_num)
 Configure conversion sequence. More...
 
void afec_disable (Afec *const afec)
 Disable AFEC Module. More...
 
void afec_disable_interrupt (Afec *const afec, enum afec_interrupt_source interrupt_source)
 Disable AFEC interrupts. More...
 
void afec_enable (Afec *const afec)
 Enable AFEC Module. More...
 
void afec_enable_interrupt (Afec *const afec, enum afec_interrupt_source interrupt_source)
 Enable AFEC interrupts. More...
 
static enum afec_cmp_mode afec_get_comparison_mode (Afec *const afec)
 Get comparison mode. More...
 
void afec_get_config_defaults (struct afec_config *const cfg)
 Get the AFEC default configurations. More...
 
uint32_t afec_get_correction_value (Afec *const afec, const enum afec_channel_num afec_ch)
 
static uint32_t afec_get_interrupt_mask (Afec *const afec)
 Get AFEC interrupt mask. More...
 
static uint32_t afec_get_interrupt_status (Afec *const afec)
 Get AFEC interrupt status. More...
 
static uint32_t afec_get_latest_chan_num (Afec *const afec)
 Get the Last Converted Channel Number. More...
 
static uint32_t afec_get_latest_value (Afec *const afec)
 Get the Last Data Converted. More...
 
static uint32_t afec_get_overrun_status (Afec *const afec)
 Get AFEC overrun error status. More...
 
static Pdc * afec_get_pdc_base (Afec *const afec)
 Get PDC registers base address. More...
 
static uint32_t afec_get_writeprotect_status (Afec *const afec)
 Indicate write protect status. More...
 
enum status_code afec_init (Afec *const afec, struct afec_config *const config)
 Initialize the AFEC Module. More...
 
void afec_set_callback (Afec *const afec, enum afec_interrupt_source source, afec_callback_t callback, uint8_t irq_level)
 Set callback for AFEC. More...
 
void afec_set_comparison_mode (Afec *const afec, const enum afec_cmp_mode mode, const enum afec_channel_num channel, uint8_t cmp_filter)
 Configure comparison mode. More...
 
static void afec_set_comparison_window (Afec *const afec, const uint16_t us_low_threshold, const uint16_t us_high_threshold)
 Configure AFEC compare window. More...
 
void afec_set_power_mode (Afec *const afec, const enum afec_power_mode mode)
 Configure AFEC power mode. More...
 
static void afec_set_resolution (Afec *const afec, const enum afec_resolution res)
 Configure conversion resolution. More...
 
void afec_set_sample_hold_mode (Afec *const afec, const enum afec_channel_num channel, const enum afec_sample_hold_mode mode)
 
static void afec_set_trigger (Afec *const afec, const enum afec_trigger trigger)
 Configure conversion trigger and free run mode. More...
 
static void afec_set_writeprotect (Afec *const afec, const bool is_enable)
 Enable or disable write protection of AFEC registers. More...
 
static enum status_code afec_start_calibration (Afec *const afec)
 Launch an automatic calibration of the AFEC on next sequence. More...
 
static void afec_start_software_conversion (Afec *const afec)
 Start analog-to-digital conversion. More...
 
void afec_temp_sensor_get_config_defaults (struct afec_temp_sensor_config *const cfg)
 Get the AFEC Temperature Sensor default configurations. More...
 
void afec_temp_sensor_set_config (Afec *const afec, struct afec_temp_sensor_config *config)
 Configure the AFEC temperature sensor. More...
 

typedef void(* afec_callback_t)(void)

Definitions for AFEC channel number.

Enumerator
AFEC_CHANNEL_0 
AFEC_CHANNEL_1 
AFEC_CHANNEL_2 
AFEC_CHANNEL_3 
AFEC_CHANNEL_4 
AFEC_CHANNEL_5 
AFEC_CHANNEL_6 
AFEC_CHANNEL_7 
AFEC_CHANNEL_8 
AFEC_CHANNEL_9 
AFEC_CHANNEL_10 
AFEC_CHANNEL_11 
AFEC_CHANNEL_12 
AFEC_CHANNEL_13 
AFEC_CHANNEL_14 
AFEC_TEMPERATURE_SENSOR 
AFEC_CHANNEL_ALL 

Definitions for Comparison Mode.

Enumerator
AFEC_CMP_MODE_0 
AFEC_CMP_MODE_1 
AFEC_CMP_MODE_2 
AFEC_CMP_MODE_3 

Definitions for AFEC gain value.

Enumerator
AFEC_GAINVALUE_0 
AFEC_GAINVALUE_1 
AFEC_GAINVALUE_2 
AFEC_GAINVALUE_3 

AFEC interrupt source type.

Enumerator
AFEC_INTERRUPT_EOC_0 
AFEC_INTERRUPT_EOC_1 
AFEC_INTERRUPT_EOC_2 
AFEC_INTERRUPT_EOC_3 
AFEC_INTERRUPT_EOC_4 
AFEC_INTERRUPT_EOC_5 
AFEC_INTERRUPT_EOC_6 
AFEC_INTERRUPT_EOC_7 
AFEC_INTERRUPT_EOC_8 
AFEC_INTERRUPT_EOC_9 
AFEC_INTERRUPT_EOC_10 
AFEC_INTERRUPT_EOC_11 
AFEC_INTERRUPT_EOC_12 
AFEC_INTERRUPT_EOC_13 
AFEC_INTERRUPT_EOC_14 
AFEC_INTERRUPT_EOC_15 
AFEC_INTERRUPT_DATA_READY 
AFEC_INTERRUPT_OVERRUN_ERROR 
AFEC_INTERRUPT_COMP_ERROR 
AFEC_INTERRUPT_END_RXBUF 
AFEC_INTERRUPT_RXBUF_FULL 
AFEC_INTERRUPT_TEMP_CHANGE 
AFEC_INTERRUPT_END_CAL 
_AFEC_NUM_OF_INTERRUPT_SOURCE 
AFEC_INTERRUPT_ALL 

Definitions for AFEC power mode.

Enumerator
AFEC_POWER_MODE_0 
AFEC_POWER_MODE_1 
AFEC_POWER_MODE_2 

Definitions for AFEC resolution.

Enumerator
AFEC_12_BITS 
AFEC_13_BITS 
AFEC_14_BITS 
AFEC_15_BITS 
AFEC_16_BITS 
Enumerator
AFEC_SAMPLE_HOLD_MODE_0 
AFEC_SAMPLE_HOLD_MODE_1 

Definitions for AFEC analog settling time.

Enumerator
AFEC_SETTLING_TIME_0 
AFEC_SETTLING_TIME_1 
AFEC_SETTLING_TIME_2 
AFEC_SETTLING_TIME_3 

Definitions for AFEC Start Up Time.

Enumerator
AFEC_STARTUP_TIME_0 
AFEC_STARTUP_TIME_1 
AFEC_STARTUP_TIME_2 
AFEC_STARTUP_TIME_3 
AFEC_STARTUP_TIME_4 
AFEC_STARTUP_TIME_5 
AFEC_STARTUP_TIME_6 
AFEC_STARTUP_TIME_7 
AFEC_STARTUP_TIME_8 
AFEC_STARTUP_TIME_9 
AFEC_STARTUP_TIME_10 
AFEC_STARTUP_TIME_11 
AFEC_STARTUP_TIME_12 
AFEC_STARTUP_TIME_13 
AFEC_STARTUP_TIME_14 
AFEC_STARTUP_TIME_15 

Definitions for Temperature Comparison Mode.

Enumerator
AFEC_TEMP_CMP_MODE_0 
AFEC_TEMP_CMP_MODE_1 
AFEC_TEMP_CMP_MODE_2 
AFEC_TEMP_CMP_MODE_3 

Definitions for AFEC trigger.

Enumerator
AFEC_TRIG_SW 
AFEC_TRIG_EXT 
AFEC_TRIG_TIO_CH_0 
AFEC_TRIG_TIO_CH_1 
AFEC_TRIG_TIO_CH_2 
AFEC_TRIG_PWM_EVENT_LINE_0 
AFEC_TRIG_PWM_EVENT_LINE_1 
AFEC_TRIG_ANALOG_COMPARATOR 
AFEC_TRIG_FREERUN 

static void afec_ch_sanity_check ( Afec *const  afec,
const enum afec_channel_num  channel 
)
inlinestatic

AFEC channel sanity check.

Parameters
afecBase address of the AFEC.
channelAFEC channel number.

References AFEC_TEMPERATURE_SENSOR, Assert, and UNUSED.

Referenced by afec_ch_set_config(), afec_channel_disable(), afec_channel_enable(), afec_channel_get_status(), afec_channel_get_value(), afec_channel_set_analog_offset(), and afec_set_comparison_mode().

static void afec_channel_disable ( Afec *const  afec,
const enum afec_channel_num  afec_ch 
)
inlinestatic

Disable the specified AFEC channel.

Parameters
afecBase address of the AFEC.
afec_chAFEC channel number.

References afec_ch_sanity_check(), and AFEC_CHANNEL_ALL.

static void afec_channel_enable ( Afec *const  afec,
const enum afec_channel_num  afec_ch 
)
inlinestatic

Enable the specified AFEC channel.

Parameters
afecBase address of the AFEC.
afec_chAFEC channel number.

References afec_ch_sanity_check(), and AFEC_CHANNEL_ALL.

Referenced by set_afec_test().

static uint32_t afec_channel_get_status ( Afec *const  afec,
const enum afec_channel_num  afec_ch 
)
inlinestatic

Get the AFEC channel status.

Parameters
afecBase address of the AFEC.
afec_chAFEC channel number.
Return values
1if channel is enabled.
0if channel is disabled.

References afec_ch_sanity_check().

static uint32_t afec_channel_get_value ( Afec *const  afec,
enum afec_channel_num  afec_ch 
)
inlinestatic

Read the Converted Data of the selected channel.

Parameters
afecBase address of the AFEC.
afec_chAFEC channel number.
Returns
AFEC converted value of the selected channel.

References afec_ch_sanity_check().

Referenced by afec_eoc_0(), and afec_eoc_1().

static void afec_channel_set_analog_offset ( Afec *const  afec,
enum afec_channel_num  afec_ch,
uint16_t  aoffset 
)
inlinestatic

Set analog offset to be used for channel CSEL.

Parameters
afecBase address of the AFEC.
afec_chAFEC channel number.
aoffsetAnalog offset value.

References afec_ch_sanity_check().

Referenced by set_afec_test().

void afec_configure_auto_error_correction ( Afec *const  afec,
const enum afec_channel_num  channel,
int16_t  offsetcorr,
uint16_t  gaincorr 
)
static enum afec_cmp_mode afec_get_comparison_mode ( Afec *const  afec)
inlinestatic

Get comparison mode.

Parameters
afecBase address of the AFEC.
Return values
Comparemode value.
uint32_t afec_get_correction_value ( Afec *const  afec,
const enum afec_channel_num  afec_ch 
)
static uint32_t afec_get_interrupt_mask ( Afec *const  afec)
inlinestatic

Get AFEC interrupt mask.

Parameters
afecBase address of the AFEC.
Returns
The interrupt mask value.

Referenced by afec_process_callback().

static uint32_t afec_get_interrupt_status ( Afec *const  afec)
inlinestatic

Get AFEC interrupt status.

Parameters
afecBase address of the AFEC.
Returns
The interrupt status value.

Referenced by afec_init(), afec_process_callback(), and set_afec_test().

static uint32_t afec_get_latest_chan_num ( Afec *const  afec)
inlinestatic

Get the Last Converted Channel Number.

Parameters
afecBase address of the AFEC.
Returns
AFEC Last Converted Channel Number.
static uint32_t afec_get_latest_value ( Afec *const  afec)
inlinestatic

Get the Last Data Converted.

Parameters
afecBase address of the AFEC.
Returns
AFEC latest converted value.

Referenced by afec0_data_ready(), afec0_diff_data_ready(), and afec1_data_ready().

static uint32_t afec_get_overrun_status ( Afec *const  afec)
inlinestatic

Get AFEC overrun error status.

Parameters
afecBase address of the AFEC.
Returns
AFEC overrun error status.
static Pdc* afec_get_pdc_base ( Afec *const  afec)
inlinestatic

Get PDC registers base address.

Parameters
afecBase address of the AFEC.
Returns
AFEC PDC register base address.
static uint32_t afec_get_writeprotect_status ( Afec *const  afec)
inlinestatic

Indicate write protect status.

Parameters
afecBase address of the AFEC.
Returns
0 if no write protect violation occurred, or 16-bit write protect violation source.
static void afec_set_comparison_window ( Afec *const  afec,
const uint16_t  us_low_threshold,
const uint16_t  us_high_threshold 
)
inlinestatic

Configure AFEC compare window.

Parameters
afecBase address of the AFEC.
us_low_thresholdLow threshold of compare window.
us_high_thresholdHigh threshold of compare window.
static void afec_set_resolution ( Afec *const  afec,
const enum afec_resolution  res 
)
inlinestatic

Configure conversion resolution.

Parameters
afecBase address of the AFEC.
resConversion resolution.
void afec_set_sample_hold_mode ( Afec *const  afec,
const enum afec_channel_num  channel,
const enum afec_sample_hold_mode  mode 
)
static void afec_set_trigger ( Afec *const  afec,
const enum afec_trigger  trigger 
)
inlinestatic

Configure conversion trigger and free run mode.

Parameters
afecBase address of the AFEC.
triggerConversion trigger.

References AFEC_TRIG_FREERUN.

Referenced by configure_tc_trigger(), and set_afec_test().

static void afec_set_writeprotect ( Afec *const  afec,
const bool  is_enable 
)
inlinestatic

Enable or disable write protection of AFEC registers.

Parameters
afecBase address of the AFEC.
is_enable1 to enable, 0 to disable.
static enum status_code afec_start_calibration ( Afec *const  afec)
inlinestatic

Launch an automatic calibration of the AFEC on next sequence.

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

References STATUS_ERR_BUSY, and STATUS_OK.

Referenced by set_afec_test().

static void afec_start_software_conversion ( Afec *const  afec)
inlinestatic

Start analog-to-digital conversion.

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

Referenced by main().