
#include "receiver_symb_func.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 + g_receiver_symb_table_param.ChannelEst_CFG4_Offset;
	uint32_t *cfg_transform8 = receiver_symb_config_dm0_ptr + g_receiver_symb_table_param.Transform8_CFG14_Offset;
    	
	int32_t *Pilot_orig_LUT = receiver_symb_config_dm2_ptr;
	uint32_t *InputPilotAddr = (uint32_t *)time_data_dm0_ptr;
	uint32_t *channelEstOutAddr = (uint32_t *)((((uint32_t)temp_dm1_ptr + 4096)>>12)<<12);
	uint32_t *ConfigByteSet = receiver_symb_config_dm0_ptr + g_receiver_symb_table_param.ByteSet_CFG15_Offset;



	int numSub = 2;
	int sliceIndex[2] = {1024,31268}; // position of pilot
	ByteSet(ConfigByteSet,0,MPU_ADDR(channelEstOutAddr),32768);
	SVRReg[0] = MPU_ADDR(ConfigByteSet);
	ByteSetAsm(SVRReg);
	WAIT_MPU_STOP;
    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,
            1);

		WAIT_MPU_STOP;

		// 新增了Pilot正变换

    	//double thita[6];
		// double *db_imag = proc_info->transform_para_real;
		// double *db_real = proc_info->transform_para_imag;
		double db_real[4] = {-0.3827, 0.8660, -0.7071, 0.5};
        double db_imag[4] = {0.9239, 0.5,    -0.7071, 0.8660};

		int32_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);
    	Transform8Asm(SVRReg);
		WAIT_MPU_STOP;

		// TODO 暂用Pilot_orig_LUT， 此处应使用PilotTrans替换Pilot_orig_LUT  20250529
		for(int i=0;i<32;i++){
			Pilot_orig_LUT[i+1024] = Pilot_orig_LUT[i];
			InputPilotAddr[i+1024] = InputPilotAddr[i];
		}

		ChannelEst(ConfigAddr_channelEst, 
				   MPU_ADDR(Pilot_orig_LUT),
				   MPU_ADDR(InputPilotAddr),
				   MPU_ADDR(channelEstOutAddr + subIndex*4096));

		ape_csu_task_lookup(DMA_TAG_G2L, 1);
		SVRReg[0] = MPU_ADDR(ConfigAddr_channelEst);
		channelEstAsm(SVRReg);

    	WAIT_MPU_STOP;

    }
	//保证最后一次微码计算结束再搬出
	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);

}

