#include "common.h" 
#include "drv_adc.h"


int8_t Sample_ADC_Value(uint8_t channel,uint16_t *ADC1ConvertedValue)
{

	    /* reset ADC */
    adc_deinit(ADC0);
    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE);

	adc_special_function_config(ADC0,ADC_SCAN_MODE,ENABLE);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
 
    /* ADC regular channel config */
    adc_regular_channel_config(ADC0, 0, channel, ADC_SAMPLETIME_55POINT5);
  

    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
    
    /* ADC discontinuous mode */
    //adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 3);

    /* enable ADC interface */
    adc_enable(ADC0);
    delay_1ms(1);
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);
	
	adc_software_trigger_enable(ADC0,ADC_REGULAR_CHANNEL);
    while(SET != adc_flag_get(ADC0,ADC_FLAG_EOC));
    *ADC1ConvertedValue = adc_regular_data_read(ADC0);
	adc_flag_clear(ADC0,ADC_FLAG_EOC);
	//printf("adc:%d\r\n",*ADC1ConvertedValue);
	return 1;
}


int8_t adc_getResult_6t(unsigned char chNo, unsigned short* result)
{
	unsigned char i;
	unsigned short Vsample[ADC_GETRESULT_6TIMES];
	unsigned short Vaverage;
	unsigned char s_min, s_max;
	int ret;


	if(result==(void*)0)
	{
		return(ADC_R_ERR_NULL);
	}

	for(i=0u;i<ADC_GETRESULT_6TIMES;i++)
	{
		ret=Sample_ADC_Value(chNo, &Vsample[i]);
		if(ret <= 0)
		{
			return ADC_R_ERR_CHNUM;
		}
	}
	
	s_min = 0u;
	s_max = 0u;
	for(i=1u;i<ADC_GETRESULT_6TIMES;i++)
	{
		if(Vsample[s_min] > Vsample[i]){
			s_min = i;
		}
		if(Vsample[s_max] < Vsample[i]){
			s_max = i;
		}
	}

	if(s_min == s_max)
	{
		*result = Vsample[0];
		return(1);
	}
	else
	{
		Vaverage = 0u;
		for(i=0u;i<ADC_GETRESULT_6TIMES;i++)
			{
			if((i != s_min) &&(i != s_max))
				{
				Vaverage += (Vsample[i] >> 2);
			}
		}
	}
	*result = Vaverage;
	
	return (1);
}


//**************************************************Measure_Temprature********************************************************

void _Measure_Temprature_Initial(void)
{
	rcu_periph_clock_enable(RCU_GPIOC);
	gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_3);

	/* enable GPIOA clock */
    rcu_periph_clock_enable(RCU_GPIOA);
    /* enable ADC0 clock */
    rcu_periph_clock_enable(RCU_ADC0);
    /* config ADC clock */
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);

	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1);

}


//**************************************************vbat********************************************************
void _Measure_Vol_Initial(void)
{
	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
}

/*void test_adc(void)
{
	uint16_t ADC1ConvertedValue;
	
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	//while(ADC_GetSoftwareStartConvStatus(ADC1));
	while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
    	ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
	ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
}
*/

