//                 Copyright (c) 2022 SmartLogic.
//                       ALL RIGHTS RESERVED
// -----------------------------------------------------------------
// Filename      : ape1_stc_timer_task.s.c
// Author        : 
// Created On    : 2022-10-24
// Last Modified :
// -----------------------------------------------------------------
// Description:
//
//
// -FHDR------------------------------------------------------------
#include "ucp_utility.h"
#include "ucp_tick.h"
#include "ucp_port.h"
#include "ucp_printf.h"
#include "phy_para.h"
#include "msg_transfer_layer.h"
#include "osp_type_def.h"
#include "spu_log.h"
#include "ape_mtimer.h"

uint32_t g_u32_ape1_stc_timer_num        = 0;   //(1) 0xB7E00204
uint32_t g_u32_ape1_stc_timer_alloc_err1 = 0;   //(2) 0xB7E00208
uint32_t g_u32_ape1_stc_timer_alloc_err2 = 0;   //(3) 0xB7E0020C

uint32_t gu32_ape1_stc_cnt = 0;
void ape1_stc_timer_task(void)
{
    uint32_t ape_id = get_core_id();
    uint16_t scs = 1;
    uint16_t sfn = get_tx_nr_sfn(scs);
    uint16_t slot = get_tx_nr_slot(scs);
    uint32_t value = (sfn << 16) + slot;

    uint32_t size = 10 * 1024;
    char* buf;
    uint32_t availableSize,offset;

    //UCP_PRINT_ERROR("ape[0x%x]_update:enter=%d, sfn[0x%x], slot[0x%x]", ape_id, ++gu32_ape1_stc_cnt, sfn, slot);

    uint16_t cu_flag = C_PLANE;
    HandleId_t handler;
    handler.port_id = get_ucp_port_id();
    handler.inst_id = 0;
    handler.type_id = CU_SPLIT;
    int32_t ret = msg_transfer_send_start(handler.value);

    ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
    if (SUCCESS != ret)
    {
        debug_write(DBG_DDR_COMMON_IDX(ape_id, 2), ++g_u32_ape1_stc_timer_alloc_err1);
        UCP_PRINT_ERROR("send_slot_indication C_PLANE ret[0x%08x]", ret);
        return;
    }
    uint32_t clockBegin;
    rdmcycle(&clockBegin);
    do_write(buf, value);
    do_write(buf+4, clockBegin);
    ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);

    cu_flag = U_PLANE;
    ret = msg_transfer_alloc_msg(handler.value, cu_flag, size, &buf, &availableSize, &offset);
    if (SUCCESS != ret)
    {
        debug_write(DBG_DDR_COMMON_IDX(ape_id, 3), ++g_u32_ape1_stc_timer_alloc_err2);
        UCP_PRINT_ERROR("send_slot_indication1 U_PLANE ret[0x%08x]", ret);
        return;
    }
    rdmcycle(&clockBegin);
    do_write(buf, value);
    do_write(buf+4, clockBegin);	
    ret = msg_transfer_send_msg(handler.value, cu_flag, (uint8_t *)buf, offset, size);	

    ret = msg_transfer_send_end(handler.value);

    g_u32_ape1_stc_timer_num++;
    debug_write(DBG_DDR_COMMON_IDX(ape_id, 1), g_u32_ape1_stc_timer_num);
    return ;
}

