#include "timer.h"
#include "led.h"

volatile int g_Tim2Array[(u16)eTimMax] = {0};
volatile u32 sys_time;
volatile u16 Timerx;

//定时器2初始化配置
void TIMER_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;//定义一个定时器结构体变量
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能定时器2,重要！！
	TIM_DeInit(TIM2);//将IM2定时器初始化位复位值
	TIM_InternalClockConfig(TIM2);//配置TIM2内部时钟
	TIM_BaseInitStructure.TIM_Period = 10000;//向上计数到1000后产生更新事件,计数值归零,1MS产生更新事件一次
											 //TIM_Period(TIM1_ARR)=1000,1000000/1000=1000us=1ms
	//TIM_BaseInitStructure.TIM_Prescaler = RCC_Clocks.PCLK1_Frequency / 1000000;//1MHz
	TIM_BaseInitStructure.TIM_Prescaler = 71;//自定义预分频系数,即APB1=72M,TIM1_CLK=72/72=1MHz 
	TIM_BaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割为0
	TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式 从0开始向上计数，计数到1000后产生更新事件
	//TIM_BaseInitStructure.TIM_RepetitionCounter = 0;//此参数只适合于TIM1和TIM8
	TIM_TimeBaseInit(TIM2, &TIM_BaseInitStructure);//根据指定参数初始化TIM时间基数寄存器
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清中断，以免一启用中断后立即产生中断 
	TIM_ARRPreloadConfig(TIM2, DISABLE);//禁止ARR 预装载缓冲器
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);//使能TIM2中断源 
	TIM_Cmd(TIM2, ENABLE);//TIM2总开关：开启 

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断组为2,NVIC中断分组2:2位抢占优先级,2位响应优先级
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//中断事件为TIM2
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//抢占优先级2
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;//响应优先级2
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//允许中断 
	NVIC_Init(&NVIC_InitStructure);//根据指定参数初始化中断寄存器
	
	sys_time=0;
}

//定时器2中断处理函数
void TIM2_IRQHandler(void)//单位10ms
{
	u16 i = 0;
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)//判断中断标志位是否为1:为1则中断响应啦,0则没有
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清楚中断挂起位
		sys_time++;
		Led_10mS_Conut();//定期器10mS调度任务
//		if(!ThreadLock)
//		{
//			NRF_Thread();//协议线程
//		}
		if(Timerx)
		{
			Timerx--;
		}
		for(i = 0; i < (u16)eTimMax; i++)
		{
			g_Tim2Array[i]++;
		}
	}
}

void TIM_Delay_Init(void)//延时定时器
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

	RCC_APB1PeriphClockCmd(DELAY_TIMER_CLK, ENABLE); 
	
	TIM_DeInit(DELAY_TIMER);//初始化位复位值
	TIM_InternalClockConfig(DELAY_TIMER);//配置内部时钟
	TIM_TimeBaseStructure.TIM_Period = 0xffff;//自动重装值;向上计数到0xffff后,计数值归零,计数到5000为5ms
	TIM_TimeBaseStructure.TIM_Prescaler =71;//预分频值,1Mhz的计数频率
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;//时钟分割为0
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
	TIM_TimeBaseInit(DELAY_TIMER, &TIM_TimeBaseStructure);//初始化TIMx的时间基数单位
	TIM_Cmd(DELAY_TIMER, DISABLE);
}

void NRF_DelayUs(u16 i)//1~65535us延时	  
{
	u16 TIMCounter = 0;
	TIM_SetCounter(DELAY_TIMER, TIMCounter);
	TIM_Cmd(DELAY_TIMER, ENABLE);
	while (TIMCounter<i)
	{
		TIMCounter = TIM_GetCounter(DELAY_TIMER);
	}
	TIM_Cmd(DELAY_TIMER, DISABLE);
}

//#ifdef DEBUG
//void TIM_MeasureStart(void)
//{
//	TIM3->CNT=0;
//	//TIM_SetCounter(TIM3, 0);
//	TIM_Cmd(TIM3,ENABLE );//使能定时器
//}

//void TIM_MeasureStop(void)
//{
//	u16 TIM_Count;
//	TIM_Cmd(TIM3,DISABLE );//关闭定时器
//	//TIM_Count=TIM_GetCounter(TIM3);//获取当前的值
//	TIM_Count=TIM3->CNT;//获取当前的值

//	printf("Used time:%d us\r\n",TIM_Count);
//}
//#endif

extern void Idle(void);
//注意事项:不要在中断中调用本函数,否则会存在重入问题.如果屏蔽了全局中断,则不要使用此函数
void Delay10Ms(u16 dly_10ms)
{
	Timerx=dly_10ms;
	while(Timerx)
	{
		Idle();
	}
}

//void NRF_DelayUs(u16 i)
//{
//	u16 k,j;
//	for(k=0;k<i;k++)
//		for(j=0;j<9;j++);
//}

//获取CPU运行时间,单位10ms.最长可以表示497天,如果连续运行时间超过这个数,则必须考虑溢出问题
u32 Get_RunTime(void)
{
	return sys_time;
}
