/******************************************************************
 * @file  Receiver_Bit_Task.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"


#define ADDR_ALIGN(addr, val)   ((((uint32_t)(addr) + (2<<(val)) - 1)>>(val))<<(val)) 


 /*!   
 * @brief:  Receiver任务处理流程
 * @author:  HUOHUO
 * @Date:  2024年4月20日
 */
void Receiver_Bit_Task(void* msg_ptr, uint32_t msg_len)
{
    g_time_start[0] = TIME_NS();
    RUN_CNT(TRACE_RECEIVER_BIT_ADDR, 0);
    

    int32_t  *receiver_bit_malloc_dm0_ptr;
    int32_t  *receiver_bit_malloc_dm1_ptr;
    int32_t  *receiver_bit_malloc_dm2_ptr;
    int32_t  *receiver_bit_malloc_dm3_ptr;
    uint32_t receiver_bit_param_ptr;

    int32_t *receiver_bit_temp_dm0_ptr;
    int32_t *receiver_bit_temp_dm1_ptr;
    int32_t *receiver_bit_temp_dm2_ptr;
    int32_t *receiver_bit_temp_dm3_ptr;
     //0 空间回收
    dmalloc_trim(0, APE_DM0);
    dmalloc_trim(0, APE_DM1);
    dmalloc_trim(0, APE_DM2);
    dmalloc_trim(0, APE_DM3);

    //1. DM0空间申请
    receiver_bit_malloc_dm0_ptr = dmemalign_unit(0x4000, 131072, APE_DM0); //申请了128KiB 首地址16k对齐
    if (NULLPTR == receiver_bit_malloc_dm0_ptr)
    {
        LOG_ERROR_S("rec bit DM0 fail\n");
        return;
    }

    //2. DM0空间分配
    //DM0第一段，调度信息
    receiver_bit_param_ptr = (uint32_t)receiver_bit_malloc_dm0_ptr;
    const uint32_t receiver_param_len = 50; //还没设计，随便设置的值50word
    if(0 < receiver_param_len)
    {
    ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)RECEIVER_BIT_CFG_BASE,
                                (uint64_t)DM_TO_CSU_ADDR(receiver_bit_param_ptr),
                                receiver_param_len * 4,
                                DMA_TAG_G2L,
                                0);  
    }
    //DM0第二段，微码相关空间
    receiver_bit_config_dm0_ptr = (uint32_t *)(ADDR_ALIGN(receiver_bit_param_ptr + receiver_param_len * 4, 12)); //起始地址4k对齐
    if(0 < g_receiver_bit_table_param.receiver_bit_config0_length)
    {
    ape_csu_dma_1D_G2L_ch2ch3_transfer((uint64_t)g_receiver_bit_table_param.receiver_bit_config0_ddr_ptr,
                                 (uint64_t)DM_TO_CSU_ADDR(receiver_bit_config_dm0_ptr),
                                 g_receiver_bit_table_param.receiver_bit_config0_length,
                                 DMA_TAG_G2L,
                                 0);    
    }
    //DM0第三段，堆空间
    receiver_bit_temp_dm0_ptr = (int32_t *)ADDR_ALIGN(receiver_bit_config_dm0_ptr +  \
        g_receiver_bit_table_param.receiver_bit_config0_length, 12);//起始地址4k对齐
    
    //3. DM3空间申请
    receiver_bit_malloc_dm3_ptr = dmemalign_unit(0x4000, 196608, APE_DM3);//申请了192KiB 首地址16k对齐
    //若空间申请失败，则释放已申请的空间，再退出任务
    if (NULLPTR == receiver_bit_malloc_dm3_ptr)
    {
        LOG_ERROR_S("rec bit DM3 fail\n");
        dfree_unit(receiver_bit_malloc_dm0_ptr, APE_DM0);
        return;
    }
    //DM3第一段，微码相关空间
    receiver_bit_config_dm3_ptr = (uint32_t*)receiver_bit_malloc_dm3_ptr;
    ape_csu_task_lookup(DMA_TAG_G2L, 1);
    if(0 < g_receiver_bit_table_param.receiver_bit_config3_length)
    {
    ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)g_receiver_bit_table_param.receiver_bit_config3_ddr_ptr,
                                (uint64_t)DM_TO_CSU_ADDR(receiver_bit_config_dm3_ptr),
                                g_receiver_bit_table_param.receiver_bit_config3_length,
                                DMA_TAG_G2L,
                                0);
    }
    //DM3第二段，堆空间
    receiver_bit_temp_dm3_ptr = (int32_t *)ADDR_ALIGN(receiver_bit_config_dm3_ptr + \
        g_receiver_bit_table_param.receiver_bit_config3_length, 14); //起始地址16k对齐
    //4. DM1空间申请
    receiver_bit_malloc_dm1_ptr = dmemalign_unit(0x4000, 131072, APE_DM1);//申请了128KiB 首地址16k对齐
    //若空间申请失败，则释放已申请的空间，再退出任务
    if (NULLPTR == receiver_bit_malloc_dm1_ptr)
    {
        LOG_ERROR_S("rec bit DM1 fail\n");
        dfree_unit(receiver_bit_malloc_dm3_ptr, APE_DM3);
        dfree_unit(receiver_bit_malloc_dm0_ptr, APE_DM0);
        return;
    }
    //DM1第一段，微码配置文件
    receiver_bit_config_dm1_ptr = (uint32_t *)receiver_bit_malloc_dm1_ptr;
    //调用DMA函数，将微码配置文件搬入DM1，DDR->DM1
    if(0 < g_receiver_bit_table_param.receiver_bit_config1_length)
    {
    ape_csu_dma_1D_G2L_ch2ch3_transfer((uint64_t)g_receiver_bit_table_param.receiver_bit_config1_ddr_ptr,
                             (uint64_t)DM_TO_CSU_ADDR(receiver_bit_config_dm1_ptr),
                             g_receiver_bit_table_param.receiver_bit_config1_length,
                             DMA_TAG_G2L,
                             0);
    }
    //DM1第二段，堆空间
    receiver_bit_temp_dm1_ptr = (int32_t *)ADDR_ALIGN(receiver_bit_config_dm1_ptr + \
        g_receiver_bit_table_param.receiver_bit_config1_length, 12); //起始地址4k对齐
    //5. DM2空间申请
    receiver_bit_malloc_dm2_ptr = dmemalign_unit(0x4000, 196608, APE_DM2);//申请了192KiB 首地址16k对齐
    //若空间申请失败，释放已申请的空间，再退出任务
    if (NULLPTR == receiver_bit_malloc_dm2_ptr)
    {
        LOG_ERROR_S("rec bit DM2 fail\n");
        dfree_unit(receiver_bit_malloc_dm3_ptr, APE_DM3);
        dfree_unit(receiver_bit_malloc_dm0_ptr, APE_DM0);
        dfree_unit(receiver_bit_malloc_dm1_ptr, APE_DM1);
        return;
    }
    //DM2第一段，微码配置文件
    receiver_bit_config_dm2_ptr = (uint32_t *)receiver_bit_malloc_dm2_ptr;
    
    //调用DMA函数，微码配置文件搬移，DDR->DM2
    ape_csu_task_lookup(DMA_TAG_G2L, 1);
    if(0 < g_receiver_bit_table_param.receiver_bit_config2_length)
    {
    ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)g_receiver_bit_table_param.receiver_bit_config2_ddr_ptr,
                             (uint64_t)DM_TO_CSU_ADDR(receiver_bit_config_dm2_ptr),
                             g_receiver_bit_table_param.receiver_bit_config2_length,
                             DMA_TAG_G2L,
                            1);
    }
    //DM2第二段，堆空间
    receiver_bit_temp_dm2_ptr = (int32_t *)ADDR_ALIGN(receiver_bit_config_dm2_ptr + \
        g_receiver_bit_table_param.receiver_bit_config2_length, 14); //起始地址16k对齐

    TRACE(TRACE_RECEIVER_BIT_ADDR, 3, 1); 

    //6. 计算流程
    Receiver_Bit_Proc(
        (uint32_t*)receiver_bit_param_ptr,
        receiver_bit_temp_dm0_ptr,
        receiver_bit_temp_dm1_ptr,
        receiver_bit_temp_dm2_ptr,
        receiver_bit_temp_dm3_ptr
    );
    TRACE(TRACE_RECEIVER_BIT_ADDR, 3, 99); 

    //8. 内存释放
    dfree_unit(receiver_bit_malloc_dm2_ptr, APE_DM2);
    dfree_unit(receiver_bit_malloc_dm1_ptr, APE_DM1);
    dfree_unit(receiver_bit_malloc_dm3_ptr, APE_DM3);
    dfree_unit(receiver_bit_malloc_dm0_ptr, APE_DM0);


    TRACE(TRACE_RECEIVER_BIT_ADDR, 3, 100);
    RUN_CNT(TRACE_RECEIVER_BIT_ADDR, 1);

    TRACE_MAX(TRACE_RECEIVER_BIT_ADDR, 2, Time_offset(0) );

    return;
}
