// Module initialisation void AnalogInInit() { #if SAM3XA || SAM4S pmc_enable_periph_clk(ID_ADC); adc_init(ADC, SystemCoreClock, 2000000, ADC_STARTUP_TIME_12); // 2MHz clock adc_configure_timing(ADC, 3, ADC_SETTLING_TIME_3, 1); // Add transfer time adc_configure_trigger(ADC, ADC_TRIG_SW, 0); // Disable hardware trigger adc_disable_interrupt(ADC, 0xFFFFFFFF); // Disable all ADC interrupts adc_disable_all_channel(ADC); #elif SAM4E || SAME70 afec_enable(AFEC0); afec_enable(AFEC1); afec_config cfg; afec_get_config_defaults(&cfg); #if 0 // these are probably not needed, the defaults should be OK // cfg.afec_clock = 2000000UL; // reduce clock frequency // cfg.settling_time = AFEC_SETTLING_TIME_3; #endif while (afec_init(AFEC0, &cfg) != STATUS_OK) { (void)afec_get_latest_value(AFEC0); } while (afec_init(AFEC1, &cfg) != STATUS_OK) { (void)afec_get_latest_value(AFEC1); } afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_ALL); afec_disable_interrupt(AFEC1, AFEC_INTERRUPT_ALL); afec_set_trigger(AFEC0, AFEC_TRIG_SW); afec_set_trigger(AFEC1, AFEC_TRIG_SW); #endif }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { int32_t ul_vol; int32_t ul_temp; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); afec_enable(AFEC0); struct afec_config afec_cfg; afec_get_config_defaults(&afec_cfg); afec_init(AFEC0, &afec_cfg); afec_set_trigger(AFEC0, AFEC_TRIG_SW); struct afec_ch_config afec_ch_cfg; afec_ch_get_config_defaults(&afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_TEMPERATURE_SENSOR, &afec_ch_cfg); /* * Because the internal ADC offset is 0x800, it should cancel it and shift * down to 0. */ afec_channel_set_analog_offset(AFEC0, AFEC_TEMPERATURE_SENSOR, 0x800); struct afec_temp_sensor_config afec_temp_sensor_cfg; afec_temp_sensor_get_config_defaults(&afec_temp_sensor_cfg); afec_temp_sensor_cfg.rctc = true; afec_temp_sensor_set_config(AFEC0, &afec_temp_sensor_cfg); afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_15, afec_temp_sensor_end_conversion, 1); while (1) { if(is_conversion_done == true) { ul_vol = g_ul_value * VOLT_REF / MAX_DIGITAL; /* * According to datasheet, The output voltage VT = 1.44V at 27C * and the temperature slope dVT/dT = 4.7 mV/C */ ul_temp = (ul_vol - 1440) * 100 / 470 + 27; printf("Temperature is: %4d\r", (int)ul_temp); is_conversion_done = false; } } }
/** * \brief Configure to trigger AFEC by TIOA output of timer. */ static void configure_tc_trigger(void) { uint32_t ul_div = 0; uint32_t ul_tc_clks = 0; uint32_t ul_sysclk = sysclk_get_cpu_hz(); /* Enable peripheral clock. */ pmc_enable_periph_clk(ID_TC0); /* TIOA configuration */ ioport_set_pin_mode(PIN_TC0_TIOA0, PIN_TC0_TIOA0_FLAGS); ioport_disable_pin(PIN_TC0_TIOA0); /* Configure TC for a 10Hz frequency and trigger on RC compare. */ tc_find_mck_divisor(10, ul_sysclk, &ul_div, &ul_tc_clks, ul_sysclk); tc_init(TC0, 0, ul_tc_clks | TC_CMR_CPCTRG | TC_CMR_WAVE | TC_CMR_ACPA_CLEAR | TC_CMR_ACPC_SET); TC0->TC_CHANNEL[0].TC_RA = (ul_sysclk / ul_div) / 2; TC0->TC_CHANNEL[0].TC_RC = (ul_sysclk / ul_div) / 1; /* Start the Timer. */ tc_start(TC0, 0); afec_set_trigger(AFEC0, AFEC_TRIG_TIO_CH_0); }
/** * \brief Test AFEC in comparsion window mode. * * \param test Current test case. */ static void run_afec_comp_test(const struct test_case *test) { afec_set_trigger(AFEC0, AFEC_TRIG_SW); afec_set_comparison_mode(AFEC0, AFEC_CMP_MODE_2, AFEC_CHANNEL_1, 0); afec_set_comparison_window(AFEC0, 0, 0xFFF); afec_set_callback(AFEC0, AFEC_INTERRUPT_COMP_ERROR, afec_set_comp_flag, 1); afec_start_software_conversion(AFEC0); delay_ms(100); test_assert_true(test, is_comp_event_flag == true, "AFEC Comparsion Window test failed"); }
/** * \brief AFEC(ADC) configuration. */ static void dsp_configure_adc(void) { pmc_enable_periph_clk(ID_AFEC0); struct afec_config afec_cfg; struct afec_ch_config afec_ch_cfg; /* AFEC0 enable, init and configuration*/ afec_enable(AFEC0); afec_get_config_defaults(&afec_cfg); afec_init(AFEC0, &afec_cfg); /* Set to Software trigger*/ afec_set_trigger(AFEC0, AFEC_TRIG_SW); /* AFEC0 channals configuration*/ afec_channel_enable(AFEC0,ADC_CHANNEL_POTENTIOMETER); afec_ch_get_config_defaults(&afec_ch_cfg); afec_ch_set_config(AFEC0, ADC_CHANNEL_POTENTIOMETER,&afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_TEMPERATURE_SENSOR, &afec_ch_cfg); /* * Because the internal ADC offset is 0x800, it should cancel it and * shift down to 0. */ afec_channel_set_analog_offset(AFEC0, ADC_CHANNEL_POTENTIOMETER, 0x800); afec_channel_set_analog_offset(AFEC0, AFEC_TEMPERATURE_SENSOR, 0x800); afec_channel_enable(AFEC0,AFEC_TEMPERATURE_SENSOR); afec_channel_enable(AFEC0,ADC_CHANNEL_MICROPHONE); struct afec_temp_sensor_config afec_temp_sensor_cfg; afec_temp_sensor_get_config_defaults(&afec_temp_sensor_cfg); afec_temp_sensor_cfg.rctc = true; afec_temp_sensor_set_config(AFEC0, &afec_temp_sensor_cfg); /* Perform an auto cab on the ADC Channel 4. */ afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); /* Enable potentiometer channel, disable microphone. */ afec_channel_disable(AFEC0, ADC_CHANNEL_MICROPHONE); afec_channel_enable(AFEC0, AFEC_TEMPERATURE_SENSOR); afec_channel_enable(AFEC0, ADC_CHANNEL_MICROPHONE); /* Start the first conversion*/ afec_start_software_conversion(AFEC0); }
void init_adc(void) { struct afec_config afec_cfg; struct afec_ch_config afec_ch_cfg; afec_enable(AFEC0); afec_enable(AFEC1); afec_get_config_defaults(&afec_cfg); afec_cfg.resolution = AFEC_12_BITS; afec_init(AFEC0, &afec_cfg); afec_init(AFEC1, &afec_cfg); afec_ch_get_config_defaults(&afec_ch_cfg); afec_ch_cfg.gain = AFEC_GAINVALUE_3; afec_ch_set_config(AFEC1, AFEC_CHANNEL_9, &afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_CHANNEL_4, &afec_ch_cfg); afec_ch_set_config(AFEC1, AFEC_CHANNEL_4, &afec_ch_cfg); afec_ch_set_config(AFEC1, AFEC_CHANNEL_5, &afec_ch_cfg); afec_set_trigger(AFEC0, AFEC_TRIG_SW); afec_set_trigger(AFEC1, AFEC_TRIG_SW); afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec0_data_ready, 1); afec_set_callback(AFEC1, AFEC_INTERRUPT_DATA_READY, afec1_data_ready, 1); // Got this calibration code from a SAM4E example, supposedly code compatible with the SAME70, but this doesn't build // afec_start_calibration(AFEC0); // while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); // afec_start_calibration(AFEC1); // while((afec_get_interrupt_status(AFEC1) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); }
/* * Inialise the temp sensor * */ void temp_init(void) { afec_enable(AFEC0); struct afec_config afec_cfg; afec_get_config_defaults(&afec_cfg); afec_init(AFEC0, &afec_cfg); afec_set_trigger(AFEC0, AFEC_TRIG_SW); struct afec_ch_config afec_ch_cfg; afec_ch_get_config_defaults(&afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_TEMPERATURE_SENSOR, &afec_ch_cfg); afec_channel_set_analog_offset(AFEC0, AFEC_TEMPERATURE_SENSOR, 0x800); struct afec_temp_sensor_config afec_temp_sensor_cfg; afec_temp_sensor_get_config_defaults(&afec_temp_sensor_cfg); afec_temp_sensor_cfg.rctc = true; afec_temp_sensor_set_config(AFEC0, &afec_temp_sensor_cfg); afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_15, afec_temp_sensor_end_conversion, 1); }
/** * \brief ACC example application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint32_t uc_key; int16_t s_volt = 0; uint32_t ul_value = 0; volatile uint32_t ul_status = 0x0; int32_t l_volt_dac0 = 0; /* Initialize the system */ sysclk_init(); board_init(); /* Initialize debug console */ configure_console(); /* Output example information */ puts(STRING_HEADER); /* Initialize DACC */ /* Enable clock for DACC */ pmc_enable_periph_clk(ID_DACC); /* Reset DACC registers */ dacc_reset(DACC); /* External trigger mode disabled. DACC in free running mode. */ dacc_disable_trigger(DACC, DACC_CHANNEL_0); /* Half word transfer mode */ dacc_set_transfer_mode(DACC, 0); #if (SAM3S) || (SAM3XA) /* Power save: * sleep mode - 0 (disabled) * fast wakeup - 0 (disabled) */ dacc_set_power_save(DACC, 0, 0); #endif /* Enable output channel DACC_CHANNEL */ dacc_enable_channel(DACC, DACC_CHANNEL_0); /* Setup analog current */ dacc_set_analog_control(DACC, DACC_ANALOG_CONTROL); /* Set DAC0 output at ADVREF/2. The DAC formula is: * * (5/6 * VOLT_REF) - (1/6 * VOLT_REF) volt - (1/6 * VOLT_REF) * ----------------------------------- = -------------------------- * MAX_DIGITAL digit * * Here, digit = MAX_DIGITAL/2 */ dacc_write_conversion_data(DACC, MAX_DIGITAL / 2, DACC_CHANNEL_0); l_volt_dac0 = (MAX_DIGITAL / 2) * (2 * VOLT_REF / 3) / MAX_DIGITAL + VOLT_REF / 6; /* Enable clock for AFEC */ afec_enable(AFEC0); struct afec_config afec_cfg; afec_get_config_defaults(&afec_cfg); /* Initialize AFEC */ afec_init(AFEC0, &afec_cfg); struct afec_ch_config afec_ch_cfg; afec_ch_get_config_defaults(&afec_ch_cfg); afec_ch_cfg.gain = AFEC_GAINVALUE_0; afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg); /* * Because the internal ADC offset is 0x200, it should cancel it and shift * down to 0. */ afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x200); afec_set_trigger(AFEC0, AFEC_TRIG_SW); /* Enable channel for potentiometer. */ afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER); /* Enable clock for ACC */ pmc_enable_periph_clk(ID_ACC); /* Initialize ACC */ acc_init(ACC, ACC_MR_SELPLUS_AFE0_AD0, ACC_MR_SELMINUS_DAC0, ACC_MR_EDGETYP_ANY, ACC_MR_INV_DIS); /* Enable ACC interrupt */ NVIC_EnableIRQ(ACC_IRQn); /* Enable */ acc_enable_interrupt(ACC); dsplay_menu(); while (1) { while (usart_read(CONSOLE_UART, &uc_key)) { } printf("input: %c\r\n", uc_key); switch (uc_key) { case 's': case 'S': printf("Input DAC0 output voltage (%d~%d mv): ", (VOLT_REF / 6), (VOLT_REF * 5 / 6)); s_volt = get_input_voltage(); puts("\r"); if (s_volt > 0) { l_volt_dac0 = s_volt; /* The DAC formula is: * * (5/6 * VOLT_REF) - (1/6 * VOLT_REF) volt - (1/6 * VOLT_REF) * ----------------------------------- = -------------------------- * MAX_DIGITAL digit * */ ul_value = ((s_volt - (VOLT_REF / 6)) * (MAX_DIGITAL * 6) / 4) / VOLT_REF; dacc_write_conversion_data(DACC, ul_value, DACC_CHANNEL_0); puts("-I- Set ok\r"); } else { puts("-I- Input voltage is invalid\r"); } break; case 'v': case 'V': /* Start conversion */ afec_start_software_conversion(AFEC0); ul_status = afec_get_interrupt_status(AFEC0); while ((ul_status & AFEC_ISR_EOC0) != AFEC_ISR_EOC0) { ul_status = afec_get_interrupt_status(AFEC0); } /* Conversion is done */ ul_value = afec_channel_get_value(AFEC0, AFEC_CHANNEL_POTENTIOMETER); /* * Convert AFEC sample data to voltage value: * voltage value = (sample data / max. resolution) * reference voltage */ s_volt = (ul_value * VOLT_REF) / MAX_DIGITAL; printf("-I- Voltage on potentiometer(AD0) is %d mv\n\r", s_volt); printf("-I- Voltage on DAC0 is %ld mv \n\r", (long)l_volt_dac0); break; case 'm': case 'M': dsplay_menu(); break; } } }
/** * \brief Application entry point. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint8_t uc_key; /* Initialize the SAM system. */ sysclk_init(); board_init(); configure_console(); /* Output example information. */ puts(STRING_HEADER); g_afec_sample_data.value = 0; g_afec_sample_data.is_done = false; g_max_digital = MAX_DIGITAL_12_BIT; afec_enable(AFEC0); struct afec_config afec_cfg; afec_get_config_defaults(&afec_cfg); afec_init(AFEC0, &afec_cfg); struct afec_ch_config afec_ch_cfg; afec_ch_get_config_defaults(&afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg); /* * Because the internal ADC offset is 0x800, it should cancel it and shift * down to 0. */ afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x800); afec_set_trigger(AFEC0, AFEC_TRIG_SW); /* Enable channel for potentiometer. */ afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER); afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec_data_ready, 1); display_menu(); afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); while (1) { afec_start_software_conversion(AFEC0); delay_ms(1000); /* Check if AFEC sample is done. */ if (g_afec_sample_data.is_done == true) { printf("Potentiometer Voltage: %4d mv.", (int)(g_afec_sample_data.value * VOLT_REF / g_max_digital)); puts("\r"); g_afec_sample_data.is_done = false; } /* Check if the user enters a key. */ if (!uart_read(CONF_UART, &uc_key)) { /* Disable all afec interrupt. */ afec_disable_interrupt(AFEC0, AFEC_INTERRUPT_ALL); set_afec_resolution(); afec_enable_interrupt(AFEC0, AFEC_INTERRUPT_DATA_READY); } } }
/** * \brief Set AFEC test mode. */ static void set_afec_test(void) { uint8_t uc_key; struct afec_config afec_cfg; struct afec_ch_config afec_ch_cfg; display_menu(); afec_enable(AFEC0); afec_get_config_defaults(&afec_cfg); afec_ch_get_config_defaults(&afec_ch_cfg); while (uart_read(CONF_UART, &uc_key)); switch (uc_key) { case '0': /* * This test will use AFEC0 channel4 to connect with external input. * Setting gain = 4, if external input voltage is 100mv, * the ouput result should be 1650 + 100 * 4 = 2050mv . */ puts("Gain Test \n\r"); g_delay_cnt = 1000; afec_init(AFEC0, &afec_cfg); afec_ch_cfg.gain = AFEC_GAINVALUE_3; afec_ch_set_config(AFEC0, AFEC_CHANNEL_4, &afec_ch_cfg); afec_set_trigger(AFEC0, AFEC_TRIG_SW); afec_channel_enable(AFEC0, AFEC_CHANNEL_4); afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec0_data_ready, 1); afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); break; case '1': /* * This test will use AFEC0 channel5 to connect with potentiometer and * AFEC1 channel0 to connect with external input. The AFEC0 conversion * is triggered by software every 3s and AFEC1 conversion is triggered * by TC every 1s. */ puts("Dual AFEC Conversion Test \n\r"); g_delay_cnt = 3000; afec_enable(AFEC1); afec_init(AFEC0, &afec_cfg); afec_init(AFEC1, &afec_cfg); afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg); afec_ch_set_config(AFEC1, AFEC_CHANNEL_0, &afec_ch_cfg); /* * Because the internal AFEC offset is 0x800, it should cancel it and shift * down to 0. */ afec_channel_set_analog_offset(AFEC1, AFEC_CHANNEL_0, 0x800); afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x800); afec_set_trigger(AFEC0, AFEC_TRIG_SW); configure_tc_trigger(); afec_channel_enable(AFEC1, AFEC_CHANNEL_0); afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER); afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec0_data_ready, 1); afec_set_callback(AFEC1, AFEC_INTERRUPT_DATA_READY, afec1_data_ready, 1); afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); afec_start_calibration(AFEC1); while((afec_get_interrupt_status(AFEC1) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); break; case '2': /* * This test will use AFEC0 channl4 and channel5 as positive and * negative input, so the output result is external input voltage subtracting * potentiometer voltage. The differential voltage range is -1.65v~ +1.65v. */ puts("Differential Input Test \n\r"); g_delay_cnt = 1000; afec_init(AFEC0, &afec_cfg); afec_ch_cfg.diff = true; afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_CHANNEL_4, &afec_ch_cfg); afec_set_trigger(AFEC0, AFEC_TRIG_SW); afec_channel_enable(AFEC0, AFEC_CHANNEL_4); afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER); afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec0_diff_data_ready, 1); afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); break; case '3': /* * This test will configure user sequence channel1, channel0, * so the first conversion is is channel1 and next is channel0. * The output information will show this. */ puts("User Sequence Test \n\r"); g_delay_cnt = 1000; afec_init(AFEC0, &afec_cfg); afec_ch_set_config(AFEC0, AFEC_CHANNEL_0, &afec_ch_cfg); afec_ch_set_config(AFEC0, AFEC_CHANNEL_1, &afec_ch_cfg); afec_channel_enable(AFEC0, AFEC_CHANNEL_0); afec_channel_enable(AFEC0, AFEC_CHANNEL_1); afec_configure_sequence(AFEC0, ch_list, 2); afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_0, afec_eoc_0, 1); afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_1, afec_eoc_1, 1); afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); break; case '4': /* * This test use AFEC0 channel4 to connect with external input. * It integrate the enhanced resolution test and gain and offset test. */ puts("Typical Application Test \n\r"); g_delay_cnt = 1000; g_max_digital = MAX_DIGITAL_12_BIT * 16; afec_cfg.resolution = AFEC_16_BITS; afec_init(AFEC0, &afec_cfg); afec_ch_cfg.gain = AFEC_GAINVALUE_3; afec_ch_set_config(AFEC0, AFEC_CHANNEL_4, &afec_ch_cfg); afec_set_trigger(AFEC0, AFEC_TRIG_SW); afec_channel_enable(AFEC0, AFEC_CHANNEL_4); afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY, afec0_data_ready, 1); afec_start_calibration(AFEC0); while((afec_get_interrupt_status(AFEC0) & AFEC_ISR_EOCAL) != AFEC_ISR_EOCAL); break; default: puts("Please select feature test correctly! \n\r"); break; } }