/******************************************************************
 * @file  Receiver_Sync_Init.s.c
 * @brief:    [file description] 
 * @author: HUOHUO
 * @Date 2024年10月30日
 * COPYRIGHT NOTICE: ITTC
 * Change_date         Owner         Change_content
 * 2024年10月30日        HUOHUO       create file

*****************************************************************/
#include "receiver_sync_func.h"
#include "mem_def.h"


 /*!   
 * @brief:  Receiver任务启动前的初始化工作
 *          初始化全局参数，ddr查找表搬入SM
 * @author:  HUOHUO
 * @Date:  2024年10月30日
 */
void Receiver_Sync_Init()
{
    int32_t  ret;

    //DM0
    g_receiver_sync_table_param.ConfigByteCopy_CFG1_Offset = 0;
    g_receiver_sync_table_param.ConfigSlidingCorrelation_CFG2_Offset = g_receiver_sync_table_param.ConfigByteCopy_CFG1_Offset + RECEIVER_SYNC_ConfigByteCopy_CFG1_LENGTH;
    g_receiver_sync_table_param.ConfigSlidingCorrelationSecond_CFG3_Offset = g_receiver_sync_table_param.ConfigSlidingCorrelation_CFG2_Offset + RECEIVER_SYNC_ConfigSlidingCorrelation_CFG2_LENGTH;
    g_receiver_sync_table_param.ConfigSyncVer_CFG4_Offset = g_receiver_sync_table_param.ConfigSlidingCorrelationSecond_CFG3_Offset + RECEIVER_SYNC_ConfigSlidingCorrelationSecond_CFG3_LENGTH;
    //DM1

    //DM2

    //DM3

    //SM


    // //7. SPU查找表dma 搬移到ShareMemory，共257544Byte 包括：
    // //   1). 置信度表uint16_t Q_W_Lut[2016] (n-5)
    // //   2). 置信度最高的K'个置0的比特掩码表uint32_t I_BitMask_Lut[61056] (K-18,n-5)
    // //   3). 解三角交织边长T表 uint8_t T_Lut[8192] (E-1)
    // //   4). CRC6校验查找表uint8_t CRC6_Lut[256] (crc6check())
    // //   5). 长度≥36的ZC序列q值查找表 uint8_t Zc_q_Lut[840] (nPrbs-3,u,v)
    // //获取地址
    //     ret = osp_get_cfgfile("nr_puxch_lut_sm.dat",
    //                       (uint32_t *)&lutDdrAddr, 
    //                       (int32_t *)&(g_receiver_sync_table_param.pucch_lut_length));
    // g_receiver_sync_table_param.pucch_lut_sm_ptr = SM0_BASE;
    // ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)lutDdrAddr, 
    //                                    (uint64_t)g_receiver_sync_table_param.pucch_lut_sm_ptr, 
    //                                    g_receiver_sync_table_param.pucch_lut_length, 
    //                                    DMA_TAG_G2G, 
    //                                    1);
    // LOG_ERROR_S("test init\n");
    //9. 微码配置文件ddr地址初始化
    ret = osp_get_cfgfile("Receiver_Sync_cfg_dm0.dat",
                          (uint32_t *)&(g_receiver_sync_table_param.receiver_sync_config0_ddr_ptr),
                          (int32_t *)&(g_receiver_sync_table_param.receiver_sync_config0_length));

    if(-1 == ret)
    {
        LOG_ERROR_S("Receiver_Sync_cfg_dm0.dat not found!\n");
    }  
    // ret = osp_get_cfgfile("Receiver_Sync_cfg_dm1.dat",
    //                       (uint32_t *)&(g_receiver_sync_table_param.receiver_sync_config1_ddr_ptr),
    //                       (int32_t *)&(g_receiver_sync_table_param.receiver_sync_config1_length));

    // ret = osp_get_cfgfile("Receiver_Sync_cfg_dm2.dat",
    //                       (uint32_t *)&(g_receiver_sync_table_param.receiver_sync_config2_ddr_ptr),
    //                       (int32_t *)&(g_receiver_sync_table_param.receiver_sync_config2_length));
    // ret = osp_get_cfgfile("Receiver_Sync_cfg_dm3.dat",
    //                       (uint32_t *)&(g_receiver_sync_table_param.receiver_sync_config3_ddr_ptr),
    //                       (int32_t *)&(g_receiver_sync_table_param.receiver_sync_config3_length));

    // LOG_ERROR_S("init %d 0x%08x %d\n", ret, g_receiver_sync_table_param.receiver_sync_config0_ddr_ptr, g_receiver_sync_table_param.receiver_sync_config0_length);
    //状态机初始化
    g_receiver_sync_status_SM_ptr = (uint32_t)RECEIVER_SYNC2SYNC_FIRST_INF_ADDR;
    
    STORE_EX_W(&g_receiver_sync_status_SM_ptr->sync_status, SYNC_IDLE);
    STORE_EX_W(&g_receiver_sync_status_SM_ptr->frame_head_offset, 0);
    g_receiver_sync_cylic_buffer.buffer_head = 0;
    g_receiver_sync_cylic_buffer.buffer_tail = 0;
    g_receiver_sync_cylic_buffer.buffer_min = 0;
    g_receiver_sync_cylic_buffer.buffer_max = 61440*RECEIVER_SYNC_SYNC2SYMB_NUM_BUFFER;
    g_receiver_sync_cylic_buffer.buffer_ptr = RECEIVER_SYNC2SYMB_BUFFER0_ADDR;
    g_receiver_sync_cylic_buffer.buffer_sel = 0;

    //sync2symb buffer地址初始化
    sync2symb_data_buffer[0] = RECEIVER_SYNC2SYMB_BUFFER0_ADDR;
    sync2symb_data_buffer[1] = RECEIVER_SYNC2SYMB_BUFFER1_ADDR;
    sync2symb_data_buffer[2] = RECEIVER_SYNC2SYMB_BUFFER2_ADDR;
    sync2symb_data_buffer[3] = RECEIVER_SYNC2SYMB_BUFFER3_ADDR;
    sync2symb_data_buffer[4] = RECEIVER_SYNC2SYMB_BUFFER4_ADDR;
    sync2symb_data_buffer[5] = RECEIVER_SYNC2SYMB_BUFFER5_ADDR;
    g_proc_id = 0;

#ifdef RECV_DBG_DATA_TEST
uint32_t dbg_addr,dbg_len;
ret = osp_get_cfgfile("timedatasym.dat",   //timedatasym.dat    frame_0x610e0000_245760.dat
    (uint32_t *)&(dbg_addr),
    (int32_t *)&(dbg_len)); 

if(-1 == ret)
{
    //LOG_ERROR_S("frame_0x610e0000_245760.dat not found!\n");
    LOG_ERROR_S("timedatasym.dat not found!\n");
}    
else
{
    LOG_ERROR_S("INIT DBG DDR 0x%08x %d\n",dbg_addr,dbg_len);

    ape_csu_dma_1D_G2L_ch2ch3_transfer((uint64_t)(dbg_addr),
            (uint64_t)JESD_NRFDD_RX_SLOT_EVEN_DATA_ADDR,//第一次固定搬移到dm0
            dbg_len,
            DMA_TAG_G2G,
            1);
    ape_csu_dma_1D_G2L_ch2ch3_transfer((uint64_t)(dbg_addr),
            (uint64_t)JESD_NRFDD_RX_SLOT_ODD_DATA_ADDR,//第一次固定搬移到dm0
            dbg_len,
            DMA_TAG_G2G,
            1);      
}
#endif   

transform_para_init(0, 0, 0);

}
