// +FHDR------------------------------------------------------------
//                 Copyright (c) 2022 SmartLogic.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : cpri_test_case44.c
// Author        : xinxin.li
// Created On    : 2023-03-22s
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------

#include "typedef.h"
#include "ucp_printf.h"
#include "ucp_utility.h"
#include "ape_csu.h"
#include "jesd_csu.h"
#include "jesd_timer.h"
#include "jesd_csu_nr_fdd.h"
#include "jesd_test.h"
#include "jesd_test_case41.h"
#include "rfm1_drv.h"

extern uint32_t antDataNr[245760]; 

extern uint32_t 		gJesdTestMode;
extern uint32_t			gJesdIOMode;
extern uint32_t			gJesdTFMode;

int32_t fh_data_init(void)
{
	gJesdTestMode = JESD_TEST_MODE;
	gJesdIOMode = JESD_CSU_CTRL;
	gJesdTFMode = FDD_MODE;
	debug_write((DBG_DDR_IDX_DRV_BASE+192), gJesdTestMode); // 0x300
	debug_write((DBG_DDR_IDX_DRV_BASE+193), gJesdIOMode);	// 0x304
	debug_write((DBG_DDR_IDX_DRV_BASE+194), gJesdTFMode); // 0x308
		
	jesd_tx_data_init();//init tx data

	return 0;
}

int32_t fh_drv_init(void)
{
	stFrontHaulDrvPara fhDrvPara;
	memset_ucp(&fhDrvPara, 0, sizeof(stFrontHaulDrvPara));
	
	fhDrvPara.protocolSel = PROTOCOL_JESD;
	fhDrvPara.rateOption = JESD_OPTION_204B;

	fronthaul_drv_cfg(&fhDrvPara);

	return 0;
}

int32_t fh_csu_test_init(void)
{
	//jesd_csu_init_nr_fdd();
	//jesd_csu_init_nr_fdd_slot0();
	jesd_csu_init_nr_fdd_rx_slot2();

	//jesd_pin_ctrl(MTIMER_JESD_RX0_ID);
	//jesd_pin_ctrl(MTIMER_JESD_TX0_ID);

	return 0;
}

void fh_test_case()
{
}

void fh_data_check(uint32_t times)
{
	return;
}

void jesd_tx_data_init()
{
	uint8_t antNum = JESD_NRFDD_ANT_NUM;
	uint8_t idAnt = 0;
	uint8_t idSlot = 0;
	uint32_t  srcAddr = 0;
	uint32_t  dstAddr = 0;
	uint32_t  dataLen = 0;
	uint16_t  samByteCnt = 4;
    uint32_t  slotSamCnt = JESD_NRFDD_SLOT_SAM_CNT;

	uint32_t cpyCnt = 0;

	memset_ucp((void*)JESD_NRFDD_TX_SLOT_EVEN_DATA_ADDR, 0, antNum*slotSamCnt*samByteCnt);
	memset_ucp((void*)JESD_NRFDD_TX_SLOT_ODD_DATA_ADDR, 0, antNum*slotSamCnt*samByteCnt);
	// valid data
	// IQ data	
	samByteCnt = 4;
	for (idAnt = 0; idAnt < antNum; idAnt++)
	{
		for (idSlot = 0; idSlot <= 1; idSlot++)
		{
			if (0 == idSlot)	// even slot
			{
				dataLen = samByteCnt * slotSamCnt;
				srcAddr = (uint32_t)(&antDataNr[0]); // + idAnt*slotSamCnt;
				dstAddr = JESD_NRFDD_TX_SLOT_EVEN_DATA_ADDR + idAnt*dataLen;
			}
			else if (1 == idSlot)	// odd slot
			{
				dataLen = samByteCnt * slotSamCnt;
				srcAddr = (uint32_t)(&antDataNr[0]); // + idAnt*slotSamCnt;
				dstAddr = JESD_NRFDD_TX_SLOT_ODD_DATA_ADDR + idAnt*dataLen;
			}
				//debug_write((DBG_DDR_IDX_DRV_BASE+256+(cpyCnt<<2)), (uint32_t)srcAddr); // 0x400
				//debug_write((DBG_DDR_IDX_DRV_BASE+256+((cpyCnt<<2)+1)), (uint32_t)dstAddr);
				//debug_write((DBG_DDR_IDX_DRV_BASE+256+((cpyCnt<<2)+2)), (uint32_t)dataLen);
//			memcpy_ucp((void*)dstAddr,(void*)srcAddr, dataLen);
			ape_csu_dma_1D_G2L_ch0ch1_transfer(srcAddr, dstAddr, dataLen, cpyCnt, 1);
			cpyCnt++;
		}
	}
	memset((void*)JESD_NRFDD_RX_SLOT_EVEN_DATA_ADDR, 0, antNum*slotSamCnt*samByteCnt);
	memset((void*)JESD_NRFDD_RX_SLOT_ODD_DATA_ADDR, 0, antNum*slotSamCnt*samByteCnt);
}



