#include "ape_jesd_timer.h"
#include "jesd204.h"
#include "ucp_param.h"
#include "ucp_drv_common.h"
#include "mtimer_com.h"
#include "ape_cnt.h"
#include "ucp_utility.h"
#include "ucp_printf.h"
#include "phy_para.h"
#include "stc_timer.h"
#include "ucp_wdt.h"
#include "smartos.h"
#include "ape_mtimer.h"


void set_jesd_timer_ape_int(int32_t nTmrId)
{
	int32_t apeId = get_core_id();
	uint32_t intNum = 0;
	irq_handler pfunc = NULL;
	if (MTIMER_JESD_RX0_ID == nTmrId)
	{
		intNum = APC_JESD_RX0_SBU0_TMR_INTR0 + MTMR_INT_APE0_SLOT + apeId;
		pfunc = isr_jesd_rx0_ape_slot;
	}
	else if (MTIMER_JESD_RX1_ID == nTmrId)
	{
		intNum = APC_JESD_RX1_SBU0_TMR_INTR0 + MTMR_INT_APE0_SLOT + apeId;
		pfunc = isr_jesd_rx1_ape_slot;
	}
	else
	{
		return;
	}
	int32_t ret = smart_irq_request(intNum, pfunc);
	if (0 != ret)
	{
		UCP_PRINT_ERROR("attach int num: 0x%x error. errno = 0x%x. \r\n", intNum, ret);
		// debug write
		debug_write(DBG_DDR_ERR_IDX(apeId, 0), ret);
	}

	//int tmrId = CPRI_TMR_APE0_TXSLOT + (apeId<<1);
	//UCP_PRINT_LOG("intNum = 0x%x, int set val: 0x%x. \r\n", intNum, (1<<tmrId) | (1<<(tmrId+1)));
}

int32_t clr_jesd_timer_ape_int(int32_t nTmrId)
{
	int32_t apeId = get_core_id();
	uint32_t intNum = 0;
	if (MTIMER_JESD_RX0_ID == nTmrId)
	{
		intNum = APC_JESD_RX0_SBU0_TMR_INTR0 + MTMR_INT_APE0_SLOT + apeId;
	}
	else if (MTIMER_JESD_RX1_ID == nTmrId)
	{
		intNum = APC_JESD_RX1_SBU0_TMR_INTR0 + MTMR_INT_APE0_SLOT + apeId;
	}
	else
	{
		return -1 ;
	}
	int32_t ret = smart_irq_free(intNum);
	if (0 != ret)
	{
		UCP_PRINT_ERROR("detach int num: 0x%x error. errno = 0x%x. \r\n", intNum, ret);
		// debug write
		debug_write(DBG_DDR_ERR_IDX(apeId, 0), ret);
		return -1;
	}
	return 0;
}

void isr_jesd_rx0_ape_slot(void)
{
	mtimer_ape_slot_callback(MTIMER_JESD_RX0_ID);
}

void isr_jesd_rx1_ape_slot(void)
{
	mtimer_ape_slot_callback(MTIMER_JESD_RX1_ID);
}


