/*C*****************************************************************************
* NAME:         time_count.c
*-------------------------------------------------------------------------------
* RELEASE:
* REVISION:     1.0

*******************************************************************************/
/*_____ I N C L U D E S ______________________________________________________*/

#include "config.h" //全局声明

void delay(unsigned int Counter)
{
	unsigned int i;
	for (; Counter != 0; Counter--)
	{
		i = 55; // 经示波器测试，精度1%
		while (i--)
			;
	}
}
/*_____ M A C R O S __________________________________________________________*/

/****************************************************
函数名：TimerTask
功能  ：定时器初始化（主定时器，timer2）1ms
参数  ：无
返回值：无
****************************************************/
void Timer_Init(void)
{
	u8 i = 0, j = 0;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 开TIM2时钟
	/*TIM2*/
	TIM_TimeBaseStructure.TIM_Prescaler = 168 - 1;				// 分频系数1000k
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数方式
	TIM_TimeBaseStructure.TIM_Period = 1000 - 1;				// 计数上限 10ms 100hz
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;		// 时钟选裕：系统时钟
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);				// TIM2时基初始化
	/***********************/
	TXHData.Timer_Num_Top = 0; // 初始化定时器数目
	/***************初始化定时器组*******************/
	for (i = 0; i < 4; i++)
	{
		TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MDRestartTimer[i];
		TXHData.Timer_Num_Top++;
		TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MBCountTimer[i];
		TXHData.Timer_Num_Top++;
		TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MBCountRXIDLETimer[i];
		TXHData.Timer_Num_Top++;
		TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MD_FramContinuityTimer[i];
		TXHData.Timer_Num_Top++;
		for (j = 0; j < 16; j++)
		{
			TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MD_Port_Info[i].GunInfo[j].LTDelayTimer;
			TXHData.Timer_Num_Top++;
			TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MD_Port_Info[i].FatherDevice[j].LTDelayTimer;
			TXHData.Timer_Num_Top++;
		}
	}
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.POSCutTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.HeartbeatTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.POS_RS232Timer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.ReSendDataTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.GPSDataTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.AnalogTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.CTIMEReadTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MD_LORA_SlaveTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.KeyFindTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.POSReLinkTimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MD_MultiplexRXIDLETimer;
	TXHData.Timer_Num_Top++;
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MD_MultiplexTimer;
	TXHData.Timer_Num_Top++;
	for (i = 0; i < 5; i++)
	{
		TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i];
		TXHData.Timer_Num_Top++;
	}
	TXHData.Timer_Buff[TXHData.Timer_Num_Top] = &TXHData.DelayDetectionTimer;
	TXHData.Timer_Num_Top++;
	TIM_Cmd(TIM2, ENABLE);					   // 打开TIM2
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //
	Timer_NVIC_Configuration();
}
/****************************************************
函数名：Timer_NVIC_Configuration
功能  ：定时器中断配置timer2
参数  ：无
返回值：无
****************************************************/
void Timer_NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure; // 定义中断配置结构体

	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}
/****************************************************
函数名：SYSTimer_IRQHandler
功能  ：系统定时器中断（主定时器，timer2）10ms
参数  ：无
返回值：无
****************************************************/
void SYSTimer_IRQHandler(void)
{
	u8 pTimer = 0;
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
	{
		for (pTimer = 0; pTimer < TXHData.Timer_Num_Top; pTimer++) // 轮训定时器队列
		{
			if (TXHData.Timer_Buff[pTimer]->Init == 1) // 定时器初始化标志
			{
				TXHData.Timer_Buff[pTimer]->Init = 0;
				TXHData.Timer_Buff[pTimer]->ON_OFF = 0;
				TXHData.Timer_Buff[pTimer]->Flag = 0;
				TXHData.Timer_Buff[pTimer]->TimerCount = 0;
			}
			else if (TXHData.Timer_Buff[pTimer]->ON_OFF == 1 && TXHData.Timer_Buff[pTimer]->Flag == 0) // 定时器开启状态,未到时
			{
				TXHData.Timer_Buff[pTimer]->TimerCount += 1;											 // 如果定时器开启，计数值累加
				if (TXHData.Timer_Buff[pTimer]->TimerCount >= TXHData.Timer_Buff[pTimer]->TimerCountMax) // 判断定时器是否到时
				{
					TXHData.Timer_Buff[pTimer]->Flag = 1;		// 置位定时器到时标志
					if (TXHData.Timer_Buff[pTimer]->Cycle == 1) // 定时器循环,清除定时器计数值
					{
						TXHData.Timer_Buff[pTimer]->TimerCount = 0;
					}
					else // 到时不循环，初始化定时器
					{
						TXHData.Timer_Buff[pTimer]->ON_OFF = 0;
						TXHData.Timer_Buff[pTimer]->TimerCount = 0;
						TXHData.Timer_Buff[pTimer]->Cycle = 0;
					}
				}
			}
		}
	}
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);
}
