/**
******************************************************************************************************************************************************************************************
*@file                       user_gpadc.c
*@author         
*@data                       
*@version                    v1.0
*@par Copyright(c):
*
*@par History:
*     version:author,date,desc
*     v1.0   :create this file
*
*******************************************************************************************************************************************************************************************
*/
#include <stdint.h>
#include <stdlib.h>
#include "cpu_gpadc.h"
#include "user_gpadc.h"
#include "app_trace.h"
#include "cpu_deepsleep.h"

#define	SLOPE					1.115841825
#define	STANDARD_ERROR			6.135541843
#define	READNUM					30
#define GPADC_EQUATION(X)		SLOPE * X - STANDARD_ERROR;

typedef struct
{
	float			   gpadc_x;
    uint16_t           gpadc_value;
	uint32_t		   gpadc_total_value;
	float			   gpadc_avg_value;
	float			   gpadc_voltage_value;
} gpadc_value_t;

typedef struct
{
	gpadc_value_t 				gpadc_value_ev;
    gpadc_mode_t        		gpadc_ev_mode;
	gpadc_en_t					gpadc_ev_en;
	gpio_pin		    		apadc_ev_ping;
} gpadc_ev_t;
gpadc_ev_t gpadc_ev;

void user_gpadc_init(gpio_pin gpio_ping, gpadc_mode_t gpadc_mode, gpadc_en_t gpadc_en)
{
	gpadc_ev.apadc_ev_ping = gpio_ping;
	gpadc_ev.gpadc_ev_mode = gpadc_mode;
	gpadc_ev.gpadc_ev_en = gpadc_en;
	if(gpadc_ev.gpadc_ev_mode == GPADC_MODE_GPIO)
	{
		gpio_remap_config(gpadc_ev.apadc_ev_ping,GPIO_Remap_ADC);
	}
	gpadc_configuration();
}

void gpadc_configuration(void)
{
	gpadc_init_type_def gpadc_init_structure;

	memset(&gpadc_init_structure, 0, sizeof(gpadc_init_structure));   //GPADC初始化清0操作
	gpadc_init_structure.gapdc_ch_sel 	  	= gpadc_ev.gpadc_ev_mode; 	   //GPADC选择模式
	gpadc_init_structure.gpadc_in_mode 	   	= GPADC_SINGLE_IN;	   //设置GPIO为单端输入信号
	gpadc_init_structure.gpadc_gain		   	= GPADC_Gain_1;		   //采集电压内部增益设置
	gpadc_init_structure.gpadc_sel1		   	= gpadc_ev.apadc_ev_ping;
	gpadc_init_structure.gpadc_sel2         = gpadc_ev.apadc_ev_ping;
	gpadc_init_structure.gpadc_vcm_sel	   	= GPADC_VCM_055; 	   //设置VCM为0.55
	gpadc_init_structure.gpadc_sample_rate   = GPADC_SAMPLERATE_8k;  //设置采样率为8k
	gpadc_init_structure.gpadc_write_in_mode  = GPADC_MODE_MANY;	   //设置模式为多次转换数据写入gpadc_data
	gpadc_init_structure.gpadc_en 		  	= gpadc_ev.gpadc_ev_en;		   //使能GPADC
	gpadc_init(&gpadc_init_structure);
	//dbg_printf("GPADC_CH_SEL1(gpadc_ev.apadc_ev_ping) = %x\n",GPADC_CH_SEL1(gpadc_ev.apadc_ev_ping));
}

float get_voltage_value(void)
{
	//hwadjust_get_temperature_adc_data_period_stop();
	gpadc_configuration();
	for(uint8_t i=0; i<100; i++);
	cpu_prevent_sleep_set(GPADC_ONGOING);
	/*get adc value*/
	gpadc_ev.gpadc_value_ev.gpadc_total_value = 0;
	for(uint8_t j = 0; j< READNUM; j++)
	{
		gpadc_ev.gpadc_value_ev.gpadc_value = gpadc_receive_data();
		gpadc_ev.gpadc_value_ev.gpadc_total_value += gpadc_ev.gpadc_value_ev.gpadc_value;
	}
	gpadc_ev.gpadc_value_ev.gpadc_avg_value = gpadc_ev.gpadc_value_ev.gpadc_total_value / READNUM;
	gpadc_ev.gpadc_value_ev.gpadc_voltage_value = GPADC_EQUATION(gpadc_ev.gpadc_value_ev.gpadc_avg_value);
	//hwadjust_get_temperature_adc_data_period_start(NULL,NULL);
	cpu_prevent_sleep_clear(GPADC_ONGOING);
//	dbg_printf("gpadc_value_ev.gpadc_avg_value = %f\r\n",gpadc_ev.gpadc_value_ev.gpadc_avg_value);
//	dbg_printf("gpadc_value_ev.gpadc_voltage_value = %fmv\r\n",gpadc_ev.gpadc_value_ev.gpadc_voltage_value);
	return gpadc_ev.gpadc_value_ev.gpadc_voltage_value;
}

