/***********************************************************************************************************
 * 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"

#if defined(UM2080F32_USING_GTIMER0) || defined(UM2080F32_USING_GTIMER1) || defined(UM2080F32_USING_GTIMER2)

#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_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_ADC_HDT	        			(1<<28)		    											/* ADC硬件触发功能使能 */
#define VAL_GTIM_OPA_BKEN	        			(1<<27)		    											/* OPA作为刹车源使能 */
#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_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_BKE_POL_LOW					(1<<11)														/* 刹车信号低电平有效 */
#define VAL_GTIM_BKE_POL_HIGH					(0<<11)														/* 刹车信号高电平有效 */
#define VAL_GTIM_BKE			    			(1<<10)														/* 刹车功能使能 */

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

#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_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计数时钟源 */

typedef enum
{
	GTIMER_IRQ_UIE								= 0,
	GTIMER_IRQ_CCIE								= 1,
	GTIMER_IRQ_BKE_IE							= 2,
}em_gtimer_irq_t;

#endif

#ifdef UM2080F32_USING_GTIMER0

void gtimer0_count_init(uint8_t clk_src,uint16_t arr,uint16_t psc);
void gtimer0_pwm_init(uint8_t clk_src,uint16_t arr,uint16_t ccr,uint16_t psc);
void gtimer0_capture_init(uint8_t clk_src,uint16_t arr,uint16_t psc,uint8_t cap_div);
void gtimer0_bke_init(uint32_t bke_src,uint32_t bke_pol);
void gtimer0_moe_set(em_enable_t moe_enable);
void gtimer0_soft_bke_set(em_enable_t soft_bk_enable);
void gtimer0_irq_init(em_enable_t irq_enable,em_gtimer_irq_t irq_type,void (*pfunc)());
void gtimer0_start(void);
void gtimer0_stop(void);

#endif

#ifdef UM2080F32_USING_GTIMER1

void gtimer1_count_init(uint8_t clk_src,uint16_t arr,uint16_t psc);
void gtimer1_pwm_init(uint8_t clk_src,uint16_t arr,uint16_t ccr,uint16_t psc);
void gtimer1_capture_init(uint8_t clk_src,uint16_t arr,uint16_t psc,uint8_t cap_div);
void gtimer1_bke_init(uint32_t bke_src,uint32_t bke_pol);
void gtimer1_moe_set(em_enable_t moe_enable);
void gtimer1_soft_bke_set(em_enable_t soft_bk_enable);
void gtimer1_irq_init(em_enable_t irq_enable,em_gtimer_irq_t irq_type,void (*pfunc)());
void gtimer1_start(void);
void gtimer1_stop(void);

#endif

#ifdef UM2080F32_USING_GTIMER2

void gtimer2_count_init(uint8_t clk_src,uint16_t arr,uint16_t psc);
void gtimer2_pwm_init(uint8_t clk_src,uint16_t arr,uint16_t ccr,uint16_t psc);
void gtimer2_capture_init(uint8_t clk_src,uint16_t arr,uint16_t psc,uint8_t cap_div);
void gtimer2_bke_init(uint32_t bke_src,uint32_t bke_pol);
void gtimer2_moe_set(em_enable_t moe_enable);
void gtimer2_soft_bke_set(em_enable_t soft_bk_enable);
void gtimer2_irq_init(em_enable_t irq_enable,em_gtimer_irq_t irq_type,void (*pfunc)());
void gtimer2_start(void);
void gtimer2_stop(void);

#endif

#endif



