54 #define NUM_OF_AFEC (2UL) 56 #define NUM_OF_AFEC (1UL) 60 #if defined __SAM4E8C__ || defined __SAM4E16C__ || defined __SAM4E8CB__ || defined __SAM4E16CB__ 61 #define AFEC_INTERRUPT_GAP1 (17UL) 62 #elif defined __SAM4E8E__ || defined __SAM4E16E__ 63 #define AFEC_INTERRUPT_GAP1 (8UL) 64 #elif (SAMV71 || SAMV70 || SAME70 || SAMS70) 66 #define AFEC_INTERRUPT_GAP1 (12UL) 69 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 71 #define AFEC_INTERRUPT_GAP2 (3UL) 74 #define AFEC_INTERRUPT_GAP2 (1UL) 78 #define AFEC_SEQ1_CHANNEL_NUM (8UL) 80 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 82 #define AFEC_TEMP_INT_SOURCE_NUM (11UL) 85 #define AFEC_TEMP_INT_SOURCE_NUM (15UL) 124 #if defined(ID_AFEC1) 129 #if defined(ID_AFEC0) 153 (config->
anach ? AFEC_MR_ANACH_ALLOWED : 0) |
155 (config->settling_time) |
166 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 187 reg &= ~(0x1u << channel);
188 reg |= (config->
diff) ? (0x1u << channel) : 0;
192 reg &= ~(0x03u << (2 * channel));
193 reg |= (config->
gain) << (2 * channel);
248 #if !(SAMV71 || SAMV70 || SAME70 || SAMS70) 249 cfg->settling_time = AFEC_SETTLING_TIME_0;
330 for (i = 0; i < _AFEC_NUM_OF_INTERRUPT_SOURCE; i++){
335 for (i = 0; i < _AFEC_NUM_OF_INTERRUPT_SOURCE; i++){
353 const enum afec_channel_num channel, uint8_t cmp_filter)
355 if (channel != AFEC_CHANNEL_ALL) {
435 enum afec_interrupt_source interrupt_source)
437 if (interrupt_source == AFEC_INTERRUPT_ALL) {
438 afec->
AFEC_IER = AFEC_INTERRUPT_ALL;
442 if (interrupt_source < AFEC_INTERRUPT_DATA_READY) {
443 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 444 if (interrupt_source == AFEC_INTERRUPT_EOC_11) {
447 if (interrupt_source == AFEC_INTERRUPT_EOC_15) {
451 afec->
AFEC_IER = 1 << interrupt_source;
453 }
else if (interrupt_source < AFEC_INTERRUPT_TEMP_CHANGE) {
454 afec->
AFEC_IER = 1 << (interrupt_source + AFEC_INTERRUPT_GAP1);
456 afec->
AFEC_IER = 1 << (interrupt_source + AFEC_INTERRUPT_GAP1
468 enum afec_interrupt_source interrupt_source)
470 if (interrupt_source == AFEC_INTERRUPT_ALL) {
471 afec->
AFEC_IDR = AFEC_INTERRUPT_ALL;
475 if (interrupt_source < AFEC_INTERRUPT_DATA_READY) {
476 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 477 if (interrupt_source == AFEC_INTERRUPT_EOC_11) {
480 if (interrupt_source == AFEC_INTERRUPT_EOC_15) {
484 afec->
AFEC_IDR = 1 << interrupt_source;
486 }
else if (interrupt_source < AFEC_INTERRUPT_TEMP_CHANGE) {
487 afec->
AFEC_IDR = 1 << (interrupt_source + AFEC_INTERRUPT_GAP1);
489 afec->
AFEC_IDR = 1 << (interrupt_source + AFEC_INTERRUPT_GAP1
505 enum afec_interrupt_source source)
520 volatile uint32_t status;
521 uint32_t cnt, inst_num;
526 for (cnt = 0; cnt < _AFEC_NUM_OF_INTERRUPT_SOURCE; cnt++) {
527 if (cnt < AFEC_INTERRUPT_DATA_READY) {
528 #if defined __SAM4E8C__ || defined __SAM4E16C__ || defined __SAM4E8CB__ || defined __SAM4E16CB__ 529 if(cnt == AFEC_INTERRUPT_EOC_15) {
534 if (status & (1 << cnt)) {
538 #elif defined __SAM4E8E__ || defined __SAM4E16E__ || SAMV71 || SAMV70 || SAMS70 || SAME70 539 if (status & (1 << cnt)) {
543 }
else if (cnt < AFEC_INTERRUPT_TEMP_CHANGE) {
544 if (status & (1 << (cnt + AFEC_INTERRUPT_GAP1))) {
611 const enum afec_channel_num ch_list[], uint8_t uc_num)
621 for (uc_counter = 0; uc_counter < uc_num; uc_counter++) {
623 ch_list[uc_counter] << (4 * uc_counter);
628 ch_list[uc_counter] << (4 * uc_counter);
638 #if (SAMV71 || SAMV70 || SAME70 || SAMS70) 647 void afec_configure_auto_error_correction(
Afec *
const afec,
648 const enum afec_channel_num channel, int16_t offsetcorr, uint16_t gaincorr)
652 if (channel != AFEC_CHANNEL_ALL) {
658 reg = (channel == AFEC_CHANNEL_ALL)? 0 : ~(0x1u << channel);
659 reg |= (channel == AFEC_CHANNEL_ALL)? AFEC_CHANNEL_ALL : (0x1u << channel);
675 uint32_t afec_get_correction_value(
Afec *
const afec,
676 const enum afec_channel_num afec_ch)
678 uint32_t corrected_data = 0;
679 uint32_t converted_data = 0;
688 return corrected_data;
699 void afec_set_sample_hold_mode(
Afec *
const afec,
700 const enum afec_channel_num channel,
const enum afec_sample_hold_mode mode)
702 if (channel != AFEC_CHANNEL_ALL) {
708 if (mode == AFEC_SAMPLE_HOLD_MODE_1) {
710 reg |= (channel == AFEC_CHANNEL_ALL)? AFEC_CHANNEL_ALL : 0x1u << channel;
714 reg = (channel == AFEC_CHANNEL_ALL)? 0 : ~(0x1u << channel);
#define AFEC_TEMP_INT_SOURCE_NUM
#define ID_AFEC0
Analog Front End 0 (AFEC0)
void AFEC1_Handler(void)
Interrupt handler for AFEC1.
void afec_set_power_mode(Afec *const afec, const enum afec_power_mode mode)
Configure AFEC power mode.
#define AFEC_MR_TRACKTIM(value)
#define ID_AFEC1
Analog Front End 1 (AFEC1)
#define AFEC_TEMPCWR_TLOWTHRES(value)
static void afec_interrupt(uint8_t inst_num, enum afec_interrupt_source source)
#define AFEC_ISR_DRDY
(AFEC_ISR) Data Ready (cleared by reading AFEC_LCDR)
void afec_temp_sensor_get_config_defaults(struct afec_temp_sensor_config *const cfg)
Get the AFEC Temperature Sensor default configurations.
void afec_ch_get_config_defaults(struct afec_ch_config *const cfg)
Get the AFEC channel default configurations.
enum afec_resolution resolution
#define AFEC_CVR_GAINCORR(value)
#define AFEC_ACR_PGA0EN
(AFEC_ACR) PGA0 Enable
#define irq_register_handler(int_num, int_prio)
Register handler for interrupt.
static uint32_t sysclk_get_cpu_hz(void)
Return the current rate in Hz of the CPU clock.
#define AFEC_SEQ1_CHANNEL_NUM
Analog-Front-End Controller configuration structure.
void afec_temp_sensor_set_config(Afec *const afec, struct afec_temp_sensor_config *config)
Configure the AFEC temperature sensor.
#define AFEC_MR_USEQ_REG_ORDER
(AFEC_MR) User Sequence mode: The sequence respects what is defined in AFEC_SEQ1R and AFEC_SEQ1R...
#define AFEC1
(AFEC1 ) Base Address
enum afec_startup_time startup_time
#define AFEC_MR_FWUP_ON
(AFEC_MR) Fast wake-up Sleep mode: The voltage reference is ON between conversions and AFE is OFF...
static uint32_t afec_find_inst_num(Afec *const afec)
#define AFEC_EMR_CMPALL
(AFEC_EMR) Compare All Channels
uint32_t pmc_disable_periph_clk(uint32_t ul_id)
Disable the specified peripheral clock.
#define AFEC_EMR_STM
(AFEC_EMR) Single Trigger Mode
#define AFEC_EMR_CMPFILTER_Msk
(AFEC_EMR) Compare Event Filtering
void afec_enable(Afec *const afec)
Enable AFEC Module.
__IO uint32_t AFEC_SEQ1R
(Afec Offset: 0x0C) AFEC Channel Sequence 1 Register
uint32_t pmc_enable_periph_clk(uint32_t ul_id)
Enable the specified peripheral clock.
__O uint32_t AFEC_IDR
(Afec Offset: 0x28) AFEC Interrupt Disable Register
#define AFEC_EMR_CMPSEL(value)
#define AFEC_MR_ONE
(AFEC_MR) One
__IO uint32_t AFEC_TEMPCWR
(Afec Offset: 0x74) AFEC Temperature Compare Window Register
static void afec_set_config(Afec *const afec, struct afec_config *config)
#define AFEC_CR_SWRST
(AFEC_CR) Software Reset
void afec_configure_sequence(Afec *const afec, const enum afec_channel_num ch_list[], uint8_t uc_num)
Configure conversion sequence.
__IO uint32_t AFEC_EMR
(Afec Offset: 0x08) AFEC Extended Mode Register
#define AFEC_MR_TRANSFER(value)
#define AFEC_TEMPMR_RTCT
(AFEC_TEMPMR) Temperature Sensor RTC Trigger Mode
#define AFEC_INTERRUPT_GAP2
__IO uint32_t AFEC_SEQ2R
(Afec Offset: 0x10) AFEC Channel Sequence 2 Register
#define AFEC_CVR_GAINCORR_Pos
void afec_get_config_defaults(struct afec_config *const cfg)
Get the AFEC default configurations.
#define AFEC_ACR_PGA1EN
(AFEC_ACR) PGA1 Enable
void afec_disable(Afec *const afec)
Disable AFEC Module.
#define AFEC_CVR_OFFSETCORR(value)
__IO uint32_t AFEC_TEMPMR
(Afec Offset: 0x70) AFEC Temperature Sensor Mode Register
#define AFEC_MR_SLEEP_SLEEP
(AFEC_MR) Sleep mode: The AFE and reference voltage circuitry are OFF between conversions.
__O uint32_t AFEC_IER
(Afec Offset: 0x24) AFEC Interrupt Enable Register
__IO uint32_t AFEC_CGR
(Afec Offset: 0x54) AFEC Channel Gain Register
Analog-Front-End Controller driver for SAM.
static uint32_t afec_get_interrupt_status(Afec *const afec)
Get AFEC interrupt status.
enum afec_temp_cmp_mode mode
__IO uint32_t AFEC_COSR
(Afec Offset: 0xD0) AFEC Correction Select Register
#define AFEC_EMR_CMPMODE_Msk
(AFEC_EMR) Comparison Mode
#define AFEC_EMR_TAG
(AFEC_EMR) TAG of the AFEC_LDCR
void afec_enable_interrupt(Afec *const afec, enum afec_interrupt_source interrupt_source)
Enable AFEC interrupts.
static uint32_t afec_find_pid(Afec *const afec)
void afec_disable_interrupt(Afec *const afec, enum afec_interrupt_source interrupt_source)
Disable AFEC interrupts.
__IO uint32_t AFEC_CVR
(Afec Offset: 0xD4) AFEC Correction Values Register
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.
static void afec_process_callback(Afec *const afec)
#define AFEC_TEMPCWR_THIGHTHRES(value)
__IO uint32_t AFEC_CECR
(Afec Offset: 0xD8) AFEC Channel Error Correction Register
#define AFEC_ACR_IBCTL(value)
#define AFEC_EMR_CMPSEL_Msk
(AFEC_EMR) Comparison Selected Channel
__IO uint32_t AFEC_SHMR
(Afec Offset: 0xA0) AFEC Sample & Hold Mode Register
#define AFEC0
(AFEC0 ) Base Address
#define AFEC_MR_PRESCAL(value)
enum status_code afec_init(Afec *const afec, struct afec_config *config)
Initialize the AFEC Module.
#define AFEC_COSR_CSEL
(AFEC_COSR) Sample & Hold unit Correction Select
#define AFEC_CVR_OFFSETCORR_Msk
(AFEC_CVR) Offset Correction
__IO uint32_t AFEC_DIFFR
(Afec Offset: 0x60) AFEC Channel Differential Register
static void sleepmgr_lock_mode(enum sleepmgr_mode mode)
Increase lock count for a sleep mode.
static void afec_ch_sanity_check(Afec *const afec, const enum afec_channel_num channel)
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 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
__IO uint32_t AFEC_ACR
(Afec Offset: 0x94) AFEC Analog Control Register
static void sleepmgr_unlock_mode(enum sleepmgr_mode mode)
Decrease lock count for a sleep mode.
static uint32_t afec_get_interrupt_mask(Afec *const afec)
Get AFEC interrupt mask.
#define AFEC_EMR_CMPFILTER(value)
void AFEC0_Handler(void)
Interrupt handler for AFEC0.
afec_callback_t afec_callback_pointer[NUM_OF_AFEC][_AFEC_NUM_OF_INTERRUPT_SOURCE]
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