#include "jesd_orx_timer.h"

#include "typedef.h"
#include "ucp_mtimer.h"
#include "mtimer_com.h"
#include "mtimer_drv.h"
#include "ucp_utility.h"
#include "ucp_printf.h"
#include "ucp_js_ctrl.h"
#include "inter_vector.h"
#include "jesd204.h"
#include "ecs_rfm_dm_mgt.h"
#include "jesd_timer.h"
#include "jesd_csu.h"

//extern void isr_jesd_10ms_rx1(void);

extern int32_t jesd_sniffer_orx_csu_init();


extern stPhyScsPara* phyPara;
extern stMtimerIntStat 		gMtimerIntCnt[SCS_MAX_NUM];
//extern stJesdListPara gJesdRxListPara[JESD_CH_NUM][JESD_LIST_NUM];

int32_t jesd_orx_mtimer_init(int32_t nTmrId, int32_t nScsId)
{
	stMtimerIntStat* pMtimerInt = &gMtimerIntCnt[nTmrId];
	
	memset(pMtimerInt, 0, sizeof(stMtimerIntStat));

	int32_t nTimerMPara = SFN_PERIOD / do_read_volatile_short(&(phyPara[nScsId].slotPeriod));
	mtimer_para_init(MTIMER_JESD_RX1_ID, nScsId, nTimerMPara);

	jesd_orx_timer_init();

	return 0;
}

int32_t jesd_orx_timer_init(void)
{
    stop_jesd_orx_timer();
	jesd_orx_1pps_src_init(MTIMER_PP1S_SRC_TOD);
	//mtimer_clear_all_event(MTIMER_JESD_RX1_ID);
	set_jesd_orx_tmr_period();
	//set_jesd_orx_1pps_scratch();
	//set_jesd_orx_tmr_point(25000); // 25ms
	
	jesd_orx_pin_ctrl();
	
	return 0;
}

int32_t set_jesd_orx_tmr_period(void)
{
    EcsRfmDmLocalMgt_t* pEcsDmLocalMgt = get_ecs_rfm_dm_local_mgt();
	stMtimerPara* pMtimerPara = pEcsDmLocalMgt->pMtimerPara[MTIMER_JESD_RX1_ID];
	
	// 设置计数器溢出值
	uint32_t tempL = pMtimerPara->tmrMsPeriod * pMtimerPara->slotPeriod / 1000;
	uint32_t tempM = pMtimerPara->tddSlotNum;
	uint32_t tempH = SNIFFER_TMR_PERIOD / pMtimerPara->slotPeriod / pMtimerPara->tddSlotNum; // 10;

	set_mtimer_period(MTIMER_JESD_RX1_ID, tempL, tempM, tempH);
	//enable_mtimer_cevent_int(MTIMER_JESD_RX1_ID, MTMR_CEVENT_CNT14H, MTMR_INT_10ms);	// 10ms int

	return 0;
}

int32_t jesd_orx_1pps_src_init(uint8_t srcId)
{
	JECS_CTRL_CPRI_GMAC_PHY_INT &= ~(0x7 << 16);
	JECS_CTRL_CPRI_GMAC_PHY_INT |= (srcId << 16);

	return 0;
}

int32_t set_jesd_orx_1pps_scratch(void)
{
	set_mtimer_1pps_scratch(MTIMER_JESD_RX1_ID);

	return 0;
}

#if 0
int32_t set_jesd_orx_timer_int(void)
{
	//int32_t apeId = get_core_id();
	uint32_t intNum = 0;
	uint32_t baseIntNum = 0;
	irq_handler func10ms;
	
	baseIntNum = APC_JESD_RX1_SBU0_TMR_INTR0;
	
	// attach interrupt func
	int32_t ret = 0;
	intNum = baseIntNum + MTMR_INT_10ms;
	ret = smart_irq_free(intNum);
	if (0 != ret)
	{
		return -1;
	}

	intNum = baseIntNum + MTMR_INT_SLOT_OFFSET;
	ret = smart_irq_free(intNum);
	if (0 != ret)
	{
		return -1;
	}
	
	func10ms = isr_jesd_10ms_rx1;
	intNum = baseIntNum + MTMR_INT_10ms;
	ret = smart_irq_request(intNum, func10ms);
	if (0 != ret)
	{
		return -1;
	}

	return 0;
}
#endif
int32_t set_jesd_orx_tmr_point(int32_t usPoint)
{
	if ((SNIFFER_TMR_PERIOD) < usPoint)
	{
		return -1;
	}

    //EcsRfmDmLocalMgt_t* pEcsDmLocalMgt = get_ecs_rfm_dm_local_mgt();
	//stMtimerPara* pMtimerPara = pEcsDmLocalMgt->pMtimerPara[MTIMER_JESD_RX1_ID];
	
	int32_t tmr25Point = usPoint;

	set_mtimer_tmrpoint(MTIMER_JESD_RX1_ID, MTMR_TDD_OFFSET_10000, tmr25Point, MTIMER_MASK_62BIT);
	enable_mtimer_tmrpoint_int(MTIMER_JESD_RX1_ID, MTMR_TDD_OFFSET_10000, MTMR_INT_10ms);

	return 0;
}

void start_jesd_orx_timer(void)
{
	start_mtimer(MTIMER_JESD_RX1_ID);
}

void stop_jesd_orx_timer(void)
{
	stop_mtimer(MTIMER_JESD_RX1_ID);
}

int32_t jesd_orx_pin_ctrl(void)
{
	uint32_t tmrBaseAddr = JS_RX0_TMR_BASE + MTIMER_JESD_RX1_ID * 0x1000;

	do_write((tmrBaseAddr+MTMR_PIN_CTRL_REG), 0x1);				//CTRL_SEL
	do_write((tmrBaseAddr+MTMR_IO_CTRL_REG), (1<<(4+(MTIMER_JESD_RX1_ID>>1))));	//IO ctrl
	
    set_jesd_rf_state(JESD_ANT_ORX, GPIO_OFF);
    set_jesd_rf_state(JESD_RF_ORX, GPIO_OFF);
    set_jesd_rf_state(JESD_TRANS_ORX, GPIO_OFF);

	return 0;
}

int32_t phy_sniffer_start(uint32_t nOffsetUs)
{
    EcsRfmDmLocalMgt_t* pEcsDmLocalMgt = get_ecs_rfm_dm_local_mgt();
    JesdOrxPara_t* orx_para_ptr = pEcsDmLocalMgt->jesd_orx_para_ptr;
	
    if (0 == orx_para_ptr->orx_calldrv_cnt)
    {
        //jesd_sniffer_orx_csu_init(); // test
        jesd_orx_timer_init();
        
        //debug_write((DBG_DDR_IDX_DRV_BASE+48), gJesdRxListPara[1][0].listAddr); // 0xC0
	    //debug_write((DBG_DDR_IDX_DRV_BASE+49), gJesdRxListPara[1][0].listNodeNum); // 0xC4
    } 
	set_jesd_rf_state(JESD_ANT_ORX, GPIO_ON);
	set_jesd_rf_state(JESD_RF_ORX, GPIO_ON);
	set_jesd_rf_state(JESD_TRANS_ORX, GPIO_ON);

	debug_write((DBG_DDR_IDX_DRV_BASE+59), nOffsetUs); // 0xEC
    orx_para_ptr->orx_start_offset_us = nOffsetUs;
    if (0 != nOffsetUs)
	{
	    set_jesd_orx_tmr_point(nOffsetUs);
	}
	
    orx_para_ptr->orx_start_flag = 1;
    orx_para_ptr->orx_calldrv_cnt++;
    debug_write((DBG_DDR_IDX_DRV_BASE+56), orx_para_ptr->orx_calldrv_cnt); // 0xE0

    return 0;
}


