/***********************************************************************
 * Copyright (c)  2017 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : btimer23.c
 * Description : btimer23 driver source file
 * Author(s)   : Sissi
 * version     : V1.0
 * Modify date : 2022-08-08
 ***********************************************************************/
#include "btimer23.h"
#include "uart1.h"

void (*btimer23_callback[4])(void) = { 0 };
/************************************************************************
 * Function   	: BTIMER23_IRQHandler
 * Description	: btimer23中断处理函数
 * Input 	    : none
 * Output 		: none
 * Return		: none
 ************************************************************************/
void BTIMER23_IRQHandler(void)
{
	if((REG_BTIM23_SR & VAL_BTIMER2_UIE))					    //UE2中断
	{
		if(btimer23_callback[0] != NULL)
		{
			btimer23_callback[0]();	
			REG_BTIM23_SR = VAL_BTIMER2_UIE;				    //清除UE2中断标志
		}		
	}	
	else if((REG_BTIM23_SR & VAL_BTIMER2_CNIE))				    //比较2中断
	{
		if(btimer23_callback[1] != NULL)
		{
			btimer23_callback[1]();	
			REG_BTIM23_SR = VAL_BTIMER2_CNIE;					//清除比较2中断标志
		}		
	}
	else if((REG_BTIM23_SR & VAL_BTIMER3_UIE))				    //UE3中断
	{		
		if(btimer23_callback[2] != NULL)
		{
			btimer23_callback[2]();	
			REG_BTIM23_SR = VAL_BTIMER3_UIE;					//清除UE3中断标志
		}		
	}	
	else if((REG_BTIM23_SR & VAL_BTIMER3_CNIE))				    //比较3中断
	{		
		if(btimer23_callback[3] != NULL)
		{
			btimer23_callback[3]();	
			REG_BTIM23_SR = VAL_BTIMER3_CNIE;					//清除比较3中断标志
		}		
	}
	
	REG_BTIM23_SR = BTIMER23_IRQ_CLEAR;			                //清除所有中断标志
}

/************************************************************************
 * Function   	: btimer23_irq_init
 * Description	: btimer23 中断初始化
 * Input 		: uint8_t irq_enable		btimer23 中断使能开关 1：使能 0：禁止
 *				  uint8_t btimer_irq_type 	btimer23 中断类型选择
 *            	  void (*pfunc)()			bitmer23 中断回调函数
 * Output 		: none
 * Return		: none
 ************************************************************************/	
void btimer23_irq_init(uint8_t irq_enable,uint8_t btimer_irq_type,void (*pfunc)())
{
	REG_BTIM23_SR = BTIMER23_IRQ_CLEAR;			          		//清除所有中断标志

	if(irq_enable == BTIMER23_IRQ_ENABLE)
	{
		switch(btimer_irq_type)
		{
			case BTIMER2_UE_IRQ:
					btimer23_callback[0] = pfunc;
					REG_BTIM23_DIER |= VAL_BTIMER2_UIE;			//允许CNT2 Update事件中断
					break;
			case BTIMER2_CNT_IRQ:
					btimer23_callback[1] = pfunc;
					REG_BTIM23_DIER |= VAL_BTIMER2_CNIE;		//允许比较2中断
					break;
			case BTIMER3_UE_IRQ:
					btimer23_callback[2] = pfunc;
					REG_BTIM23_DIER |= VAL_BTIMER3_UIE;			//允许CNT3 Update事件中断
					break;
			case BTIMER3_CNT_IRQ:
					btimer23_callback[3] = pfunc;
					REG_BTIM23_DIER |= VAL_BTIMER3_CNIE;		//允许比较3中断
					break;	
		}
		NVIC_ClearPendingIRQ(BTIMER23_IRQn);
		NVIC_EnableIRQ(BTIMER23_IRQn);
	}
	else
	{
		switch(btimer_irq_type)
		{
			case BTIMER2_UE_IRQ:
					btimer23_callback[0] = pfunc;
					REG_BTIM23_DIER &= ~VAL_BTIMER2_UIE;		//禁止CNT2 Update事件中断
					break;
			case BTIMER2_CNT_IRQ:
					btimer23_callback[1] = pfunc;
					REG_BTIM23_DIER &= ~VAL_BTIMER2_CNIE;		//禁止比较2中断
					break;
			case BTIMER3_UE_IRQ:
					btimer23_callback[2] = pfunc;
					REG_BTIM23_DIER &= ~VAL_BTIMER3_UIE;		//禁止CNT3 Update事件中断
					break;
			case BTIMER3_CNT_IRQ:
					btimer23_callback[3] = pfunc;
					REG_BTIM23_DIER &= ~VAL_BTIMER3_CNIE;		//禁止比较3中断
					break;	
		}
	}
}

/************************************************************************
 * Function   	: btimer2_count_init
 * Description	: btimer2 count 初始化
 * Input 		: uint16_t arr 		重载值设置
 *			  	  uint16_t psc 		预分频值设置
 * Output 		: none
 * Return		: none
 ************************************************************************/
void btimer2_count_init(uint16_t arr, uint16_t psc)
{
	REG_SCU_PERIRESET |= BTIMER23_CLKEN;						//BTIMER23模块时钟使能
	REG_SCU_PERICLKEN |= BTIMER23_RESET;						//BTIMER23模块复位使能
	
	REG_BTIM2_ARR = arr;
	REG_BTIM2_PSC = psc;
	
	REG_BTIM2_EGR = 0x1;									//使能update事件
	REG_BTIM2_CR |= (0<<1);									//UE事件发生时计数不停止
	REG_BTIM2_CR |= (1<<6);							    	//ARR寄存器使能preload			
	
	REG_BTIM23_SR = BTIMER23_IRQ_CLEAR;					    //清除所有中断标志
	REG_BTIM23_DIER |= VAL_BTIMER2_UIE;				        //允许CNT Update事件中断
	
	REG_BTIM2_CR |= BTIMER23_CEN_ENABLE;			        //BTIMER2计数器使能
}

/************************************************************************
 * Function   	: btimer3_count_init
 * Description	: btimer3 count 初始化
 * Input 		: uint16_t arr 		  重载值设置
 *			  	  uint16_t psc 		  预分频值设置
 * Output 		: none
 * Return		: none
 ************************************************************************/
void btimer3_count_init(uint16_t arr, uint16_t psc)
{
	REG_SCU_PERIRESET |= BTIMER23_CLKEN;						//BTIMER23模块时钟使能
	REG_SCU_PERICLKEN |= BTIMER23_RESET;						//BTIMER23模块复位使能
	
	REG_BTIM3_ARR = arr;
	REG_BTIM3_PSC = psc;
	
	REG_BTIM3_EGR = 0x1;									//使能update事件
	REG_BTIM3_CR |= (0<<1);									//UE事件发生时计数不停止
	REG_BTIM3_CR |= (1<<6);							    	//ARR寄存器使能preload
	
	REG_BTIM23_SR = BTIMER23_IRQ_CLEAR;					    //清除所有中断标志
	REG_BTIM23_DIER |= VAL_BTIMER3_UIE;				        //允许CNT3 Update事件中断
	
	REG_BTIM3_CR |= BTIMER23_CEN_ENABLE;			        //BTIMER3计数器使能
}

/************************************************************************
 * Function   	: btimer2_pwm_cn_init
 * Description	: btimer2 count pwm输出初始化
 * Input 		: uint16_t arr 		重载值设置
 *				  uint16_t ccr 		比较值设置
 *			  	  uint16_t psc 		预分频值设置
 * Output 		: none
 * Return		: none
 ************************************************************************/
void btimer2_pwm_cn_init(uint16_t arr,uint16_t ccr,uint16_t psc)
{
	REG_SCU_PERIRESET |= BTIMER23_CLKEN;						//BTIMER23模块时钟使能
	REG_SCU_PERICLKEN |= BTIMER23_RESET;						//BTIMER23模块复位使能
	
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 					//清除IO写保护

	REG_SCU_PGSEL = (REG_SCU_PGSEL&(~(0xF<<16)))|(0x5<<16);	//PG4复用为BTIMER2-OUT
	
	REG_SCU_IOCTRLPROTECT = 0xffffffff; 					//开启IO写保护
	
	REG_BTIM2_CR &= ~(1<<2);								//正向输出极性
	REG_BTIM2_CR |= (1<<7);									//输出PWM频率
	REG_BTIM2_CR &= ~(1<<1);								//UE事件发生时计数不停止
	
	REG_BTIM2_ARR = arr;									//重载值
	REG_BTIM2_CCR = ccr;									//比较值
	REG_BTIM2_PSC = psc;									//f(CNT) = f(CLK) / (psc + 1);
	
	REG_BTIM2_EGR = 0x1;									//使能update事件
	
	REG_BTIM23_SR = BTIMER23_IRQ_CLEAR;					    //清除所有中断标志
	
	REG_BTIM2_CR |= BTIMER23_CEN_ENABLE;			        //BTIMER2计数器使能
}

/************************************************************************
 * Function   	: btimer3_pwm_cn_init
 * Description	: btimer3 count pwn输出初始化
 * Input 		: uint16_t arr 		重载值设置
 *				  uint16_t ccr 		比较值设置
 *			  	  uint16_t psc 		预分频值设置
 * Output 		: none
 * Return		: none
 ************************************************************************/
void btimer3_pwm_cn_init(uint16_t arr,uint16_t ccr,uint16_t psc)
{
	REG_SCU_PERIRESET |= BTIMER23_CLKEN;						//BTIMER23模块时钟使能
	REG_SCU_PERICLKEN |= BTIMER23_RESET;						//BTIMER23模块复位使能
	
	REG_SCU_IOCTRLPROTECT = 0xA5A55A5A; 					//清除IO写保护

	REG_SCU_PGSEL = (REG_SCU_PGSEL&(~(0xF<<20)))|(0x8<<20);	//PG5复用为BTIMER3-OUT

	REG_SCU_IOCTRLPROTECT = 0xffffffff; 					//开启IO写保护
	
	REG_BTIM3_CR &= ~(1<<2);								//正向输出极性
	REG_BTIM3_CR |= (1<<7);									//输出PWM频率
	REG_BTIM3_CR &= ~(1<<1);								//UE事件发生时计数不停止
	
	REG_BTIM3_ARR = arr;									//重载值
	REG_BTIM3_CCR = ccr;									//比较值
	REG_BTIM3_PSC = psc;									//f(CNT) = f(CLK) / (psc + 1);
	
	REG_BTIM3_EGR = 0x1;									//使能update事件
	
	REG_BTIM23_SR = BTIMER23_IRQ_CLEAR;					    //清除所有中断标志
					
	REG_BTIM3_CR |= BTIMER23_CEN_ENABLE;			        //BTIMER3计数器使能
}

/************************************************************************
 * function   : btimer2_caen
 * Description: btimer2 count 使能受cen_all控制
 * input      : none
 * return     : none
 ************************************************************************/
void btimer2_caen(void)
{
	REG_BTIM2_CR |= (1<<3);
}

/************************************************************************
 * function   : btimer3_caen
 * Description: btimer3 count使能受cen_all控制
 * input      : none
 * return     : none
 ************************************************************************/
void btimer3_caen(void)
{
	REG_BTIM3_CR |= (1<<3);
}

/************************************************************************
 * function   : btimer2_cen_all
 * Description: 使能Atimer,Btimer01,Btimer23 cnt1,Gtimer,LPtimer cen = 1
 * input      : none
 * return     : none
 ************************************************************************/
void btimer2_cen_all(void)
{
	REG_BTIM2_CR |= (1<<4);
}

/************************************************************************
 * function   : btimer3_cen_all
 * Description: 使能Atimer,Btimer01,Btimer23 cnt0,Gtimer,LPtimer cen = 1
 * input      : none
 * return     : none
 ************************************************************************/
void btimer3_cen_all(void)
{
	REG_BTIM3_CR |= (1<<4);
}
