/***********************************************************************
 * Copyright (c)  2017 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : adc.h
 * Description : adc header file
 * Author(s)   : Hackrtt
 * version     : V1.0
 * Modify date : 2022-07-10
 ***********************************************************************/
#ifndef _ADC_H_
#define _ADC_H_

#include  "um321xF.h"

#ifndef ENABLE
#define ENABLE	1
#endif

#ifndef DISABLE
#define DISABLE	0
#endif



/******ADC REGISTER BIT DESCRIPTION**********/

/******REG ADC_GCR********/											
#define ADC_RST										(1<<9)	//ADC复位
#define ADC_PD_ON									(1<<8)	//ADC上电

#define ADC_DMA_ACCESS_RXFIFO						1	//ADC DMA访问RXFIFO
#define ADC_CPU_ACCESS_RXFIFO						0	//ADC CPU访问RXFIFO
#define ADC_RXFIFO_LEAST_8DATA						1	//
#define ADC_RXFIFO_LEAST_1DATA						0	//



/************REG ADC_ISR****************/
#define ADC_IT_RX_FIFO_FULL							(1<<15)
#define ADC_IT_RX_FIFO_AVAILABLE					(1<<14)
#define ADC_IT_CH13_ACTIVE							(1<<13)
#define ADC_IT_CH12_ACTIVE							(1<<12)
#define ADC_IT_CH11_ACTIVE							(1<<11)
#define ADC_IT_CH10_ACTIVE							(1<<10)
#define ADC_IT_CH9_ACTIVE							(1<<9)
#define ADC_IT_CH8_ACTIVE							(1<<8)
#define ADC_IT_CH7_ACTIVE							(1<<7)
#define ADC_IT_CH6_ACTIVE							(1<<6)
#define ADC_IT_CH5_ACTIVE							(1<<5)
#define ADC_IT_CH4_ACTIVE							(1<<4)
#define ADC_IT_CH3_ACTIVE							(1<<3)
#define ADC_IT_CH2_ACTIVE							(1<<2)
#define ADC_IT_CH1_ACTIVE							(1<<1)
#define ADC_IT_CH0_ACTIVE							(1<<0)
															
/************REG ADC_IER****************/
#define ADC_IT_RX_FIFO_FULL_EN						(1<<15)
#define ADC_IT_RX_FIFO_EN							(1<<14)
#define ADC_IT_CH13_EN								(1<<13)
#define ADC_IT_CH12_EN								(1<<12)
#define ADC_IT_CH11_EN								(1<<11)
#define ADC_IT_CH10_EN								(1<<10)
#define ADC_IT_CH9_EN								(1<<9)
#define ADC_IT_CH8_EN								(1<<8)
#define ADC_IT_CH7_EN								(1<<7)
#define ADC_IT_CH6_EN								(1<<6)
#define ADC_IT_CH5_EN								(1<<5)
#define ADC_IT_CH4_EN								(1<<4)
#define ADC_IT_CH3_EN								(1<<3)
#define ADC_IT_CH2_EN								(1<<2)
#define ADC_IT_CH1_EN								(1<<1)
#define ADC_IT_CH0_EN								(1<<0)

/************REG ADC_ICR****************/
#define ADC_IT_RX_FIFO_FULL_CLR						(1<<15)
#define ADC_IT_RX_FIFO_CLR							(1<<14)
#define ADC_IT_CH13_CLR								(1<<13)
#define ADC_IT_CH12_CLR								(1<<12)
#define ADC_IT_CH11_CLR								(1<<11)
#define ADC_IT_CH10_CLR								(1<<10)
#define ADC_IT_CH9_CLR								(1<<9)
#define ADC_IT_CH8_CLR								(1<<8)
#define ADC_IT_CH7_CLR								(1<<7)
#define ADC_IT_CH6_CLR								(1<<6)
#define ADC_IT_CH5_CLR								(1<<5)
#define ADC_IT_CH4_CLR								(1<<4)
#define ADC_IT_CH3_CLR								(1<<3)
#define ADC_IT_CH2_CLR								(1<<2)
#define ADC_IT_CH1_CLR								(1<<1)
#define ADC_IT_CH0_CLR								(1<<0)


//SYSCFG REG (PERI_CLKEN&PERI_RESET) ADC 时钟选择
#define ADC_RESET  	           						(1<<14)
#define ADC_CLKEN  	           						(1<<14)


/***********************************************/
/*                                             */
/*		APPLICATION_MACRO(应用宏声明)          */
/*                                             */
/***********************************************/


#define ADC_LDO 	           				(0)
#define ADC_PF0  	           				(1<<8)
#define ADC_PA6 	           				(1<<6)
#define ADC_PB0 	           				(1<<8)
#define ADC_PB1  	           				(1<<9)
#define ADC_PB2  	           				(1<<10)
#define ADC_PF2  	           				(1<<10)
#define ADC_PF1  	           				(1<<9)
#define ADC_PB7                             (1<<15)
#define ADC_VDDH 	           				(0)
#define ADC_PB6                             (1<<14)
#define ADC_PB5                             (1<<13)
#define ADC_PB4                             (1<<12)
#define ADC_PB3                             (1<<11)

//ADC通道转换使能                                     
#define ADC_CH0_EN							(1<<0)   	// LDO
#define ADC_CH1_EN							(1<<1)   	// ADC_PF0
#define ADC_CH2_EN							(1<<2)   	// ADC_PA6
#define ADC_CH3_EN							(1<<3)   	// ADC_PB0
#define ADC_CH4_EN							(1<<4)   	// ADC_PB1
#define ADC_CH5_EN							(1<<5)   	// ADC_PB2
#define ADC_CH6_EN							(1<<6)   	// ADC_PF2
#define ADC_CH7_EN							(1<<7)   	// ADC_PF1
#define ADC_CH8_EN							(1<<8)   	// ADC_PB7
#define ADC_CH9_EN							(1<<9)   	// VDDH 1/4
#define ADC_CH10_EN							(1<<10)   	// ADC_PB6
#define ADC_CH11_EN							(1<<11)   	// ADC_PB5
#define ADC_CH12_EN							(1<<12)   	// ADC_PB4
#define ADC_CH13_EN							(1<<13)   	// ADC_PB3

//ADC参考电压源选择
#define ADC_REF_VDDH						0			//选择VDDH作为ADC电压源
#define ADC_REF_VREFIO						2			//选择外接VREF作为ADC电压源
#define ADC_REF_VREF						3			//选择内部VREF作为ADC电压源,内部vref默认关闭，需要在系统寄存器中开启，具体的电压值需要自己调整

#define	ADC_IRQ_DISABLE 					0			//ADC中断禁止
#define	ADC_IRQ_ENABLE						1			//ADC中断使能

#define ADC_SINGLE_MODE						0			//ADC单次转换
#define ADC_CONTINUOUS_MODE					1			//ADC连续转换


//在采样周期为4个ADC_Clk时下适用，即默认状态下
#define ADC_SAMPL_RATE_1M_SPS				3
#define ADC_SAMPL_RATE_500K_SPS				6
#define ADC_SAMPL_RATE_200k_SPS				15			
#define ADC_SAMPL_RATE_100K_SPS				30			
#define ADC_SAMPL_RATE_50K_SPS				60			
#define ADC_SAMPL_RATE_25K_SPS				120			
#define ADC_SAMPL_RATE_12500_SPS			240	

#define ADC_SAMPL_RATE_LOWSPEED				32768	



/*******************硬件触发*******************/
#define ADC_HARDWARE_TRIG_GTIM0				0
#define ADC_HARDWARE_TRIG_GTIM0TRGO			1
#define ADC_HARDWARE_TRIG_GTIM1				2
#define ADC_HARDWARE_TRIG_GTIM1TRGO			3
#define ADC_HARDWARE_TRIG_GTIM2				4
#define ADC_HARDWARE_TRIG_GTIM2TRGO			5
#define ADC_HARDWARE_TRIG_LPTIM0OUT0		6
#define ADC_HARDWARE_TRIG_LPTIM0OUT1		7
#define ADC_HARDWARE_TRIG_LPTIM1OUT0		8
#define ADC_HARDWARE_TRIG_LPTIM1OUT1		9
#define ADC_HARDWARE_TRIG_LPTIM2OUT0		10
#define ADC_HARDWARE_TRIG_LPTIM2OUT1		11
#define ADC_HARDWARE_TRIG_LPTIM3OUT0		12
#define ADC_HARDWARE_TRIG_LPTIM3OUT1		13
#define ADC_HARDWARE_TRIG_COMP0				14
#define ADC_HARDWARE_TRIG_COMP1				15
#define ADC_HARDWARE_TRIG_COMP2				16
#define ADC_HARDWARE_TRIG_OPA				17
#define ADC_HARDWARE_TRIG_LVD				18
#define ADC_HARDWARE_TRIG_ATIMCH1			19
#define ADC_HARDWARE_TRIG_ATIMCH2			20
#define ADC_HARDWARE_TRIG_ATIMCH3			21
#define ADC_HARDWARE_TRIG_ATIMCH4			22
#define ADC_HARDWARE_TRIG_ATIMCHTRGO		23
#define ADC_HARDWARE_TRIG_BTIM0				24
#define ADC_HARDWARE_TRIG_BTIM1				25
#define ADC_HARDWARE_TRIG_BTIM2				26
#define ADC_HARDWARE_TRIG_BTIM3				27
#define ADC_HARDWARE_TRIG_PF4				28
#define ADC_HARDWARE_TRIG_PF5				29
#define ADC_HARDWARE_TRIG_PF6				30
#define ADC_HARDWARE_TRIG_RTCFOUT			31

/*******************硬件触发极性*******************/
#define ADC_HARDWARE_POLARITY_UPEDGE		0
#define ADC_HARDWARE_POLARITY_DOWNEDGE		1
#define ADC_HARDWARE_POLARITY_DOUBLEEDGE	2


/***********************************************/
/*                                             */
/*		FUNCTION_INSTANCE(功能接口)            */
/*                                             */
/***********************************************/

/************************************************************************
 * Function   	: adc_init
 * Description	: adc_init 			ADC初始化
 * Input 		: uint8_t vref_sel 	ADC参考电压源选择
 *            	  uint32_t div 		ADC内部时钟分频器分频值
 *			  	  uint8_t work_mode ADC工作模式选择(0:单次模式 1：连续模式)
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_init(uint8_t vref_sel,uint32_t speed_div,uint8_t work_mode);

/************************************************************************
 * Function   	: adc_close
 * Description	: adc_close ADC模块关闭
 * Input 		: none
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_close(void);

/************************************************************************
 * Function   	: adc_irq_init
 * Description	: adc_irq_init 		   ADC中断初始化
 * Input 		: uint8_t irq_enable   ADC中断使能选择
 *            	  uint32_t irq_type    ADC中断类型
 *			  	  void (*pfunc)()      ADC中断回调函数
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_irq_init(uint8_t irq_enable, uint32_t irq_type,void (*pfunc)(uint8_t ch));

/************************************************************************
 * Function   	: adc_channel_gpio_config
 * Description	: adc_channel_gpio_config 	ADC通道配置
 * Input 		: uint8_t channel   		ADC通道选择
 *            	  uint32_t gpio     		ADC通道对应IO管脚
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_channel_gpio_config(uint16_t channel, uint32_t gpio);

/************************************************************************
 * Function   	: adc_convert_start
 * Description	: adc_convert_start 使能ADC转换
 * Input 		: none
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_convert_start(void);

/************************************************************************
 * Function   	: adc_convert_stop
 * Description	: adc_convert_stop 停止ADC转换
 * Input 		: none
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_convert_stop(void);

/************************************************************************
 * Function   	: adc_channel_cal
 * Description	: adc_channel_cal   通道转换，将当前通道的信息转换为实际的通道编号
 * Input 		: uint8_t ch_num    当前完成ADC采样的通道信息
 * Output 		: none				
 * Return		: uint8_t ret		实际的通道编号
 ************************************************************************/
uint8_t adc_channel_cal(uint16_t ch_num);

/************************************************************************
 * Function   	: adc_irq_get_data
 * Description	: adc_irq_get_data			获取ADC通道数据//中断专用
 * Input 		: uint8_t ch_num   		 	ADC通道选择
 * Output 		: none
 * Return		: uint16_t ret    			ADC通道有效数据及通道编号信息（高4位：通道信息、低12位：有效数据）
 ************************************************************************/
uint16_t adc_irq_get_data(uint8_t ch);

/************************************************************************
 * Function   	: adc_get_data
 * Description	: adc_get_data   		 获取ADC通道数据//查询专用
 * Input 		: uint8_t ch_num   		 ADC通道选择
 * Output 		: none
 * Return		: uint8_t ret    		 ADC通道有效数据
 ************************************************************************/
uint16_t adc_get_data(uint8_t ch_num);

/************************************************************************
 * Function   	: adc_rxfifo_config
 * Description	: adc_rxfifo_config			ADC接收缓存配置
 * Input 		: uint8_t rxfigo_trig_level	ADC的rxfifo中断触发数量控制（ADC_RXFIFO_LEAST_8DATA/ADC_RXFIFO_LEAST_1DATA）
 *            	  uint8_t dma_access		ADC的rxfifo访问模式（ADC_DMA_ACCESS_RXFIFO/ADC_CPU_ACCESS_RXFIFO）
 *			  	  uint8_t state				使能状态（ENABLE/DISABLE）
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_rxfifo_config(uint8_t rxfigo_trig_level, uint8_t dma_access, uint8_t state);

/************************************************************************
 * Function   	: adc_hardware_trig_config
 * Description	: adc_hardware_trig_config	ADC硬件触发配置
 * Input 		: uint8_t hardware_type		ADC的硬件触发类型选择
 *            	  uint8_t polarity_type		ADC的硬件触发极性选择
 *			  	  uint8_t state				使能状态（ENABLE/DISABLE）
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_hardware_trig_config(uint8_t hardware_type, uint8_t polarity_type, uint8_t state);

#endif

