// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// File name    : adc_drv.h
// Version      : V0.1
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


#ifndef __ADC_DRV_H__
#define __ADC_DRV_H__

#include "type.h"
#include "adc_reg.h"
#include "memmap.h"


#define ADC_CH0	    	0
#define ADC_CH1	    	8
#define ADC_CH2	    	1
#define ADC_CH9	    	9 		//通道9直接连接内部1.1V基准点压

/*
typedef enum
{
    ADC_CH0=0,
    ADC_CH1=8,
    ADC_CH2=1,
    ADC_CH9=9,

}ADC_CH;
*/


#define ADC_DIS     0
#define ADC_EN      1

/*ADC configuration register 1:ADC_CFGR1*/
#define ADC_ANALOG_INPUT_DIFF_MASK                       ((uint32_t)(1<<31))
#define ADC_OVERRUN_MODE_MASK                            ((uint32_t)(1<<30))
#define ADC_QADC_CLK_DISABLE_MASK                        ((uint32_t)(1<<29))
#define ADC_SAMPLE_OUTPUT_BUFFER_EN                      ((uint32_t)(1<<28))
#define ADC_DISCONTINUOUS_EN                             ((uint32_t)(1<<23)) 
#define ADC_AUTO_OFF_MODE_EN                             ((uint32_t)(1<<22))
#define ADC_WAIT_CONVERSION_MODE_MASK                    ((uint32_t)(1<<21))
#define ADC_CONTINUOUS_CONVERSION_MASK                   ((uint32_t)(1<<20))
#define ADC_EXTERNAL_VREF_MASK                           ((uint32_t)(1<<15))
#define ADC_CFGR1_ALIGN_LEFT_ALIGNMENT                   ((uint32_t)(1<<10))
#define ADC_CFGR1_ALIGN_RIGHT_ALIGNMENT                  ((uint32_t)(0<<10))
#define ADC_LEFT_ALIGNMENT_MASK                          ((uint32_t)(1<<10))
#define ADC_CFGR1_RES_10BIT                              ((uint32_t)(1<<8))
#define ADC_CFGR1_DMATH_2                                ((uint32_t)(2<<4))
#define ADC_DMA_ACCESS_EN                                ((uint32_t)(1<<0))
#define ADC_CFGR1_SEQ_LEN_SHIFT                          ((uint32_t)(24))

typedef struct
{
	uint8_t channel;    /*< ADC通道号，参照ADC_ChannelTypeDef定义 */
	uint8_t resolution; /*< ADC采样精度设置，参照ADC_ResTypeDef定义 */
	uint8_t clk_div;    /*< ADC时钟分频 - QCLK = SYS_CLK/clk_div */
	uint8_t stab_time;  /*< ADC转化开始稳定时间约2us - 2*(10^-6)*(1/QCLK) */
	uint8_t smp_time;   /*< ADC采样时间 - (smp_time + 2)*QCLKs */

	uint8_t vref;     /*< ADC基准电压源选择，参照ADC_VrefTypeDef定义 */
	uint8_t align;    /*< ADC采样值对其方式，参照ADC_AlignTypeDef定义 */
	uint8_t conv;     /*< ADC采样转换模式，参照ADC_ConvModeTypeDef定义 */
	uint8_t overrun;  /*< ADC采样转换模式，参照ADC_OverrunManagTypeDef定义 */
	uint8_t int_kind; /*< ADC采样中断类型选择 */
} ADC_InitTypeDef;

typedef enum
{
	ADC_CLK_DIV_1 = 0,
	ADC_CLK_DIV_2,
	ADC_CLK_DIV_3,
	ADC_CLK_DIV_4,
	ADC_CLK_DIV_5,
	ADC_CLK_DIV_6,
	ADC_CLK_DIV_7,
	ADC_CLK_DIV_8,
	ADC_CLK_DIV_9,
	ADC_CLK_DIV_10,
	ADC_CLK_DIV_11,
	ADC_CLK_DIV_12,
	ADC_CLK_DIV_13,
	ADC_CLK_DIV_14,
	ADC_CLK_DIV_15,
	ADC_CLK_DIV_16
} ADC_ClkDivTypeDef;


#define ADC		((ADC_TypeDef *)(ADC_BASE_ADDR))

typedef enum
{
	ADC_RESOLUTION_12BIT = 0,
	ADC_RESOLUTION_10BIT = 1,
	ADC_RESOLUTION_8BIT  = 2,
	ADC_RESOLUTION_6BIT  = 3
} ADC_ResolutionTypeDef;


#define _reg_write(reg, val)    					((reg) = (val))
#define _reg_read(reg)          					((reg))
#define _bit_set(value, bit)    					((value) |=  (bit))
#define _bit_clr(value, bit)    					((value) &= ~(bit))

#define _reg_modify(reg, clearmask, setmask)  _reg_write((reg), (((_reg_read(reg)) & (clearmask)) | (setmask)))


 /*******************************************************************************
 * Function Name  : ADC_Cmd
 * Description    : Enable or Disable ADC
 * Input          :  - en：ADC_DIS----disable ADC   ADC_EN----enable ADC
 *
 * Output         : None
 * Return         : None
 ******************************************************************************/
 extern void ADC_Cmd(BOOL en);


 /*******************************************************************************
 * Function Name  : ADC_StartConv
 * Description    : ADC 转换启动
 * Input          : None
 *
 * Output         : None
 * Return         : None
 ******************************************************************************/

 extern void ADC_StartConv(void);

 /*******************************************************************************
 * Function Name  : ADC_StopConv
 * Description    : ADC 转换停止
 * Input          : None
 *
 * Output         : None
 * Return         : None
 ******************************************************************************/
 extern void ADC_StopConv(void);
 
/*******************************************************************************
* Function Name  : ADC_Get_Battery
* Description    : 采集锂电池电量
* Input          : - ADC_CHx：ADC采用通道  对应芯片引脚 ADC_INx (其中x取值0~7)
*
* Output         : None
* Return         : None
******************************************************************************/
extern unsigned int ADC_Get_Battery(unsigned char ADC_CHx);


extern void ADC_Init(ADC_TypeDef *ADCx,u8 channel);
extern uint16_t ADC_GetValue(ADC_TypeDef *ADCx,u8 channel);

#endif /* __ADC_DRV_H__ */
