/***********************************************************************************************************
 * Copyright (c)  2021 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : gtimer.h
 * Description : gtimer driver header file
 * Author(s)   : yanhaihua
 * version     : V1.0
 * Modify date : 2021-09-23
 ***********************************************************************************************************/
#ifndef __GTIMER_H__
#define __GTIMER_H__

#include "config.h"

//GTIMER_ID
#define GTIMER0	        			0		    	//GTIMER0_ID
#define GTIMER1		        		1		    	//GTIMER1_ID
#define GTIMER2	        			2		    	//GTIMER2_ID

//GTIMER时钟使能
#define GTIMER0_CLKEN	        	(1<<9)		    //GTIMER0时钟使能
#define GTIMER1_CLKEN		        (1<<2)		    //GTIMER1时钟使能
#define GTIMER2_CLKEN	        	(1<<3)		    //GTIMER2时钟使能

//GTIMER复位
#define GTIMER0_RESET	        	(1<<9)		    //GTIMER0复位
#define GTIMER1_RESET		        (1<<2)		    //GTIMER1复位
#define GTIMER2_RESET	        	(1<<3)		    //GTIMER2复位

/********        GTIM_IER寄存器         ********/
#define VAL_GTIM_UIE	        	(1<<0)		    //UPTADE事件中断
#define VAL_GTIM_CCIE		        (1<<1)		    //捕捉/比较事件中断
#define VAL_GTIM_BKE_IE	        	(1<<2)		    //刹车事件中断

/********        GTIM_SR寄存器         ********/
#define VAL_GTIM_SRCLEAR	        (0x7)		    //清除所有中断标志
#define VAL_GTIM_UIF	        	(1<<0)		    //UPTADE事件中断标志
#define VAL_GTIM_CCIF		        (1<<1)		    //捕捉/比较事件中断标志
#define VAL_GTIM_BKEF	        	(1<<2)		    //刹车事件中断标志


/********        GTIM_EGR寄存器         ********/
#define VAL_EGR	        			(0x1)		    //产生Update事件

/********        GTIM_CR寄存器         ********/
#define VAL_GTIM_ADC_HDT	        (1<<28)		    //ADC硬件触发功能使能
#define VAL_GTIM_OPA_BKEN	        (1<<27)		    //OPA作为刹车源使能
#define VAL_GTIM_COMP2_BKEN	        (1<<26)		    //COMP2作为刹车源使能
#define VAL_GTIM_COMP1_BKEN	        (1<<25)		    //COMP1作为刹车源使能
#define VAL_GTIM_COMP0_BKEN	        (1<<24)		    //COMP0作为刹车源使能
#define VAL_GTIM_LVD_BKEN	        (1<<23)		    //LVD作为刹车源使能
#define VAL_GTIM_BREAK2_SEL	        (1<<22)		    //选择Gtimer配置的IO作为刹车源
#define VAL_GTIM_BREAK1_SEL	        (1<<21)		    //选择Gtimer配置的IO作为刹车源
#define VAL_GTIM_GTIMER_BK	        (0<<21)		    //选择各自的GTIMER_BK IO作为刹车源

#define VAL_GTIM_PWMN_IDLE	        (1<<20)		    //PWM输出负向电平IDLE状态 为高电平
#define VAL_GTIM_PWMP_IDLE          (1<<19)		    //PWM输出正向电平IDLE状态 为高电平

#define VAL_GTIM_MOE_ENABLE			(1<<18)			//输出总使能

#define VAL_GTIM_PWMN_B_S	        (1<<16)		    //PWM刹车触发后，PWM互补电平状态设置位 为高电平
#define VAL_GTIM_PWMS_B_S           (1<<14)		    //PWM刹车触发后，PWM正向电平状态设置位 为高电平

#define VAL_GTIM_SOFT_BK			(1<<13)			//软件触发刹车功能

#define VAL_GTIM_CEN_ALL			(1<<12)			//同时使能Gtimer0/1/2和LPTIM0/1/2，写此位后Gtimer0/1/2和LPTIM0/1/2的CEN位同时为1
#define VAL_GTIM_CEN_ALL_EN		    (1<<3)			//当前Gtimer可以被CEN_ALL控制

#define VAL_GTIM_BKE_POL_LOW		(1<<11)			//刹车信号低电平有效
#define VAL_GTIM_BKE_POL_HIGH		(0<<11)			//刹车信号高电平有效

#define VAL_GTIM_BKE			    (1<<10)			//刹车功能使能

#define VAL_GTIM_PWM_DEAD_ENABLE	(1<<9)			//避免死区功能使能

#define VAL_GTIM_PWM_INV_ENABLE		(1<<8)			//互补PWM和原PWM反相位

#define VAL_GTIM_UE_TRGO			(1<<7)			//UE信号被用作TRGO
#define VAL_GTIM_OC1REF_TRGO		(0<<7)			//OC1REF用作TRGO

#define VAL_GTIM_PRELOAD_ENABLE		(1<<6)			//ARR寄存器使能preload
#define VAL_GTIM_PRELOAD_DISABLE	(0<<6)			//ARR寄存器不使能preload

#define VAL_GTIM_EDGE_ALIGN			(0<<4)			//边沿对齐模式
#define VAL_GTIM_CENTRAL_ALIGN1		(1<<4)			//中央对齐模式1，输出比较中断标志仅在计数器向下计数的过程中置位
#define VAL_GTIM_CENTRAL_ALIGN2		(2<<4)			//中央对齐模式2，输出比较中断标志仅在计数器向上计数的过程中置位
#define VAL_GTIM_CENTRAL_ALIGN3		(3<<4)			//中央对齐模式3，输出比较中断标志在计数器向上向下计数的过程中都会置位

#define VAL_GTIM_EDGE_DIR_UP		(0<<2)			//边沿模式向上计数
#define VAL_GTIM_EDGE_DIR_DOWN		(1<<2)			//边沿模式向下计数

#define VAL_GTIM_OPM_ENABLE			(1<<1)			//Update Event发生时计数器停止（自动清零CEN）
#define VAL_GTIM_CEN				(1<<0)			//计数器使能

/********        GTIM_CCMR寄存器         ********/
#define VAL_GTIM_CAPCLR				(1<<15)			//计数器使能后保持为0，直到捕捉到第一个沿开始计数

#define VAL_GTIM_CAPSSEL_GTIMER_CH		(0<<8)		//GTIMER_CH作为捕捉源
#define VAL_GTIM_CAPSSEL_CLK32K			(3<<8)		//CLK32K作为捕捉源
#define VAL_GTIM_CAPSSEL_UART0_RX		(1<<8)		//UART0_RX作为GTIMER0捕捉源
#define VAL_GTIM_CAPSSEL_UART1_RX		(1<<8)		//UART1_RX作为GTIMER1捕捉源
#define VAL_GTIM_CAPSSEL_I2C_SCL		(1<<8)		//I2C_SCL作为GTIMER2捕捉源
#define VAL_GTIM_CAPSSEL_LPTIM0_LPOUT	(2<<8)		//LPTIM0_LPOUT作为GTIMER0捕捉源
#define VAL_GTIM_CAPSSEL_LPTIM1_LPOUT	(2<<8)		//LPTIM1_LPOUT作为GTIMER1捕捉源
#define VAL_GTIM_CAPSSEL_LPTIM2_LPOUT	(2<<8)		//LPTIM2_LPOUT作为GTIMER2捕捉源

#define VAL_GTIM_ICPSC_DIV1			(0<<13)			//捕捉源1分频
#define VAL_GTIM_ICPSC_DIV2			(1<<13)			//捕捉源2分频
#define VAL_GTIM_ICPSC_DIV4			(2<<13)			//捕捉源4分频
#define VAL_GTIM_ICPSC_DIV8			(3<<13)			//捕捉源8分频

#define VAL_GTIM_CAPEDGE_UP			(0<<10)			//捕捉源上升沿触发
#define VAL_GTIM_CAPEDGE_DOWN		(1<<10)			//捕捉源下降沿触发
#define VAL_GTIM_CAPEDGE_UP_DOWN	(2<<10)			//捕捉源上升或下降沿触发

#define VAL_GTIM_TFLT_ENABLE		(1<<7)			//外部计数源滤波使能
#define VAL_GTIM_TEDGE_UP			(0<<6)			//计数源上升沿计数
#define VAL_GTIM_TEDGE_DOWN			(1<<6)			//计数源下降沿计数

#define VAL_GTIM_TSSEL_APBCLK		(0<<4)			//APBCLK作为计数时钟源
#define VAL_GTIM_TSSEL_CLK32K		(2<<4)			//CLK32K作为计数时钟源
#define VAL_GTIM_TSSEL_GTIMER_CH	(3<<4)			//外部时钟源作为计数时钟源

#define VAL_GTIM_TSSEL_GTIMER2_TRGO	(1<<4)		    //GTIMER2 TRGO作为GTIMER0计数时钟源
#define VAL_GTIM_TSSEL_GTIMER0_TRGO	(1<<4)		    //GTIMER0 TRGO作为GTIMER1计数时钟源
#define VAL_GTIM_TSSEL_GTIMER1_TRGO	(1<<4)		    //GTIMER1 TRGO作为GTIMER2计数时钟源

#define VAL_GTIM_CCS_OUTPUT			(0<<0)			//CC通道配置为输出
#define VAL_GTIM_CCS_INPUT			(1<<0)			//CC通道配置为输入

#define VAL_GTIM_CCS	        	(1<<0)		    //CC通道方向选择

/********        GTIM_CCER寄存器         ********/
#define VAL_GTIM_CCP_HIGH			(0<<1)			//CNT<CCR时输出高电平
#define VAL_GTIM_CCP_LOW			(1<<1)			//CNT>CCR时输出高电平
#define VAL_GTIM_CCE_ENABLE			(1<<0)			//捕捉/比较输出使能

#define GTIM_MOE_ENABLE				(1)
#define GTIM_MOE_DISABLE			(0)

#define GTIM_SOFT_BK_ENABLE			(1)
#define GTIM_SOFT_BK_DISABLE		(0)

#define GTIM_IRQ_ENABLE				(1)
#define GTIM_IRQ_DISABLE			(0)

#define GTIM_UE_IRQ					(1)
#define GTIM_COMP_IRQ				(2)
#define GTIM_CAP_IRQ				(3)
#define GTIM_BKE_IRQ				(4)


#ifdef UM3213xA_USING_GTIMER0

void gtimer0_pwm_init(uint16_t arr,uint16_t ccr,uint16_t psc);

void gtimer0_start(void);

void gtimer0_stop(void);

void gtimer0_pwm_enable(void);

void gtimer0_pwm_idle(void);

void gtimer0_count_init(uint8_t clk_src,uint16_t arr,uint16_t psc);

void gtimer0_irq_init(uint8_t irq_enable,uint8_t gtimer_irq_type,void (*pfunc)());

uint16_t gtimer0_get_cnt(void);

void gtimer0_clear_cnt(void);

#endif

#ifdef UM3213xA_USING_GTIMER1

void gtimer1_pwm_init(uint16_t arr,uint16_t ccr,uint16_t psc);

void gtimer1_start(void);

void gtimer1_stop(void);

void gtimer1_pwm_enable(void);

void gtimer1_pwm_idle(void);

#endif

#ifdef UM3213xA_USING_GTIMER2

void gtimer2_count_init(uint16_t arr,uint16_t psc);

void gtimer2_count_irq_init(em_enable_t enable,void (*pfunc)());

void gtimer2_start(void);

void gtimer2_stop(void);

#endif

#if defined(UM3213xA_USING_GTIMER1) || defined(UM3213xA_USING_GTIMER1)

void gtimer_start_all(void);

void gtimer_set_irq_flag(uint8_t flag);
#endif

#endif

