Ejemplo n.º 1
0
/**
 * \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 Test AFEC in TC trigger mode,
 *        which also tests interrupt driven conversions.
 *
 * \param test Current test case.
 */
static void run_afec_tc_trig_test(const struct test_case *test)
{
	configure_tc_trigger();

	afec_set_callback(AFEC0, AFEC_INTERRUPT_DATA_READY,
			afec_set_data_ready_flag, 1);
	delay_ms(2000);

	test_assert_true(test, is_data_ready == true,
			"AFEC using TC trigger test failed");
}
/**
 * \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");
}
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);
	
}
Ejemplo n.º 5
0
/*
*	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);
}
Ejemplo n.º 6
0
/**
 * \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);
		}
	}
}
Ejemplo n.º 7
0
void tempAdcRead(void)
{
	struct afec_config afec_cfg;
	struct afec_ch_config afec_ch_cfg;

	delayCNT = 1000;

	afec_enable(AFEC0);
	afec_enable(AFEC1);

	afec_get_config_defaults(&afec_cfg);
	afec_ch_get_config_defaults(&afec_ch_cfg);

	afec_init(AFEC0, &afec_cfg);
	afec_init(AFEC1, &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_ch_set_config(AFEC0, AFEC_CHANNEL_2, &afec_ch_cfg);
	afec_ch_set_config(AFEC0, AFEC_CHANNEL_6, &afec_ch_cfg);
	afec_ch_set_config(AFEC0, AFEC_CHANNEL_14, &afec_ch_cfg);
	afec_ch_set_config(AFEC0, AFEC_TEMPERATURE_SENSOR, &afec_ch_cfg);
	afec_ch_set_config(AFEC1, AFEC_CHANNEL_0, &afec_ch_cfg);
	afec_ch_set_config(AFEC1, AFEC_CHANNEL_1, &afec_ch_cfg);
	afec_ch_set_config(AFEC1, AFEC_CHANNEL_2, &afec_ch_cfg);

	afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_0, 0x800);
	afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_1, 0x800);
	afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_2, 0x800);
	afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_6, 0x800);
	afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_14, 0x800);
	afec_channel_set_analog_offset(AFEC0, AFEC_TEMPERATURE_SENSOR, 0x800);
	afec_channel_set_analog_offset(AFEC1, AFEC_CHANNEL_0, 0x800);
	afec_channel_set_analog_offset(AFEC1, AFEC_CHANNEL_1, 0x800);
	afec_channel_set_analog_offset(AFEC1, AFEC_CHANNEL_2, 0x800);
	
	afec_channel_enable(AFEC0, AFEC_CHANNEL_0);
	afec_channel_enable(AFEC0, AFEC_CHANNEL_1);
	afec_channel_enable(AFEC0, AFEC_CHANNEL_2);
	afec_channel_enable(AFEC0, AFEC_CHANNEL_6);
	afec_channel_enable(AFEC0, AFEC_CHANNEL_14);
	afec_channel_enable(AFEC0, AFEC_TEMPERATURE_SENSOR);
	afec_channel_enable(AFEC1, AFEC_CHANNEL_0);
	afec_channel_enable(AFEC1, AFEC_CHANNEL_1);
	afec_channel_enable(AFEC1, AFEC_CHANNEL_2);

	afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_0, afec0_temp_adcRead_ch0, 1);
	afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_1, afec0_temp_adcRead_ch1, 1);
	afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_2, afec0_temp_adcRead_ch2, 1);
	afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_6, afec0_temp_adcRead_ch6, 1);
	afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_14, afec0_temp_adcRead_ch14, 1);	
	afec_set_callback(AFEC0, AFEC_INTERRUPT_EOC_15, afec0_temp_adcRead_tempSensor, 1);
	afec_set_callback(AFEC1, AFEC_INTERRUPT_EOC_0, afec1_temp_adcRead_ch0, 1);
	afec_set_callback(AFEC1, AFEC_INTERRUPT_EOC_1, afec1_temp_adcRead_ch1, 1);
	afec_set_callback(AFEC1, AFEC_INTERRUPT_EOC_2, afec1_temp_adcRead_ch2, 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);

}
Ejemplo n.º 8
0
/**
 * \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;
	}
}