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

uint32_t g_u32_stc_timer_tick     = 0;  //(34)  0xB7E00088
uint32_t g_u32_stc_timer_tick_err = 0;  //(35)  0xB7E0008C
uint32_t g_u32_alloc_err = 0;           //(36)  0xB7E00090
uint32_t g_u32_send_err  = 0;           //(37)  0xB7E00094
uint32_t g_u32_stc_time_sum = 0;        //(38)  0xB7E00098
uint32_t g_u32_send_ok      = 0;        //(39)  0xB7E0009C

void ape0_stc_timer_task(void)
{
    uint32_t ape_id = get_core_id();
    int  ret = 0;
    char *addr = NULL;
    uint32_t slotOffset = get_tx_nr_slot_cycle();

    g_u32_stc_time_sum++;

    uint32_t clockNow;
    uint32_t clockOffset;
    rdmcycle(&clockNow);
    debug_write(((DBG_DDR_IDX_DRV_BASE+7168) + (g_u32_stc_time_sum&0x1FF) + (ape_id << 9)), clockNow);	// 0x7000
    debug_write(DBG_DDR_COMMON_IDX(ape_id,(64+(g_u32_stc_time_sum&0xF))), slotOffset); 

    if (0 == g_u32_stc_timer_tick)
    {
        g_u32_stc_timer_tick = clockNow;
    }
    else
    {
        if (clockNow > g_u32_stc_timer_tick)
        {
            clockOffset = clockNow - g_u32_stc_timer_tick;
            if ((clockOffset > TIMER_OFFSET_MAX) || (clockOffset < TIMER_OFFSET_MIN))
            {
                g_u32_stc_timer_tick_err++;
                UCP_PRINT_ERROR("ape[%d] stc_timer tick err[%d], cnt[%d]",clockOffset, g_u32_stc_timer_tick_err);
            }
        }
        g_u32_stc_timer_tick = clockNow;
    }
    debug_write(DBG_DDR_COMMON_IDX(ape_id, 34), g_u32_stc_timer_tick);
    debug_write(DBG_DDR_COMMON_IDX(ape_id, 35), g_u32_stc_timer_tick_err);


    addr = osp_alloc_msg(TEST_MSG_SIZE);
    if (NULL == addr)
    {
        g_u32_alloc_err++;
        debug_write(DBG_DDR_COMMON_IDX(ape_id, 36), g_u32_alloc_err);
        return ;
    }

    do_write(addr, g_u32_stc_time_sum);
    do_write((addr+4), 0xA001B002);

    ret = osp_send_msg((uint32_t)(addr),
                        TEST_MSG_SIZE,
                        UCP4008_KERNEL_INTER,
                        ape_id,   // src que id
                        ape_id+1,   // dst que id
                        40,         // src task id
                        43);        // dst task id

    if (0 != ret)
    {
        g_u32_send_err++;
        debug_write(DBG_DDR_COMMON_IDX(ape_id, 37), g_u32_send_err);
        return ;
    }
    g_u32_send_ok++;
    debug_write(DBG_DDR_COMMON_IDX(ape_id, 38), g_u32_stc_time_sum);
    debug_write(DBG_DDR_COMMON_IDX(ape_id, 39), g_u32_send_ok);

    return ;
}

