#ifndef _STC_TIMER_H_
#define _STC_TIMER_H_

#include "ucp_drv_common.h"

#define TDD_TMR_POINT_MAX       (200)   //(500)     // 1个TDD周期内，可设置的最多定时点个数
#define TDD_SLOT_NUM_MAX        10

#define STC_RT_PALLADIUM		100000000
#define STC_RT					1000000000

typedef struct _tagStcTimerPoint
{
	uint32_t pointVal;
	uint32_t lastFlag;
}stStcTimerPoint;

typedef struct _tagStcTimerPara
{
	uint32_t pClk;
	uint32_t R;
	uint32_t N;
	uint32_t N1;
	uint32_t A;
	uint32_t C;
	uint32_t usR;								// us的clk
	uint32_t ctwVal;
	stStcTimerPoint tmrPoint[TDD_TMR_POINT_MAX];
	uint32_t tmrPointNum[TDD_SLOT_NUM_MAX];	// 1个tdd周期内，设置的各个时隙的定时点个数
	uint32_t tmrPointTotalNum;				// 1个tdd周期内，设置的定时点总数
	uint32_t slotTddNumMax;
	uint32_t slotPointNum;
	uint32_t curSlotNum;						// current slot id
	uint32_t curSlotTmrPointId;				// current timer point id of this slot
	uint32_t ctwIntCnt;
	uint32_t todIntCnt;
	uint32_t todLastStcCnt;					// 计算tod周期精度使用，记录tod中断来时的时间
	uint32_t ctwSetFinished;					// 定时点设置完成
	uint32_t ctwStartFlag;					// start set ctw value
	uint32_t apeSlotCallCnt;				// 时隙中断中调用定时点任务计数
}stStcTimerPara;

/*
stc的选择信号;
3’b000:表示选择gmac_pps;
3’b001:表示选择gmac1_pps;
3’b011:表示选择cpri_gmac_pps;
3’b100:表示选择pet_pps;
3’b101:表示选择gps_one_pps_out;
*/
typedef enum _tagStcPP1sSrc{
	STC_PP1S_SRC_GMAC	= 0,
	STC_PP1S_SRC_GMAC1	= 1,
	STC_PP1S_SRC_CPRI	= 3,
	STC_PP1S_SRC_PET	= 4,
	STC_PP1S_SRC_GPS	= 5
}numStcPP1sSrc;

void rfm_stc_init();

uint32_t stc_pclk_init();

// 计算最大公约数
int32_t gcd_stein(uint32_t x, uint32_t y);

void stc_timer_init(uint32_t pClk, uint32_t valR);
void stc_timer_tod_init(uint32_t pClk);
void stc_pp1s_src_init(uint8_t srcId);
void stc_pp1s_out_set();

void stc_timer_para_init(uint32_t pClk, uint32_t valR);
void stc_timer_local_init();
void stc_timer0_para_init();
void stc_timer1_para_init();

void stc_timer_sync(int32_t nScsId);
void stc_timer_set_tmrpoint(stStcTimerPoint* pTmrPoint);

void stc_timer_set_next_ctw(int32_t setFlag);

void stc_timer_ctwint_init();

void stc_timer_todint_init();

void stc_timer_ctwint_enable();
void stc_timer_ctwint_disable();

void isr_stc_timer_int();

void isr_stc_tod_int();

void stc_tod_para_init(uint32_t pClk);
void stc_timer_set_ctw(uint32_t val);


#endif
