// +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_7ds2u.h"
#include "jesd_test.h"
#include "jesd_test_case45.h"
#include "rfm1_drv.h"

extern uint32_t antDataPre7[123008]; 
extern uint32_t antDataPost7[122752]; 

extern uint32_t 		gJesdTestMode;
extern uint32_t			gJesdIOMode;
//extern stJesdTimerPara	gJesdTmrPara;

int32_t fh_data_init(void)
{
	gJesdTestMode = JESD_TEST_MODE;
	gJesdIOMode = JESD_CSU_CTRL;
	debug_write((DBG_DDR_IDX_DRV_BASE+192), gJesdTestMode); // 0x300
	debug_write((DBG_DDR_IDX_DRV_BASE+193), gJesdIOMode);	// 0x304
		
	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;

	fronthaul_drv_cfg(&fhDrvPara);

	return 0;
}

int32_t fh_csu_test_init(void)
{
	if (JESD_CSU_CTRL == gJesdIOMode)
	{
	    jesd_csu_init_nr_7ds2u();
	    //jesd_csu_init_nr_7d2u_slot0();
	}
	else if (JESD_IO_CTRL == gJesdIOMode)
	{
		jesd_csu_init_nr_7ds2u_iomode();
	}

	jesd_sniffer_orx_csu_init();

	return 0;
}

void fh_test_case()
{
	//jesd_csu_start_nr_7ds2u();
}

void fh_data_check(uint32_t times)
{
	return;
}

void jesd_tx_data_init()
{
	uint8_t antNum = 4;
	uint8_t idAnt = 0;
	uint8_t idSlot = 0;
	uint8_t idSymbolBlock = 0;	// symbol0~6, symbol7~13
	uint32_t  srcAddr = 0;
	uint32_t  dstAddr = 0;
	uint32_t  dataLen = 0;
	uint16_t  samByteCnt = 4;
//	uint32_t  slotSamCnt = LONGCP_SAM_CNT+SHORTCP_SAM_CNT*13;
	uint32_t  f7SamCnt = LONGCP_SAM_CNT+SHORTCP_SAM_CNT*6;
	uint32_t  b7SamCnt = SHORTCP_SAM_CNT*7;

	uint32_t cpyCnt = 0;

	memset_ucp((void*)JESD_NR7DS2U_TX_SLOT_EVEN_F7SYMBOL_ADDR, 0, 4*(f7SamCnt)*samByteCnt);
	memset_ucp((void*)JESD_NR7DS2U_TX_SLOT_ODD_F7SYMBOL_ADDR, 0, 4*(f7SamCnt)*samByteCnt);
	memset_ucp((void*)JESD_NR7DS2U_TX_SLOT_EVEN_B7SYMBOL_ADDR, 0, 4*(b7SamCnt)*samByteCnt);
	memset_ucp((void*)JESD_NR7DS2U_TX_SLOT_ODD_B7SYMBOL_ADDR, 0, 4*(b7SamCnt)*samByteCnt);
	// valid data
	// IQ data	
	samByteCnt = 4;
	for (idAnt = 0; idAnt < antNum; idAnt++)
	{
	for (idSlot = 0; idSlot <= 1; idSlot++)
	{
		for (idSymbolBlock = 0; idSymbolBlock <= 1; idSymbolBlock++)
		{
			if ((0 == idSlot) && (0 == idSymbolBlock))	// even slot, symbol0~6
			{
				dataLen = samByteCnt * f7SamCnt;
				srcAddr = (uint32_t)(&antDataPre7[0]);
					dstAddr = JESD_NR7DS2U_TX_SLOT_EVEN_F7SYMBOL_ADDR + idAnt*dataLen;
			}
			else if ((0 == idSlot) && (1 == idSymbolBlock))	// even slot, symbol7~13
			{
				dataLen = samByteCnt * b7SamCnt;
				srcAddr = (uint32_t)(&antDataPost7[0]);
					dstAddr = JESD_NR7DS2U_TX_SLOT_EVEN_B7SYMBOL_ADDR + idAnt*dataLen;
			}
			else if ((1 == idSlot) && (0 == idSymbolBlock))	// odd slot, symbol0~6
			{
				dataLen = samByteCnt * f7SamCnt;
				srcAddr = (uint32_t)(&antDataPre7[0]);
					dstAddr = JESD_NR7DS2U_TX_SLOT_ODD_F7SYMBOL_ADDR + idAnt*dataLen;
			}
			else if ((1 == idSlot) && (1 == idSymbolBlock))	// odd slot, symbol7~13
			{
				dataLen = samByteCnt * b7SamCnt;
				srcAddr = (uint32_t)(&antDataPost7[0]);
					dstAddr = JESD_NR7DS2U_TX_SLOT_ODD_B7SYMBOL_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_NR7DS2U_RX_SLOTS_DATA_ADDR, 0, 4*8*SHORTCP_SAM_CNT*samByteCnt);
	memset((void*)JESD_NR7DS2U_RX_SLOT_EVEN_DATA_ADDR, 0, 4*(f7SamCnt+b7SamCnt)*samByteCnt);
	memset((void*)JESD_NR7DS2U_RX_SLOT_ODD_DATA_ADDR, 0, 4*(f7SamCnt+b7SamCnt)*samByteCnt);
}



