
#include "receiver_symb_func.h"
#include "ape_common.h"


void FreOff_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
)
{
    //局部变量定义
    int32_t  *cfg_addr;
    uint32_t time_data_ddr_ptr;
    uint32_t time_data_length;
    uint32_t time_data_dm3_ptr = ((((uint32_t)&temp_dm3_ptr[0] + 4095)>>12)<<12);
    uint32_t res_ptr = RECEIVER_SYMB_OUT;
    uint32_t time0, time1;

    // Read Global Buff
    time_data_ddr_ptr = (uint32_t)*param_ptr;
    time_data_length = 2048;
    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(time_data_dm3_ptr),
                             time_data_length*4,
                             DMA_TAG_G2L,
                             0);

    // Get Configuration
    uint32_t *ConfigAddr_est = receiver_symb_config_dm0_ptr;
    uint32_t *ConfigAddr_comp = receiver_symb_config_dm0_ptr + RECEIVER_SYMB_FreOffEst_CFG1_LENGTH;
    uint32_t *ConfigAddr_cordic = receiver_symb_config_dm0_ptr + RECEIVER_SYMB_FreOffEst_CFG1_LENGTH + RECEIVER_SYMB_FreOffComp_CFG2_LENGTH;

    // Get CP and Pilot
    uint32_t *InputCPAddr = (uint32_t *)time_data_dm3_ptr;
    uint32_t *InputPilotAddr = InputCPAddr + 1024;

	uint32_t *freEstOutAddr = (uint32_t *)(temp_dm1_ptr + 0x0020);
    time0 = Time_offset(0);
    freOffEstImpl(ConfigAddr_est, InputCPAddr, InputPilotAddr, freEstOutAddr);
    time1 = Time_offset(0);
    TRACE_MAX(TRACE_RECEIVER_SYMB_ADDR, 9, time1-time0);
    time0 = time1;
    // Low-pass Filter
    if(storedfreoffestvalue > 600000){
    	storedfreoffestvalue = freEstOutAddr[0];
    }
    else{
    	 storedfreoffestvalue =  0.7*storedfreoffestvalue + 0.3*freEstOutAddr[0];
    }
    freEstOutAddr[0] = storedfreoffestvalue;

    // Frequency Offset Compensate
    uint32_t *available_ptr_dm2 = temp_dm2_ptr;
    uint32_t *available_ptr_dm3 = temp_dm3_ptr;
    freOffCompImpl(
			ConfigAddr_comp,
			ConfigAddr_cordic,
			freEstOutAddr,
			(uint32_t *)time_data_ddr_ptr,
			(uint32_t *)res_ptr,
			available_ptr_dm2,
			available_ptr_dm3
		);
    time1 = Time_offset(0);
    TRACE_MAX(TRACE_RECEIVER_SYMB_ADDR, 10, time1-time0);

}



//    //计算结果搬移到SM
//
//    temp_u32 = (uint32_t)(((time_data_length +7)>>3)<<3);//计算byte数
//    WAIT_MPU_STOP;
//    ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR(time_data_dm0_ptr),
//                            (uint64_t)res_ptr,
//                            temp_u32,
//                            DMA_TAG_L2G,
//                            1);
//
//    return;
//}
