/***********************************************************************************
 * @Copyright (c)	:(C)2020, Qindao ieslab Co., Ltd
 * @FileName   	  :hc32_timer_driver.c
 * @Author       	:
 * @Version      	:V1.0
 * @Date         	:2020-7-1
 * @Description	:the function of the entity of system processor
 ************************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "UWater_timer1uS_driver.h"


/* Private variables ---------------------------------------------------------*/
static u32 timer_5ms_old_num[TIMER_5MS_MAX_SUM];
static u32 timer_5ms_set_num[TIMER_5MS_MAX_SUM];

static u32 timer_100ms_old_num[TIMER_100MS_MAX_SUM];
static u32 timer_100ms_set_num[TIMER_100MS_MAX_SUM];


/* Exported functions -------------------------------------------------------*/
/*************************************************
Function: void Timer1usInit(void)
Description: 微秒级定时器初始化
Input：
Return:
Others:
*************************************************/
void Timer1usInit()
{	
//	u32 tmpsmcr = 0;
//	u32 tmpcr1 = 0;
//	u32 tmpcr2;
//	u32 tmpsmcr3;
	u32 u32CntValue;
	
	#if TIME_1uS_Timer0
	stc_bt_mode0_cfg_t stcBtBaseCfg;    
	DDL_ZERO_STRUCT(stcBtBaseCfg);	
	Sysctrl_SetPeripheralGate(SysctrlPeripheralBaseTim, TRUE); //Base Timer外设时钟使能
	
	stcBtBaseCfg.enWorkMode = BtWorkMode0;                  //定时器模式
	stcBtBaseCfg.enCT       = BtTimer;                      //定时器功能，计数时钟为内部PCLK
	stcBtBaseCfg.enPRS      = BtPCLKDiv4;                 //PCLK/256
	stcBtBaseCfg.enCntMode  = Bt32bitFreeMode;               //自动重载16位计数器/定时器
	stcBtBaseCfg.bEnTog     = FALSE;
	stcBtBaseCfg.bEnGate    = FALSE;
	stcBtBaseCfg.enGateP    = BtGatePositive;
	Bt_Mode0_Init(TIM0, &stcBtBaseCfg);                     //TIM0 的模式0功能初始化
	
	u32CntValue = 0;
	Bt_M0_Cnt32Set(TIM0, u32CntValue);                      //设置计数初值
	
	Bt_M0_Run(TIM0);
	#endif
	
	#if TIME_1uS_Timer3
	stc_tim3_mode0_cfg_t stcTim3BaseCfg;	
	DDL_ZERO_STRUCT(stcTim3BaseCfg);	
	Sysctrl_SetPeripheralGate(SysctrlPeripheralTim3, TRUE); //timer3外设时钟使能
	
	stcTim3BaseCfg.enWorkMode = Tim3WorkMode0;              //定时器模式
	stcTim3BaseCfg.enCT       = Tim3Timer;                  //定时器功能，计数时钟为内部PCLK
	stcTim3BaseCfg.enPRS      = Tim3PCLKDiv16;              //PCLK/16
	stcTim3BaseCfg.enCntMode  = Tim332bitFreeMode;          //32位计数器/定时器
	stcTim3BaseCfg.bEnTog     = FALSE;
	stcTim3BaseCfg.bEnGate    = FALSE;
	stcTim3BaseCfg.enGateP    = Tim3GatePositive;
	Tim3_Mode0_Init(&stcTim3BaseCfg);                       //TIM3 的模式0功能初始化
	
	u32CntValue = 0;
	Tim3_M0_Cnt32Set(u32CntValue);
	
	Tim3_M0_Run(); 
	#endif
}
/*************************************************
Function:  u8 TimerIfSleep()
Description: 是否可以休眠
Input：
Return:
Others:
*************************************************/
u8 TimerIfSleep()
{
	return TRUE;
}
/*************************************************
Function:  u32 Timer1usGetTick(u8 timerNum)
Description: 获取当前计数器的数值
Input：
Return:
Others:
*************************************************/
u32 Timer1usGetTick()
{ 
	#if TIME_1uS_Timer0
	u32 b;
	b=Bt_M0_Cnt32Get(TIM0);
	return b ;
	#endif
	#if TIME_1uS_Timer3
	u32 b;
	b=Tim3_M0_Cnt32Get();
	return b ;
	#endif
}

/*************************************************
Function:void Timer1usWakeSleep()
Description: 微秒定时器唤醒
Input:
Return:
Others:
*************************************************/
void Timer1usWakeSleep()
{
	Timer1usInit();
}
/*************************************************
Function:void Timer1usPreSleep()
Description: 微秒定时器休眠前接口
Input:
Return:
Others:
*************************************************/
void Timer1usPreSleep()
{
	#if TIME_1uS_Timer0
	Bt_M0_Stop(TIM0);
	Sysctrl_SetPeripheralGate(SysctrlPeripheralBaseTim, FALSE);
	#endif
	#if TIME_1uS_Timer3
	Tim3_M0_Stop();
	Sysctrl_SetPeripheralGate(SysctrlPeripheralTim3, FALSE);
	#endif
}


/*************************************************
Function:void Set5msTimer(u8 timer_5ms_num,u16 timespan_5ms)
Description: 设置5ms定时器
Input:  
Return: 
Others: 
*************************************************/
void Set5msTimer(u8 timer_5ms_num,u16 timespan_5ms)
{
	if(timer_5ms_num < TIMER_5MS_MAX_SUM)
	{
		timer_5ms_set_num[timer_5ms_num] = (u32)timespan_5ms * 1000 * 5;
		timer_5ms_old_num[timer_5ms_num] = Timer1usGetTick();
	}
}

/*************************************************
Function: u16 Check5msTimer(u8 timer_5ms_num)
Description: 检查定时器当前值，如果为0则说明定时时间到
Input:  
Return: 
Others: 
*************************************************/
u8 Check5msTimer(u8 timer_5ms_num)
{
    u32 tmp_count = 0;
	if(timer_5ms_num < TIMER_5MS_MAX_SUM)
	{
		tmp_count = Timer1usGetTick();
	
		if(tmp_count >= timer_5ms_old_num[timer_5ms_num])//定时起止无溢出
		{
			tmp_count -= timer_5ms_old_num[timer_5ms_num];
		}
		else
		{
			tmp_count += (0xFFFFFFFF- timer_5ms_old_num[timer_5ms_num]);     
		}

		if(tmp_count >= timer_5ms_set_num[timer_5ms_num])
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	else
	{
		return 0;
	}
}

/*************************************************
Function:void void Set100msTimer(u8 timer_100ms_num,u16 timespan_100ms)
Description: 设置100ms定时器
Input:  
Return: 
Others: 
*************************************************/
void Set100msTimer(u8 timer_100ms_num,u16 timespan_100ms)
{
	if(timer_100ms_num < TIMER_100MS_MAX_SUM)
	{
		timer_100ms_set_num[timer_100ms_num] = (u32)timespan_100ms * 1000 * 100;
		timer_100ms_old_num[timer_100ms_num] = Timer1usGetTick();
	}
}

/*************************************************
Function: u16 Check100msTimer(u8 timer_100ms_num)
Description: 检查定时器当前值，如果为0则说明定时时间到
Input:  
Return: 
Others: 
*************************************************/
u8 Check100msTimer(u8 timer_100ms_num)
{
    u32 tmp_count = 0;
//	u32 tmp_count1 = 0;
	if(timer_100ms_num < TIMER_100MS_MAX_SUM)
	{
		tmp_count = Timer1usGetTick();
//		tmp_count1 = tmp_count;
		if(tmp_count >= timer_100ms_old_num[timer_100ms_num])//定时起止无溢出
		{
			tmp_count -= timer_100ms_old_num[timer_100ms_num];
		}
		else
		{
			tmp_count += (0xFFFFFFFF- timer_100ms_old_num[timer_100ms_num]);     
		}

		if(tmp_count >= timer_100ms_set_num[timer_100ms_num])
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	else
	{
		return 0;
	}
}

/*************************************************
Function: u32 Get100msTimer(u8 timer_100ms_num)
Description: 获取定时开始到当前的时间
*************************************************/
u32 Get100msTimer(u8 timer_100ms_num)
{
   u32 tmp_count = 0;
	if(timer_100ms_num < TIMER_100MS_MAX_SUM)
	{
		tmp_count = Timer1usGetTick();
		if(tmp_count >= timer_100ms_old_num[timer_100ms_num])//定时起止无溢出
		{
			tmp_count -= timer_100ms_old_num[timer_100ms_num];
		}
		else
		{
			tmp_count += (0xFFFFFFFF- timer_100ms_old_num[timer_100ms_num]);     
		}
		return tmp_count;
	}
	else
	{
		return 0;
	}
}

