/******************************************************************
 * @file  Receiver_Bit_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_bit_func.h"


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

    //DM0
    g_receiver_bit_table_param.LLRDemodulation_CFG1_Offset = 0;
    g_receiver_bit_table_param.ByteCopy_CFG1_Offset = g_receiver_bit_table_param.LLRDemodulation_CFG1_Offset + RECEIVER_LLRDemodulation_CFG1_LENGTH;
    g_receiver_bit_table_param.ByteShift_CFG1_Offset = g_receiver_bit_table_param.ByteCopy_CFG1_Offset + RECEIVER_ByteCopy_CFG1_LENGTH;
    g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG1_Offset = g_receiver_bit_table_param.ByteShift_CFG1_Offset + RECEIVER_ByteShift_CFG1_LENGTH;
    g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT1_Offset = g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG1_Offset + RECEIVER_LdpcDecBG1AllZc_CFG1_LENGTH;
    g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG2_Offset = g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT1_Offset + RECEIVER_LdpcDecBG1AllZc_LUT1_LENGTH;
    //DM1
    g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT2_Offset = 0;
    g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG3_Offset = g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT2_Offset + RECEIVER_LdpcDecBG1AllZc_LUT2_LENGTH;
    //DM2
    g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT3_Offset = 0;
    g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG4_Offset = g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT3_Offset + RECEIVER_LdpcDecBG1AllZc_LUT3_LENGTH;
    //DM3
    g_receiver_bit_table_param.ByteCopy_CFG2_Offset = 0;
    g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG5_Offset = g_receiver_bit_table_param.ByteCopy_CFG2_Offset + RECEIVER_ByteCopy_CFG2_LENGTH;
    //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_bit_table_param.pucch_lut_length));
    // g_receiver_bit_table_param.pucch_lut_sm_ptr = SM0_BASE;
    // ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)lutDdrAddr, 
    //                                    (uint64_t)g_receiver_bit_table_param.pucch_lut_sm_ptr, 
    //                                    g_receiver_bit_table_param.pucch_lut_length, 
    //                                    DMA_TAG_G2G, 
    //                                    1);

    //9. 微码配置文件ddr地址初始化
    ret = osp_get_cfgfile("Receiver_Bit_cfg_dm0.dat",
                          (uint32_t *)&(g_receiver_bit_table_param.receiver_bit_config0_ddr_ptr),
                          (int32_t *)&(g_receiver_bit_table_param.receiver_bit_config0_length));
    if(-1 == ret)
    {
        LOG_ERROR_S("Receiver_Bit_cfg_dm0.dat not found!\n");
    }  
    ret = osp_get_cfgfile("Receiver_Bit_cfg_dm1.dat",
                          (uint32_t *)&(g_receiver_bit_table_param.receiver_bit_config1_ddr_ptr),
                          (int32_t *)&(g_receiver_bit_table_param.receiver_bit_config1_length));
    if(-1 == ret)
    {
        LOG_ERROR_S("Receiver_Bit_cfg_dm1.dat not found!\n");
    }  
    ret = osp_get_cfgfile("Receiver_Bit_cfg_dm2.dat",
                          (uint32_t *)&(g_receiver_bit_table_param.receiver_bit_config2_ddr_ptr),
                          (int32_t *)&(g_receiver_bit_table_param.receiver_bit_config2_length));
    if(-1 == ret)
    {
        LOG_ERROR_S("Receiver_Bit_cfg_dm2.dat not found!\n");
    }  
    ret = osp_get_cfgfile("Receiver_Bit_cfg_dm3.dat",
                          (uint32_t *)&(g_receiver_bit_table_param.receiver_bit_config3_ddr_ptr),
                          (int32_t *)&(g_receiver_bit_table_param.receiver_bit_config3_length));
    if(-1 == ret)
    {
        LOG_ERROR_S("Receiver_Bit_cfg_dm3.dat not found!\n");
    }  
    //10.输入测试数据
	uint32_t timedata_ptr;
	int32_t timedata_len;
    ret = osp_get_cfgfile("timedata.dat",
                          (uint32_t *)&(timedata_ptr),
                          (int32_t *)&(timedata_len));
    if(-1 == ret)
    {
        LOG_ERROR_S("timedata.dat not found!\n");
    }  
   uint32_t gain_mmse_test_ptr;
   int32_t gain_mmse_test_len;
   ret = osp_get_cfgfile("gain_test.dat",
   					      (uint32_t *)&(gain_mmse_test_ptr),
						  (int32_t *)&(gain_mmse_test_len));
    if(-1 == ret)
    {
        LOG_ERROR_S("gain_test.dat not found!\n");
    }  
    int* param_ptr = (int *)RECEIVER_BIT_CFG_BASE;
	STORE_EX_W(param_ptr, timedata_ptr);
	STORE_EX_W((param_ptr+1), timedata_len);
	STORE_EX_W((param_ptr+2), gain_mmse_test_ptr);
	STORE_EX_W((param_ptr+3), gain_mmse_test_len);
}
