#include "ape_cpri_timer.h"
#include "ucp_drv_common.h"
#include "ucp_cpri.h"
#include "ucp_param.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 "smartos.h"
#include "ucp_wdt.h"
#include "ape_mtimer.h"

void set_cpri_timer_ape_int(void)
{
	int32_t apeId = get_core_id();
	// tx slot
	uint32_t intNum = APC_CPRI_TMR_INTR0 + MTMR_INT_APE0_SLOT + apeId;
	int32_t ret = smart_irq_request(intNum, isr_cpri_ape_slot_offset);
	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_cpri_timer_ape_int(void)
{
	int32_t apeId = get_core_id();
	// tx slot
	uint32_t intNum = APC_CPRI_TMR_INTR0 + MTMR_INT_APE0_SLOT + apeId;
	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_cpri_ape_slot_offset(void)
{
	mtimer_ape_slot_callback(MTIMER_CPRI_ID);
}


