
#include "receiver_symb_func.h"
#include "ByteSet.h"


void ChannelEst_Proc(
    uint32_t *param_ptr,
    int32_t  *temp_dm0_ptr,
    int32_t  *temp_dm1_ptr,
	int32_t  *temp_dm2_ptr,
	int32_t  *temp_dm3_ptr

)
{
    //局部变量定义

	receiver_sync2symb_t *proc_info = param_ptr; 

    int32_t  *cfg_addr;
    uint32_t time_data_ddr_ptr;
    uint32_t time_data_length;
    uint32_t time_data_dm0_ptr = ((((uint32_t)&temp_dm0_ptr[0] + 4095)>>12)<<12);
    uint32_t res_ptr = CHANNELEST_DATA_DDR_PTR;


	// Get Configuration and LUT
	uint32_t *ConfigAddr_channelEst = receiver_symb_config_dm0_ptr + RECEIVER_SYMB_FreOffEst_CFG1_LENGTH + RECEIVER_SYMB_FreOffComp_CFG2_LENGTH + RECEIVER_SYMB_FreOffCordic_CFG3_LENGTH;
	uint32_t *cfg_transform8 = receiver_symb_config_dm0_ptr + RECEIVER_SYMB_FREOFF_CFG_LENGTH + RECEIVER_SYMB_CHANNELEST_CFG_LENGTH + RECEIVER_SYMB_CHANNELEQU_CFG_LENGTH + RECEIVER_SYMB_DeTransform2_CFG8_LENGTH + RECEIVER_SYMB_DeTransform4_CFG9_LENGTH;
    	
	uint32_t *Pilot_orig_LUT = receiver_symb_config_dm2_ptr;
	uint32_t *InputPilotAddr = (uint32_t *)time_data_dm0_ptr;
	uint32_t *channelEstOutAddr = (uint32_t *)(temp_dm1_ptr);
	uint32_t *ConfigByteSet = receiver_symb_config_dm0_ptr + RECEIVER_SYMB_COMMEN_CFG_LENGTH;

	ByteSet(ConfigByteSet,0,MPU_ADDR(channelEstOutAddr),32768);
	SVRReg[0] = MPU_ADDR(ConfigByteSet);
	ByteSetAsm(SVRReg);

	int numSub = 2;
	int sliceIndex[2] = {1024,31268}; // position of pilot
    for(int subIndex=0;subIndex<numSub;subIndex++){

    	time_data_ddr_ptr = COMPENSATED_DATA_DDR_PTR + sliceIndex[subIndex]*4;
    	time_data_length = 1024;
    	ape_csu_task_lookup(DMA_TAG_G2L, 1);
    	ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)time_data_ddr_ptr,
                             (uint64_t)DM_TO_CSU_ADDR(InputPilotAddr),
                             time_data_length*4,
                             DMA_TAG_G2L,
                             0);
		
		// 新增了Pilot正变换

    	double thita[6];
		double *db_imag = proc_info->transform_para_real;
		double *db_real = proc_info->transform_para_imag;

		uint32_t *PilotTrans = temp_dm3_ptr;
		Transform((int)cfg_transform8,MPU_ADDR(Pilot_orig_LUT),MPU_ADDR(PilotTrans), 8, db_real, db_imag, 1);
    	SVRReg[0] = MPU_ADDR(cfg_transform8);

		WAIT_MPU_STOP;
		ape_csu_task_lookup(DMA_TAG_G2L, 1);
    	Transform8Asm(SVRReg);

    	ChannelEstImpl(ConfigAddr_channelEst, PilotTrans, InputPilotAddr, channelEstOutAddr + subIndex*4096);
    	WAIT_MPU_STOP;

    }

	ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR((uint32_t)channelEstOutAddr),
							(uint64_t)res_ptr,
							4096*2*4 ,
							DMA_TAG_L2G,
							1);

}

