/***********************************************************************
 * Copyright (c)  2017-2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : lptimer01.c
 * Description : lptimer01 driver source file
 * Author(s)   : zhilan
 * version     : V1.0
 * Modify date : 2022-08-04
 ***********************************************************************/
#include  "lptimer.h"
	
void (*lptimer0func[4])(void) = { 0 };
void (*lptimer1func[4])(void) = { 0 };
/************************************************************************
 * function   : lptimer01_IRQHandler
 * description: lptimer01_IRQHandler
 * input  	  : none  
 * return	  : none
 ************************************************************************/
void LPTIMER01_IRQHandler(void)
{
	if(REG_LPTIM01_SR & 0xFF)
	{
		///////////////////////////////////LPTIMER0/////////////////////////////////////
		if(REG_LPTIM01_SR & VAL_LPTIM0_SR_UIF)		//更新中断
		{                                           
			if(lptimer0func[0]!= NULL)              
			{                                       
				((void(*)())(lptimer0func[0]))();	//lptimer0更新中断处理回调函数
			}	                                    
			REG_LPTIM01_SR = VAL_LPTIM0_SR_UIF;       
		}                                           
		                                            
		if(REG_LPTIM01_SR & VAL_LPTIM0_SR_CC1IF)	//比较1/捕获1触发中断
		{                                           
			if(lptimer0func[1]!= NULL)              
			{                                       
				((void(*)())(lptimer0func[1]))();  	//lptimer0比较1/捕获1触发处理回调函数
			}	                                    
			REG_LPTIM01_SR = VAL_LPTIM0_SR_CC1IF;     
		}                                           
	                                                
		if(REG_LPTIM01_SR & VAL_LPTIM0_SR_CC2IF)	//比较2/捕获2触发中断
		{                                           
			if(lptimer0func[2]!= NULL)              
			{                                       
				((void(*)())(lptimer0func[2]))();	//lptimer0比较2/捕获2触发处理回调函数
			}	                                    
			REG_LPTIM01_SR = VAL_LPTIM0_SR_CC2IF;     
		}                                           
		                                            
		if(REG_LPTIM01_SR & VAL_LPTIM0_SR_TIF)		//外部触发中断
		{                                           
			if(lptimer0func[3]!= NULL)              
			{                                       
				((void(*)())(lptimer0func[3]))();	//lptimer0外部触发中断处理回调函数
			}	
			REG_LPTIM01_SR = VAL_LPTIM0_SR_TIF;
		}
		///////////////////////////////////LPTIMER1/////////////////////////////////////
		if(REG_LPTIM01_SR & VAL_LPTIM1_SR_UIF)		//更新中断
		{                                           
			if(lptimer1func[0]!= NULL)              
			{                                       
				((void(*)())(lptimer1func[0]))();	//lptimer1更新中断处理回调函数
			}	                                    
			REG_LPTIM01_SR = VAL_LPTIM1_SR_UIF;       
		}                                           
		                                            
		if(REG_LPTIM01_SR & VAL_LPTIM1_SR_CC1IF)	//比较1/捕获1触发中断
		{                                           
			if(lptimer1func[1]!= NULL)              
			{                                       
				((void(*)())(lptimer1func[1]))();  	//lptimer1比较1/捕获1触发处理回调函数
			}	                                    
			REG_LPTIM01_SR = VAL_LPTIM1_SR_CC1IF;     
		}                                           
	                                                
		if(REG_LPTIM01_SR & VAL_LPTIM1_SR_CC2IF)	//比较2/捕获2触发中断
		{                                           
			if(lptimer1func[2]!= NULL)              
			{                                       
				((void(*)())(lptimer1func[2]))();	//lptimer1比较2/捕获2触发处理回调函数
			}	                                    
			REG_LPTIM01_SR = VAL_LPTIM1_SR_CC2IF;     
		}                                           
		                                            
		if(REG_LPTIM01_SR & VAL_LPTIM1_SR_TIF)		//外部触发中断
		{                                           
			if(lptimer1func[3]!= NULL)              
			{                                       
				((void(*)())(lptimer1func[3]))();	//lptimer1外部触发中断处理回调函数
			}	
			REG_LPTIM01_SR = VAL_LPTIM1_SR_TIF;
		}
	}
	
	else 
	{
		REG_LPTIM01_SR = 0x0f;						//清除中断状态 												
	}
}
/*******************************************************************
* function		: lptimer01_model_init
* description	: lptimer01_model_init  	LPTimer01定时初始化
* input			: none      	  
* return		: none
********************************************************************/
void lptimer01_model_init(void)
{
	REG_SCU_PERIRESET |= ((uint32_t)0x01<<8);			// lpTimer0正常工作 
	REG_SCU_PERICLKEN |= ((uint32_t)0x01<<8);			// 开启lpTimer0时钟使能 
}			
/*******************************************************************
* function		: lptimer0_count_init
* description	: lptimer0_count_init  	LPTimer定时初始化
* input			: uint32_t clock_source	时钟源选择
*				  uint32_t clock_div	时钟源分频
*             	  uint32_t time			定时时间        	  
* return		: none
********************************************************************/
void lptimer0_count_init(uint32_t clock_source, uint32_t clock_div, uint32_t time)
{
	lptimer01_model_init();
	
	REG_LPTIM0_CR2 &= ~(0x00000FFF);					// 恢复默认值 
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_TMODE_TIME;			// 设置工作模式 
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_MODE_COUNT;			// 连续计数模式 
	REG_LPTIM0_CR2 |= clock_div;						// 设置分频值2分频
	REG_LPTIM0_CR2 |= clock_source;						// 选择时钟源 
	REG_LPTIM0_ARR1 = time;								// 设置高低位目标寄存器的值
}

/*******************************************************************
* function		: lptimer0_pwm_init
* description	: lptimer0_pwm_init     duty cyle（占空比） = (cmp / target) * 100% 
* input			: uint32_t cmp:  	            计数比较值		
*				  uint32_t target:              计数目标值
*            	  uint32_t clock_div  ：        计数时钟分频值
*            	  uint32_t clock_source  ：     计数时钟来源
* return		: none
********************************************************************/
void lptimer0_pwm_init(uint32_t cmp, uint32_t target, uint32_t clock_source, uint32_t clock_div)
{
	lptimer01_model_init(); 
			
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 													// 清除IO写保护 	
	//PWM0						
	REG_SCU_PGSEL = (REG_SCU_PGSEL&(~(0xF<<16)))|(0xC<<16); 								// PG4选择LPTIM0_OUT
	//PWM1							
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<4)))|(0xA<<4); 									// PF1选择LPTIM0_OUT 
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 													// IO写保护 
									
	REG_LPTIM0_CR2 &= ~(0x00000FFF);														// 恢复默认值 
	REG_LPTIM0_CR2 |= clock_source;			         										// 选择时钟源 
	REG_LPTIM0_CR2 |= clock_div;                      										// 分频系数
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_MODE_COUNT;												// 计数模式: 连续计数模式
									
	REG_LPTIM0_CCR1 = cmp;																	// 设置高低位比较寄存器的值
	REG_LPTIM0_CCR2 = cmp;																	// 设置高低位比较寄存器的值	
	REG_LPTIM0_ARR1 = target;																// 设置高低位目标寄存器的值 

	REG_LPTIM0_CCMCFG1 &= ~(0x00003F);
	REG_LPTIM0_CCMCFG2 &= ~(0x00003F);
	REG_LPTIM0_CCMCFG1 = VAL_LPTIM_CCE_ENABLE | VAL_LPTIM_CCS_PWM | VAL_LPTIM_CCP_POLAR2 ;	// 通道1PWM输出配置
	REG_LPTIM0_CCMCFG2 = VAL_LPTIM_CCE_ENABLE | VAL_LPTIM_CCS_PWM | VAL_LPTIM_CCP_POLAR2 ;	// 通道2PWM输出获配置

}

/*******************************************************************
* function		: lptimer0_trigger_init
* description	: lptimer0_trigger_init Trigger触发计数初始化
* input			: uint32_t target:              计数目标值
*				  uint32_t clock_div  ：        计数时钟分频值
*             	  uint32_t clock_source  ：     计数时钟来源
* return		: none
********************************************************************/
void lptimer0_trigger_init(uint32_t clock_source, uint32_t clock_div, uint32_t target)
{
	lptimer01_model_init(); 
	
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 													// 清除IO写保护 
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<0)))|(9<<0);   									// PF0选择LPTIM0_EXT
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 													// IO写保护 
									
	REG_LPTIM0_CR2 &= ~(0x00001FFF);														// 恢复默认值 
	REG_LPTIM0_CR2 |= clock_source;				     										// 选择时钟源 
	REG_LPTIM0_CR2 |= clock_div;                      										// 设置时钟分频
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_MODE_COUNT;												// 计数模式  
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_TMODE_TRIGGER;	    									// Trigger脉冲触发计数模式 
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_TRIGDGE_FALL;											// 外部输入信号下降沿trigger 
	REG_LPTIM0_ARR1 = target;																// 设置高低位目标寄存器的值 
	
}

/*******************************************************************
* function		: lptimer0_extcount_init
* description	: lptimer0_extcount_init 外部异步脉冲计数初始化
* input			: uint32_t target:              计数目标值 
*				  uint32_t clock_div  ：        计数时钟分频值
*				  uint32_t clock_source  ：     计数时钟来源
* return		: none
********************************************************************/
void lptimer0_extcount_init(uint32_t clock_source, uint32_t clock_div, uint32_t target)
{
	lptimer01_model_init();  
	
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 													// 清除IO写保护  
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<8)))|(0xB<<8);   								// PF2选择LPTIM0_IN 
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 													// IO写保护
									
	REG_LPTIM0_CR2 &= ~(0x00001FFF);														// 恢复默认值 
	REG_LPTIM0_CR2 |= clock_source;				      										// 选择时钟源 
	REG_LPTIM0_CR2 |= clock_div;                      										// 分频系数
	REG_LPTIM0_ARR1 = target;																// 设置高低位目标寄存器的值 
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_TMODE_EXT;												// 选择外部异步脉冲模式
									
	REG_LPTIM0_CR2 |=  VAL_LPTIM_CR2_EDGESEL_FALL;   										// LPTIN 的上升沿计数
 
}
/*******************************************************************
* function		: lptimer0_capture_init
* description	: lptimer0_capture_init   lptimer 输入捕获初始化
* input			: uint32_t target:              计数目标值 
*				  uint32_t clock_div  ：        计数时钟分频值
*				  uint32_t clock_source  ：     计数时钟来源
* return		: none
********************************************************************/
void lptimer0_capture_init(uint32_t target, uint32_t clock_source, uint32_t clock_div)
{
	lptimer01_model_init();  
		
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 													// 清除IO写保护 
																													
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<16)))|(0xE<<16); 								// PF4选择LPTIM0_CAP0
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 													// IO写保护 
									
	REG_LPTIM0_CR2 &= ~(0x00001FFF);														// 恢复默认值 
	REG_LPTIM0_CR2 |= clock_source;			         										// 选择时钟源 
	REG_LPTIM0_CR2 |= clock_div;                      										// 分频系数
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_MODE_COUNT;												// 计数模式: 连续计数模式
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_TRIGDGE_FALL;
	
	REG_LPTIM0_CCMCFG1 = VAL_LPTIM_CCE_ENABLE | VAL_LPTIM_CCS_CAP | VAL_LPTIM_CAPEDGE_DOWN ;// 通道1输入捕获配置
	REG_LPTIM0_ARR1 = target;																// 设置高低位目标寄存器的值 

}

/*******************************************************************
* function		: lptimer0_time_out_init
* description	: lptimer0_time_out_init  lptimer 超时退出初始化
* input			: uint32_t target:              计数目标值  
*				  uint32_t clock_div  ：        计数时钟分频值
*				  uint32_t clock_source  ：     计数时钟来源
* return		: none
********************************************************************/
void lptimer0_time_out_init(uint32_t clock_source, uint32_t clock_div, uint32_t target)
{
	lptimer01_model_init(); 
	
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 													// 清除IO写保护
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<0)))|(9<<0);   									// PF0选择LPTIM0_EXT
	if(clock_source == VAL_LPTIM_CR2_CLKSEL_LPTIN)							
	{							
		REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<8)))|(0xB<<8);   							// PF2选择LPTIM0_IN 
	}											
								
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 													// IO写保护 
								
	REG_LPTIM0_CR2 &= ~(0x00001FFF);														// 恢复默认值 
	REG_LPTIM0_CR2 |= clock_source;				         									// 时钟源选择 
	REG_LPTIM0_CR2 |= clock_div;                    										// 设置时钟分频
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_MODE_COUNT;												// 计数模式 
	REG_LPTIM0_ARR1 = target;																// 设置高低位目标寄存器的值 
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_TMODE_TIMEOUT;											// 模式选择 	
	REG_LPTIM0_CR2 |= VAL_LPTIM_CR2_TRIGDGE_FALL;											// 外部触发边沿选择			
}

/*******************************************************************
* function		: lptimer0_irq_init
* description	: lptimer0_irq_init 中断初始化
* input			: uint8_t irq_enable:中断开关
*				  uint32_t irq_type:中断类型
*				  void (*pfunc)():中断回调函数
* return		: none
********************************************************************/
void lptimer0_irq_init(uint8_t irq_enable, uint32_t irq_type, void (*pfunc)()) 
{
	if(irq_enable == VAL_LPTIM_IRQ_ENABLE)				   									//中断使能
	{                                                   									
		NVIC_ClearPendingIRQ(LPTIMER01_IRQn);            									
		NVIC_EnableIRQ(LPTIMER01_IRQn);                  									
																							
		if(irq_type == VAL_LPTIM0_IER_UIE)         												
		{                                               									
			REG_LPTIM01_IER |= VAL_LPTIM0_IER_UIE;		   									//计数器溢出中断使能
			lptimer0func[0] = pfunc;                       									    
		}                                                  									    
		if(irq_type == VAL_LPTIM0_IER_CC1IE)  													
		{                                                  									    
			REG_LPTIM01_IER |= VAL_LPTIM0_IER_CC1IE;										//比较/捕获通道1中断使能
			lptimer0func[1] = pfunc;                       									    
		}                                                  									    
		if(irq_type == VAL_LPTIM0_IER_CC2IE)  													
		{                                                  									    
			REG_LPTIM01_IER |= VAL_LPTIM0_IER_CC2IE;										//比较/捕获通道2中断使能
			lptimer0func[2] = pfunc;                    									
		}                                               									
		if(irq_type == VAL_LPTIM0_IER_TIE)  													
		{                                                  									    
			REG_LPTIM01_IER |= VAL_LPTIM0_IER_TIE;		   									//外部触发到来中断使能
			lptimer0func[3] = pfunc;                    									
		}                                  
		 		
	}                                                   
	else                                                                             
	{		                                            
		if(irq_type == VAL_LPTIM0_IER_UIE) 				 
		{                                                    
			REG_LPTIM01_IER &= ~(VAL_LPTIM0_IER_UIE); 										//计数器溢出中断不使能
			lptimer0func[0] = NULL;                     									        
		}                                               									        
		if(irq_type == VAL_LPTIM0_IER_CC1IE)            									 	
		{                                               									        
			REG_LPTIM01_IER &= ~(VAL_LPTIM0_IER_CC1IE);										//比较/捕获通道1中断不使能
			lptimer0func[1] = NULL;                     									        
		}                                               									        
		if(irq_type == VAL_LPTIM0_IER_CC2IE)            									 	
		{                                               									        
			REG_LPTIM01_IER &= ~(VAL_LPTIM0_IER_CC2IE);										//比较/捕获通道2中断不使能
			lptimer0func[2] = NULL;                     									
		}                                               									
		if(irq_type == VAL_LPTIM0_IER_TIE)  													
		{                                               									        
			REG_LPTIM01_IER |= VAL_LPTIM0_IER_TIE;											//外部触发到来中断不使能
			lptimer0func[3] = NULL;
		}
		
		NVIC_ClearPendingIRQ(LPTIMER01_IRQn);                                               //清除中断    
		NVIC_DisableIRQ(LPTIMER01_IRQn);                                                    //中断禁止
		
	}
}

/*******************************************************************
* function		: lptimer0_start
* description	: enable lptimer0 count
* input			: none
* return		: none
********************************************************************/
void lptimer0_start(void)
{
	REG_LPTIM0_CR1 |= VAL_LPTIM_CR1_ENABLE;		                                           // 打开LPTEN使能位，启动计数器
}

/*******************************************************************
* function		: lptimer0_stop
* description	: disable lptimer count
* input			: none
* return		: none
********************************************************************/
void lptimer0_stop(void)
{
	REG_LPTIM0_CR1 &= ~VAL_LPTIM_CR1_ENABLE;	                                          // 关闭LPTEN使能位，关闭计数器
}


/////////////////////////////////////////////////////////////////////////LPTIMER1////////////////////////////////////////////////////////////////////////////////////

/*******************************************************************
* function		: lptimer1_count_init
* description	: lptimer1_count_init  	LPTimer1定时初始化
* input			: uint32_t clock_source	时钟源选择
*				  uint32_t clock_div	时钟源分频
*             	  uint32_t time			定时时间        	  
* return		: none
********************************************************************/
void lptimer1_count_init(uint32_t clock_source, uint32_t clock_div, uint32_t time)
{
	lptimer01_model_init();                             									// lptimer1 模块时钟初始化  
												
	REG_LPTIM1_CR4 &= ~(0x00001FFF);														// 恢复默认值 
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_TMODE_TIME;												// 设置工作模式 
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_MODE_COUNT;												// 计数模式 
	REG_LPTIM1_CR4 |= clock_div;															// 设置分频值2分频
	REG_LPTIM1_CR4 |= clock_source;															// 选择时钟源 
	REG_LPTIM1_ARR2 = time;																	// 设置高低位目标寄存器的值
}

/*******************************************************************
* function		: lptimer1_pwm_init
* description	: lptimer1_pwm_init      duty cyle（占空比） = (cmp / target) * 100% 
* input			: uint32_t cmp:              计数器比较值			
*				  uint32_t target:           计数目标值
*            	  uint32_t clock_div  ：     计数时钟分频值
*            	  uint32_t clock_source  ：  计数时钟来源
* return		: none
********************************************************************/
void lptimer1_pwm_init(uint32_t cmp, uint32_t target, uint32_t clock_source, uint32_t clock_div)
{
	lptimer01_model_init();                                             					// lptimer1 模块时钟初始化  
									
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 													// 清除IO写保护 						
	                                                                                        // PWM0						
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<4)))|(0xC<<4); 		   							// PF1选择LPTIM1_OUT 
	                                                                                        // PWM1							
	REG_SCU_PBSEL = (REG_SCU_PBSEL&(~(0xF<<20)))|(0x6<<20); 								// PB5选择LPTIM1_OUT 
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 													// IO写保护 
							
	REG_LPTIM1_CR4 &= ~(0x00001FFF);														// 恢复默认值 
	REG_LPTIM1_CR4 |= clock_source;			         										// 选择时钟源 
	REG_LPTIM1_CR4 |= clock_div;                      										// 分频系数
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_MODE_COUNT;												// 计数模式: 连续计数模式
								
	REG_LPTIM1_CCR3 = cmp;																	// 设置比较寄存器的值 
	REG_LPTIM1_CCR4 = cmp;																	// 设置比较寄存器的值 
	REG_LPTIM1_ARR2 = target;																// 设置高低位目标寄存器的值 
			 
	REG_LPTIM1_CCMCFG3 &= ~(0x00003F);
	REG_LPTIM1_CCMCFG4 &= ~(0x00003F);
	REG_LPTIM1_CCMCFG3 = VAL_LPTIM_CCE_ENABLE | VAL_LPTIM_CCS_PWM | VAL_LPTIM_CCP_POLAR2 ;	// 通道1PWM输出配置
	REG_LPTIM1_CCMCFG4 = VAL_LPTIM_CCE_ENABLE | VAL_LPTIM_CCS_PWM | VAL_LPTIM_CCP_POLAR2 ;	// 通道2PWM输出配置
}

/*******************************************************************
* function		: lptimer1_trigger_init
* description	: lptimer1_trigger_init Trigger触发计数初始化
* input			: uint32_t target:           计数目标值
*            	  uint32_t clock_div  ：     计数时钟分频值
*				  uint32_t clock_source  ：  计数时钟来源
* return		: none
********************************************************************/
void lptimer1_trigger_init(uint32_t clock_source, uint32_t clock_div, uint32_t target)
{
	lptimer01_model_init();                                								    // lptimer1 模块时钟初始化 
									
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 													// 清除IO写保护 
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<0)))|(0xB<<0); 									// PF0选择LPTIM1_EXT 
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 													// IO写保护 
									
	REG_LPTIM1_CR4 &= ~(0x00001FFF);														// 恢复默认值 
	REG_LPTIM1_CR4 |= clock_source;				     										// 选择时钟源 
	REG_LPTIM1_CR4 |= clock_div;                      										// 设置时钟分频
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_MODE_COUNT;												// 计数模式  
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_TMODE_TRIGGER;											// Trigger脉冲触发计数模式 
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_TRIGDGE_FALL;											// 外部输入信号下降沿trigger 
	REG_LPTIM1_ARR2 = target;																// 设置高低位目标寄存器的值 
}

/*******************************************************************
* function		: lptimer1_extcount_init
* description	: lptimer1_extcount_init 外部异步脉冲计数初始化
* input			: uint32_t target:           计数目标值
*				  uint32_t clock_div  ：     计数时钟分频值
*				  uint32_t clock_source  ：  计数时钟来源
* return		: none
********************************************************************/
void lptimer1_extcount_init(uint32_t clock_source, uint32_t clock_div, uint32_t target)
{
	lptimer01_model_init();                                 							// lptimer1 模块时钟初始化 
								
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 												// 清除IO写保护   
	REG_SCU_PGSEL = (REG_SCU_PGSEL&(~(0xF<<8)))|(0xE<<8);   							// PG2选择LPTIM1_IN 
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 												// IO写保护
								
	REG_LPTIM1_CR4 &= ~(0x00001FFF);													// 恢复默认值 
	REG_LPTIM1_CR4 |= clock_source;				      									// 选择时钟源 
	REG_LPTIM1_CR4 |= clock_div;                      									// 分频系数
	REG_LPTIM1_ARR2 = target;															// 设置高低位目标寄存器的值 
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_TMODE_EXT;											// 选择外部异步脉冲模式
										
	REG_LPTIM1_CR4 |=  VAL_LPTIM_CR2_EDGESEL_FALL;   									// LPTIN 的上升沿计数
 
}
/*******************************************************************
* function		: lptimer1_capture_init
* description	: lptimer1_capture_init     lptimer输入捕获初始化 
* input			: uint32_t target:           计数目标值
*				  uint32_t clock_div  ：     计数时钟分频值
*				  uint32_t clock_source  ：  计数时钟来源
* return		: none
********************************************************************/
void lptimer1_capture_init(uint32_t target, uint32_t clock_source, uint32_t clock_div)
{
	lptimer01_model_init();                                     									// lptimer1 模块时钟初始化  
											
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 															// 清除IO写保护 
																									
	REG_SCU_PGSEL = (REG_SCU_PGSEL&(~(0xF<<4)))|(0xC<<4); 											// PG1选择LPTIM1_CAP1																	
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 															// IO写保护 
											
	REG_LPTIM1_CR4 &= ~(0x00001FFF);																// 恢复默认值 
	REG_LPTIM1_CR4 |= clock_source;			         												// 选择时钟源 
	REG_LPTIM1_CR4 |= clock_div;                      												// 分频系数
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_MODE_COUNT;														// 计数模式: 连续计数模式
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_TRIGDGE_FALL;
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_CAP1SRSEL_LPTCH1;
			 
	REG_LPTIM1_CCMCFG4 = VAL_LPTIM_CCE_ENABLE | VAL_LPTIM_CCS_CAP | VAL_LPTIM_CAPEDGE_UP_DOWN ;		// 通道2输入捕获配置
	REG_LPTIM1_ARR2 = target;																		// 设置高低位目标寄存器的值 

}

/*******************************************************************
* function		: lptimer1_time_out_init
* description	: lptim1_time_out  lptimer 超时退出初始化
* input			: uint32_t target:           计数目标值  
*				  uint32_t clock_div  ：     计数时钟分频值 
*				  uint32_t clock_source  ：  计数时钟来源
* return		: none
********************************************************************/
void lptimer1_time_out_init(uint32_t clock_source, uint32_t clock_div, uint32_t target)
{
	lptimer01_model_init();                                     									// lptimer1 模块时钟初始化 
										
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 															// 清除IO写保护
	REG_SCU_PFSEL = (REG_SCU_PFSEL&(~(0xF<<0)))|(0xB<<0);  											// PF0选择LPTIM1_EXT
	if(clock_source == VAL_LPTIM_CR2_CLKSEL_LPTIN)									
	{									
		REG_SCU_PGSEL = (REG_SCU_PGSEL&(~(0xF<<8)))|(0xE<<8);   									// PG2选择LPTIM1_IN 
	}													
										
	REG_SCU_IOCTRLPROTECT = 0xFFFFFFFF; 															// IO写保护 
										
	REG_LPTIM1_CR4 &= ~(0x00001FFF);																// 恢复默认值 
	REG_LPTIM1_CR4 |= clock_source;				         											// 时钟源选择 
	REG_LPTIM1_CR4 |= clock_div;                    												// 设置时钟分频
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_MODE_COUNT;														// 计数模式 
	REG_LPTIM1_ARR2 = target;																		// 设置高低位目标寄存器的值 
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_TMODE_TIMEOUT;													// 模式选择 	
	REG_LPTIM1_CR4 |= VAL_LPTIM_CR2_TRIGDGE_FALL;													// 外部触发边沿选择			
}

/*******************************************************************
* function		: lptimer1_irq_init
* description	: lptimer1_irq_init 中断初始化
* input			: uint8_t irq_enable：中断开关
*				  uint32_t irq_type： 中断类型
*		          void (*pfunc)()：   中断回调函数
* return		: none
********************************************************************/
void lptimer1_irq_init(uint8_t irq_enable, uint32_t irq_type, void (*pfunc)()) 
{
	if(irq_enable == VAL_LPTIM_IRQ_ENABLE)				   											//中断开关
	{                                                   											
		NVIC_ClearPendingIRQ(LPTIMER01_IRQn);            											
		NVIC_EnableIRQ(LPTIMER01_IRQn);                  											
																									
		if(irq_type == VAL_LPTIM1_IER_UIE)         														
		{                                               											
			REG_LPTIM01_IER |= VAL_LPTIM1_IER_UIE;		   											//计数器溢出中断使能
			lptimer1func[0] = pfunc;                       											     
		}                                                  											     
		if(irq_type == VAL_LPTIM1_IER_CC1IE)  															
		{                                                  											     
			REG_LPTIM01_IER |= VAL_LPTIM1_IER_CC1IE;												//比较/捕获通道1中断使能
			lptimer1func[1] = pfunc;                       											     
		}                                                  											     
		if(irq_type == VAL_LPTIM1_IER_CC2IE)  															
		{                                                  											     
			REG_LPTIM01_IER |= VAL_LPTIM1_IER_CC2IE;												//比较/捕获通道2中断使能
			lptimer1func[2] = pfunc;                    											
		}                                               											
		if(irq_type == VAL_LPTIM1_IER_TIE)  															
		{                                                  											     
			REG_LPTIM01_IER |= VAL_LPTIM1_IER_TIE;		   											 //外部触发到来中断使能
			lptimer1func[3] = pfunc;                    
		}                                               
	}                                                   
	else                                                
	{		                                            
		if(irq_type == VAL_LPTIM1_IER_UIE) 				 
		{                                                    
			REG_LPTIM01_IER &= ~(VAL_LPTIM1_IER_UIE); 												//计数器溢出中断不使能
			lptimer1func[0] = NULL;                     											        
		}                                               											        
		if(irq_type == VAL_LPTIM1_IER_CC1IE)            											 	
		{                                               											        
			REG_LPTIM01_IER &= ~(VAL_LPTIM1_IER_CC1IE);												//比较/捕获通道1中断不使能
			lptimer1func[1] = NULL;                     											        
		}                                               											        
		if(irq_type == VAL_LPTIM1_IER_CC2IE)            											 	
		{                                               											        
			REG_LPTIM01_IER &= ~(VAL_LPTIM1_IER_CC2IE);												//比较/捕获通道2中断不使能
			lptimer1func[2] = NULL;                     											
		}                                               											
		if(irq_type == VAL_LPTIM1_IER_TIE)  															
		{                                               											        
			REG_LPTIM01_IER &= ~ (VAL_LPTIM1_IER_TIE);												//外部触发到来中断不使能
			lptimer1func[3] = NULL;
		}
		
		NVIC_ClearPendingIRQ(LPTIMER01_IRQn);                                                       //中断清除
		NVIC_DisableIRQ(LPTIMER01_IRQn);                                                            //中断禁止
	}
}
/*******************************************************************
* function		: lptimer1_start
* description	: enable lptimer1 count
* input			: none
* return		: none
********************************************************************/
void lptimer1_start(void)
{
	REG_LPTIM1_CR3 |= VAL_LPTIM_CR1_ENABLE;															// 打开LPTEN使能位，启动计数器
}

/*******************************************************************
* function		: lptimer1_stop
* description	: disable lptimer count
* input			: none
* return		: none
********************************************************************/
void lptimer1_stop(void)
{
	REG_LPTIM1_CR3 &= ~VAL_LPTIM_CR1_ENABLE;														// 关闭LPTEN使能位，关闭计数器
}
