#include "Frame_test.h"

void Test_Task()
{

    uint16_t start_slot = TIME_SLOT();
    g_time_start[0] = TIME_NS();
    RUN_CNT(TRACE_TESTTASK_ADDR, 0);
    
    uint32_t cur_sfn = get_rx_nr_sfn();
    uint32_t cur_slot = get_rx_nr_slot();
    int32_t  *transmitter_malloc_dm0_ptr;

    LOG_ERROR_S("cur_sfn:%d, cur_slot:%d\n", cur_sfn, cur_slot);

    TRACE(TRACE_TESTTASK_ADDR, 3, 1); 


   //0 空间回收
    dmalloc_trim(0, APE_DM0);
    dmalloc_trim(0, APE_DM1);
    dmalloc_trim(0, APE_DM2);
    dmalloc_trim(0, APE_DM3);

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


  int32_t  *output_data_ptr;// 最终输出数据地址(dm)

  output_data_ptr = transmitter_malloc_dm0_ptr;

  TRACE(TRACE_TESTTASK_ADDR, 3, 2); 
  uint32_t idx = 0;
  for(idx =0; idx < 100; idx++)
  {
	   *(output_data_ptr + idx) = cur_sfn * 100000 + cur_slot * 100 + idx;
  }

  uint32_t output_data_ptr_ddr_even = (uint32_t)JESD_NRFDD_RX_SLOT_ODD_DATA_ADDR;// 最终输出数据地址(ddr)
  uint32_t output_data_ptr_ddr_odd = (uint32_t)JESD_NRFDD_RX_SLOT_EVEN_DATA_ADDR ;// 最终输出数据地址(ddr)
//        ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR((uint32_t)output_data_ptr),
//                            output_data_ptr_ddr_even,
//                            temp_len_32,
//                            DMA_TAG_L2G,
//                            1);

  //LOG_ERROR_S("ddr_even:%08x, ddr_odd:%08x\n", output_data_ptr_ddr_even, output_data_ptr_ddr_odd);
  TRACE(TRACE_TESTTASK_ADDR, 3, 3); 

  uint32_t temp_len_32 = 7168;

  if(cur_slot % 2 == 1){
      ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR(output_data_ptr),
                          output_data_ptr_ddr_even,
                          temp_len_32,
                          DMA_TAG_L2G,
                          1);
  }else{
      ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR(output_data_ptr),
                          output_data_ptr_ddr_odd,
                          temp_len_32,
                          DMA_TAG_L2G,
                          1);
  }

    dfree_unit(transmitter_malloc_dm0_ptr, APE_DM0);

    TRACE(TRACE_TESTTASK_ADDR, 3, 4);
    RUN_CNT(TRACE_TESTTASK_ADDR, 1);
    TRACE_MAX(TRACE_TESTTASK_ADDR, 2, Time_offset(0) );
}
