37 #ifndef AFEC_H_INCLUDED 38 #define AFEC_H_INCLUDED 43 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 89 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 97 #if defined __SAM4E8C__ || defined __SAM4E16C__ || defined __SAM4E8CB__ || defined __SAM4E16CB__ 99 enum afec_channel_num {
106 AFEC_TEMPERATURE_SENSOR = 15,
107 AFEC_CHANNEL_ALL = 0x803F,
109 #elif defined __SAM4E8E__ || defined __SAM4E16E__ 111 enum afec_channel_num {
127 AFEC_TEMPERATURE_SENSOR,
128 AFEC_CHANNEL_ALL = 0xFFFF,
130 #elif (SAMV71 || SAMV70 || SAME70 || SAMS70) 132 enum afec_channel_num {
144 AFEC_TEMPERATURE_SENSOR,
145 AFEC_CHANNEL_ALL = 0x0FFF,
148 #define NB_CH_AFE0 (12UL) 149 #define NB_CH_AFE1 (12UL) 152 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 153 enum afec_sample_hold_mode {
155 AFEC_SAMPLE_HOLD_MODE_0,
157 AFEC_SAMPLE_HOLD_MODE_1,
191 enum afec_settling_time {
192 AFEC_SETTLING_TIME_0 = AFEC_MR_SETTLING_AST3,
193 AFEC_SETTLING_TIME_1 = AFEC_MR_SETTLING_AST5,
194 AFEC_SETTLING_TIME_2 = AFEC_MR_SETTLING_AST9,
195 AFEC_SETTLING_TIME_3 = AFEC_MR_SETTLING_AST17
233 enum afec_settling_time settling_time;
271 #if defined __SAM4E8C__ || defined __SAM4E16C__ || defined __SAM4E8CB__ || defined __SAM4E16CB__ 273 enum afec_interrupt_source {
274 AFEC_INTERRUPT_EOC_0 = 0,
275 AFEC_INTERRUPT_EOC_1,
276 AFEC_INTERRUPT_EOC_2,
277 AFEC_INTERRUPT_EOC_3,
278 AFEC_INTERRUPT_EOC_4,
279 AFEC_INTERRUPT_EOC_5,
280 AFEC_INTERRUPT_EOC_15,
281 AFEC_INTERRUPT_DATA_READY,
282 AFEC_INTERRUPT_OVERRUN_ERROR,
283 AFEC_INTERRUPT_COMP_ERROR,
284 AFEC_INTERRUPT_END_RXBUF,
285 AFEC_INTERRUPT_RXBUF_FULL,
286 AFEC_INTERRUPT_TEMP_CHANGE,
287 AFEC_INTERRUPT_END_CAL,
288 _AFEC_NUM_OF_INTERRUPT_SOURCE,
289 AFEC_INTERRUPT_ALL = 0xDF00803F,
291 #elif defined __SAM4E8E__ || defined __SAM4E16E__ 293 enum afec_interrupt_source {
294 AFEC_INTERRUPT_EOC_0 = 0,
295 AFEC_INTERRUPT_EOC_1,
296 AFEC_INTERRUPT_EOC_2,
297 AFEC_INTERRUPT_EOC_3,
298 AFEC_INTERRUPT_EOC_4,
299 AFEC_INTERRUPT_EOC_5,
300 AFEC_INTERRUPT_EOC_6,
301 AFEC_INTERRUPT_EOC_7,
302 AFEC_INTERRUPT_EOC_8,
303 AFEC_INTERRUPT_EOC_9,
304 AFEC_INTERRUPT_EOC_10,
305 AFEC_INTERRUPT_EOC_11,
306 AFEC_INTERRUPT_EOC_12,
307 AFEC_INTERRUPT_EOC_13,
308 AFEC_INTERRUPT_EOC_14,
309 AFEC_INTERRUPT_EOC_15,
310 AFEC_INTERRUPT_DATA_READY,
311 AFEC_INTERRUPT_OVERRUN_ERROR,
312 AFEC_INTERRUPT_COMP_ERROR,
313 AFEC_INTERRUPT_END_RXBUF,
314 AFEC_INTERRUPT_RXBUF_FULL,
315 AFEC_INTERRUPT_TEMP_CHANGE,
316 AFEC_INTERRUPT_END_CAL,
317 _AFEC_NUM_OF_INTERRUPT_SOURCE,
318 AFEC_INTERRUPT_ALL = 0xDF00FFFF,
320 #elif (SAMV71 || SAMV70 || SAME70 || SAMS70) 322 enum afec_interrupt_source {
323 AFEC_INTERRUPT_EOC_0 = 0,
324 AFEC_INTERRUPT_EOC_1,
325 AFEC_INTERRUPT_EOC_2,
326 AFEC_INTERRUPT_EOC_3,
327 AFEC_INTERRUPT_EOC_4,
328 AFEC_INTERRUPT_EOC_5,
329 AFEC_INTERRUPT_EOC_6,
330 AFEC_INTERRUPT_EOC_7,
331 AFEC_INTERRUPT_EOC_8,
332 AFEC_INTERRUPT_EOC_9,
333 AFEC_INTERRUPT_EOC_10,
334 AFEC_INTERRUPT_EOC_11,
335 AFEC_INTERRUPT_DATA_READY,
336 AFEC_INTERRUPT_OVERRUN_ERROR,
337 AFEC_INTERRUPT_COMP_ERROR,
338 AFEC_INTERRUPT_TEMP_CHANGE,
339 _AFEC_NUM_OF_INTERRUPT_SOURCE,
340 AFEC_INTERRUPT_ALL = 0x47000FFF,
356 const enum afec_channel_num ch_list[],
const uint8_t uc_num);
362 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 363 void afec_configure_auto_error_correction(
Afec *
const afec,
364 const enum afec_channel_num channel,int16_t offsetcorr, uint16_t gaincorr);
366 uint32_t afec_get_correction_value(
Afec *
const afec,
367 const enum afec_channel_num afec_ch);
368 void afec_set_sample_hold_mode(
Afec *
const afec,
369 const enum afec_channel_num channel,
const enum afec_sample_hold_mode mode);
381 const enum afec_channel_num channel)
384 #if defined __SAM4E8C__ || defined __SAM4E16C__ || defined __SAM4E8CB__ || defined __SAM4E16CB__ 385 Assert((channel < NB_CH_AFE0) || (channel == AFEC_TEMPERATURE_SENSOR));
386 #elif defined __SAM4E8E__ || defined __SAM4E16E__ 387 Assert(channel < NB_CH_AFE0);
388 #elif (SAMV71 || SAMV70 || SAME70 || SAMS70) 389 Assert(channel < NB_CH_AFE0);
391 }
else if (afec ==
AFEC1) {
392 Assert(channel < NB_CH_AFE1);
442 const enum afec_cmp_mode mode,
const enum afec_channel_num channel,
465 const uint16_t us_low_threshold,
const uint16_t us_high_threshold)
478 const bool is_enable)
542 const enum afec_channel_num afec_ch)
544 if (afec_ch != AFEC_CHANNEL_ALL) {
548 afec->
AFEC_CHER = (afec_ch == AFEC_CHANNEL_ALL) ?
549 AFEC_CHANNEL_ALL : 1 << afec_ch;
559 const enum afec_channel_num afec_ch)
561 if (afec_ch != AFEC_CHANNEL_ALL) {
565 afec->
AFEC_CHDR = (afec_ch == AFEC_CHANNEL_ALL) ?
566 AFEC_CHANNEL_ALL : 1 << afec_ch;
579 const enum afec_channel_num afec_ch)
595 enum afec_channel_num afec_ch)
611 enum afec_channel_num afec_ch, uint16_t aoffset)
644 enum afec_interrupt_source interrupt_source);
647 enum afec_interrupt_source interrupt_source);
681 static inline Pdc *afec_get_pdc_base(
Afec *
const afec)
683 Pdc *p_pdc_base =
NULL;
687 p_pdc_base = PDC_AFEC0;
692 else if (afec ==
AFEC1) {
693 p_pdc_base = PDC_AFEC1;
709 static inline enum status_code afec_start_calibration(
Afec *
const afec)
714 afec->AFEC_CDOR = reg;
719 afec->
AFEC_CR = AFEC_CR_AUTOCAL;
__IO uint32_t AFEC_COCR
(Afec Offset: 0x6C) AFEC Channel Offset Compensation Register
#define AFEC_MR_TRGSEL_AFEC_TRIG5
(AFEC_MR) PWM0 event line 1 for AFEC0 / PWM1 event line 1 for AFEC1
void afec_set_power_mode(Afec *const afec, const enum afec_power_mode mode)
Configure AFEC power mode.
#define AFEC_MR_FREERUN
(AFEC_MR) Free Run Mode
#define UNUSED(v)
Marking v as a unused parameter or value.
#define AFEC_EMR_RES_Msk
(AFEC_EMR) Resolution
static uint32_t afec_channel_get_status(Afec *const afec, const enum afec_channel_num afec_ch)
Get the AFEC channel status.
__I uint32_t AFEC_LCDR
(Afec Offset: 0x20) AFEC Last Converted Data Register
#define AFEC_MR_TRGSEL_AFEC_TRIG1
(AFEC_MR) TIOA Output of the Timer Counter Channel 0 for AFEC0/TIOA Output of the Timer Counter Chann...
void afec_temp_sensor_get_config_defaults(struct afec_temp_sensor_config *const cfg)
Get the AFEC Temperature Sensor default configurations.
#define AFEC_MR_STARTUP_SUT960
(AFEC_MR) 960 periods of AFE clock
void afec_ch_get_config_defaults(struct afec_ch_config *const cfg)
Get the AFEC channel default configurations.
enum afec_resolution resolution
#define AFEC_TEMPMR_TEMPCMPMOD_OUT
(AFEC_TEMPMR) Generates an event when the converted data is out of the comparison window...
#define AFEC_TEMPMR_TEMPCMPMOD_LOW
(AFEC_TEMPMR) Generates an event when the converted data is lower than the low threshold of the windo...
static void afec_set_resolution(Afec *const afec, const enum afec_resolution res)
Configure conversion resolution.
#define AFEC_EMR_CMPMODE_LOW
(AFEC_EMR) Generates an event when the converted data is lower than the low threshold of the window...
Analog-Front-End Controller configuration structure.
__I uint32_t AFEC_IMR
(Afec Offset: 0x2C) AFEC Interrupt Mask Register
void afec_temp_sensor_set_config(Afec *const afec, struct afec_temp_sensor_config *config)
Configure the AFEC temperature sensor.
__IO uint32_t AFEC_WPMR
(Afec Offset: 0xE4) AFEC Write Protection Mode Register
#define AFEC1
(AFEC1 ) Base Address
enum afec_startup_time startup_time
#define AFEC_MR_STARTUP_SUT16
(AFEC_MR) 16 periods of AFE clock
static uint32_t afec_get_writeprotect_status(Afec *const afec)
Indicate write protect status.
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.
#define AFEC_MR_STARTUP_SUT832
(AFEC_MR) 832 periods of AFE clock
#define AFEC_MR_TRGSEL_AFEC_TRIG3
(AFEC_MR) TIOA Output of the Timer Counter Channel 2 for AFEC0/TIOA Output of the Timer Counter Chann...
#define AFEC_MR_STARTUP_SUT96
(AFEC_MR) 96 periods of AFE clock
__O uint32_t AFEC_CHDR
(Afec Offset: 0x18) AFEC Channel Disable Register
void afec_enable(Afec *const afec)
Enable AFEC Module.
#define AFEC_MR_TRGSEL_AFEC_TRIG2
(AFEC_MR) TIOA Output of the Timer Counter Channel 1 for AFEC0/TIOA Output of the Timer Counter Chann...
__I uint32_t AFEC_OVER
(Afec Offset: 0x4C) AFEC Overrun Status Register
static void afec_channel_enable(Afec *const afec, const enum afec_channel_num afec_ch)
Enable the specified AFEC channel.
#define AFEC_MR_STARTUP_SUT640
(AFEC_MR) 640 periods of AFE clock
#define AFEC_EMR_RES_NO_AVERAGE
(AFEC_EMR) 12-bit resolution, AFE sample rate is maximum (no averaging).
#define AFEC_WPSR_WPVS
(AFEC_WPSR) Write Protect Violation Status
static void afec_set_writeprotect(Afec *const afec, const bool is_enable)
Enable or disable write protection of AFEC registers.
static void afec_start_software_conversion(Afec *const afec)
Start analog-to-digital conversion.
#define AFEC_EMR_CMPMODE_HIGH
(AFEC_EMR) Generates an event when the converted data is higher than the high threshold of the window...
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.
#define AFEC_EMR_RES_OSR4
(AFEC_EMR) 13-bit resolution, AFE sample rate divided by 4 (averaging).
Commonly used includes, types and macros.
#define AFEC_TEMPMR_TEMPCMPMOD_HIGH
(AFEC_TEMPMR) Generates an event when the converted data is higher than the high threshold of the win...
__O uint32_t AFEC_CHER
(Afec Offset: 0x14) AFEC Channel Enable Register
#define AFEC_MR_TRGSEL_AFEC_TRIG6
(AFEC_MR) Analog Comparator
#define AFEC_MR_FREERUN_ON
(AFEC_MR) Free Run mode: Never wait for any trigger.
void afec_configure_sequence(Afec *const afec, const enum afec_channel_num ch_list[], const uint8_t uc_num)
Configure conversion sequence.
__IO uint32_t AFEC_EMR
(Afec Offset: 0x08) AFEC Extended Mode Register
void afec_get_config_defaults(struct afec_config *const cfg)
Get the AFEC default configurations.
#define AFEC_MR_TRGEN_DIS
(AFEC_MR) Hardware triggers are disabled. Starting a conversion is only possible by software...
void afec_disable(Afec *const afec)
Disable AFEC Module.
static void afec_channel_disable(Afec *const afec, const enum afec_channel_num afec_ch)
Disable the specified AFEC channel.
#define AFEC_EMR_CMPMODE_IN
(AFEC_EMR) Generates an event when the converted data is in the comparison window.
#define AFEC_MR_TRGSEL_AFEC_TRIG4
(AFEC_MR) PWM0 event line 0 for AFEC0 / PWM1 event line 0 for AFEC1
static uint32_t afec_get_interrupt_status(Afec *const afec)
Get AFEC interrupt status.
#define AFEC_EMR_RES_OSR256
(AFEC_EMR) 16-bit resolution, AFE sample rate divided by 256 (averaging).
#define AFEC_EMR_CMPMODE_Msk
(AFEC_EMR) Comparison Mode
#define AFEC_MR_STARTUP_SUT896
(AFEC_MR) 896 periods of AFE clock
#define AFEC_WPSR_WPVSRC_Pos
#define AFEC_LCDR_CHNB_Msk
(AFEC_LCDR) Channel Number
void afec_enable_interrupt(Afec *const afec, enum afec_interrupt_source interrupt_source)
Enable AFEC interrupts.
void afec_disable_interrupt(Afec *const afec, enum afec_interrupt_source interrupt_source)
Disable AFEC interrupts.
#define AFEC_MR_STARTUP_SUT512
(AFEC_MR) 512 periods of AFE clock
#define AFEC_TEMPMR_TEMPCMPMOD_IN
(AFEC_TEMPMR) Generates an event when the converted data is in the comparison window.
__I uint32_t AFEC_ISR
(Afec Offset: 0x30) AFEC Interrupt Status Register
#define AFEC_MR_TRGEN
(AFEC_MR) Trigger Enable
static uint32_t afec_get_overrun_status(Afec *const afec)
Get AFEC overrun error status.
#define AFEC_WPSR_WPVSRC_Msk
(AFEC_WPSR) Write Protect Violation Source
void(* afec_callback_t)(void)
void afec_set_callback(Afec *const afec, enum afec_interrupt_source source, afec_callback_t callback, uint8_t irq_level)
Set callback for AFEC.
__I uint32_t AFEC_WPSR
(Afec Offset: 0xE8) AFEC Write Protection Status Register
#define AFEC_MR_STARTUP_SUT8
(AFEC_MR) 8 periods of AFE clock
static uint32_t afec_channel_get_value(Afec *const afec, enum afec_channel_num afec_ch)
Read the Converted Data of the selected channel.
#define AFEC_EMR_CMPMODE_OUT
(AFEC_EMR) Generates an event when the converted data is out of the comparison window.
#define AFEC_MR_STARTUP_SUT112
(AFEC_MR) 112 periods of AFE clock
#define AFEC_EMR_RES_OSR64
(AFEC_EMR) 15-bit resolution, AFE sample rate divided by 64 (averaging).
#define AFEC_MR_TRGSEL_Msk
(AFEC_MR) Trigger Selection
static void afec_set_trigger(Afec *const afec, const enum afec_trigger trigger)
Configure conversion trigger and free run mode.
#define AFEC_WPMR_WPKEY_PASSWD
(AFEC_WPMR) Writing any other value in this field aborts the write operation of the WPEN bit...
#define AFEC_MR_STARTUP_SUT24
(AFEC_MR) 24 periods of AFE clock
#define AFEC_LCDR_CHNB_Pos
__IO uint32_t AFEC_CWR
(Afec Offset: 0x50) AFEC Compare Window Register
#define AFEC_CWR_LOWTHRES(value)
__I uint32_t AFEC_CHSR
(Afec Offset: 0x1C) AFEC Channel Status Register
static enum afec_cmp_mode afec_get_comparison_mode(Afec *const afec)
Get comparison mode.
static uint32_t afec_get_latest_chan_num(Afec *const afec)
Get the Last Converted Channel Number.
#define AFEC0
(AFEC0 ) Base Address
enum status_code afec_init(Afec *const afec, struct afec_config *const config)
Initialize the AFEC Module.
#define AFEC_COCR_AOFF_Msk
(AFEC_COCR) Analog Offset
#define AFEC_MR_STARTUP_SUT64
(AFEC_MR) 64 periods of AFE clock
#define AFEC_CWR_HIGHTHRES(value)
#define AFEC_MR_STARTUP_SUT0
(AFEC_MR) 0 periods of AFE clock
#define AFEC_MR_STARTUP_SUT704
(AFEC_MR) 704 periods of AFE clock
#define AFEC_CR_START
(AFEC_CR) Start Conversion
#define AFEC_EMR_RES_OSR16
(AFEC_EMR) 14-bit resolution, AFE sample rate divided by 16 (averaging).
static void afec_ch_sanity_check(Afec *const afec, const enum afec_channel_num channel)
#define AFEC_MR_STARTUP_SUT576
(AFEC_MR) 576 periods of AFE clock
#define AFEC_LCDR_LDATA_Msk
(AFEC_LCDR) Last Data Converted
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.
__IO uint32_t AFEC_MR
(Afec Offset: 0x04) AFEC Mode Register
#define AFEC_MR_STARTUP_SUT768
(AFEC_MR) 768 periods of AFE clock
#define Assert(expr)
This macro is used to test fatal errors.
__O uint32_t AFEC_CR
(Afec Offset: 0x00) AFEC Control Register
__IO uint32_t AFEC_CSELR
(Afec Offset: 0x64) AFEC Channel Selection Register
#define AFEC_MR_STARTUP_SUT80
(AFEC_MR) 80 periods of AFE clock
static uint32_t afec_get_interrupt_mask(Afec *const afec)
Get AFEC interrupt mask.
#define AFEC_MR_TRGSEL_AFEC_TRIG0
(AFEC_MR) AFE0_ADTRG for AFEC0 / AFE1_ADTRG for AFEC1
static uint32_t afec_get_latest_value(Afec *const afec)
Get the Last Data Converted.
void afec_ch_set_config(Afec *const afec, const enum afec_channel_num channel, struct afec_ch_config *config)
Configure the AFEC channel.
__I uint32_t AFEC_CDR
(Afec Offset: 0x68) AFEC Channel Data Register
#define AFEC_WPMR_WPEN
(AFEC_WPMR) Write Protection Enable