/******************************************************************************/
/* Williams Advanced Engineering Limited                     Copyright(C) 2021*/
/* Grove                                                  All rights reserved */
/*                                                                            */
/******************************************************************************/


#include "FBL_DataMCfg.h"

// Removing of compiler warnings and errors because of type re-declaration between hal_stdtypes.h and Std_Types.h
#define _BOOLEAN_DECLARED
#define _UINT8_DECLARED
#define _UINT16_DECLARED
#define _UINT32_DECLARED
#define _UINT64_DECLARED
#define _SINT8_DECLARED
#define _SINT16_DECLARED
#define _SINT32_DECLARED
#define _SINT64_DECLARED
#define _FLOAT32_DECLARED
#define _FLOAT64_DECLARED
#define _STD_RETURN_TYPE_DECLARED
#define _STDVERSION_DECLARED

#include "Ios_IntrTimerManagement.h"

uint32 g_ulTimerCounter = 0;
uint8 g_bTick = FALSE;


void fnIosIncrementTimerCounter(void)
{
    g_ulTimerCounter++;
    g_bTick = TRUE;
}

uint32 fnIosGet10msTimerCounterValue(void)
{
    return (g_ulTimerCounter);
}


void fnIosResetTimerCounter(void)
{
    g_ulTimerCounter = 0;
}

void fnIosResetTick(void)
{
    g_bTick = FALSE;
}

uint8 fnIosGetTick(void)
{
    return(g_bTick);
}

// Do not use it - Clarification on how this works required - Datasheet is not clear
uint64 fnIosGetFreeRunningCounterValue64Bits(uint8 bCounterID)
{
    uint64 u64CounterValue = 0UL;
    uint64 u64CounterValueLow = 0UL, u64CounterValueHigh = 0UL;

    if(bCounterID <= rtiCOUNTER_BLOCK1)
    {
        u64CounterValueLow = (uint64)((*((uint32*)(&(rtiREG1->CNT[bCounterID].FRCx)))) & 0xFFFFFFFF);
        u64CounterValueHigh = (uint64)((*((uint32*)(&(rtiREG1->CNT[bCounterID].UCx)))) & 0xFFFFFFFF);
        u64CounterValue = ((u64CounterValueHigh << 32) & 0xFFFFFFFF00000000) | (u64CounterValueLow);
    }
    else u64CounterValue = 0xFFFFFFFFFFFFFFFF;

    return (u64CounterValue);
}

uint32 fnIosGetFreeRunningCounterValue32Bits(uint8 bCounterID)
{
    uint32 ulCounterValue = 0UL;

    if(bCounterID <= rtiCOUNTER_BLOCK1)
    {
        ulCounterValue = (uint32)(*((uint32*)(&(rtiREG1->CNT[bCounterID].FRCx))));
    }
    else ulCounterValue = 0xFFFFFFFF;

    return (ulCounterValue);
}

void fnIosInitRTICountersFromNVM(void)
{
    uint64 ullNVMValue;
    Std_ReturnType retRead;

    retRead = Fbl_DataM_NvmFreeRunningCntrReadFunc((uint8*)&ullNVMValue);
    if(retRead==E_OK)
    {
        rtiREG1->CNT[0].FRCx = (uint32) (ullNVMValue & 0x00000000FFFFFFFF);
        rtiREG1->CNT[1].FRCx = (uint32) ((ullNVMValue >> 32) & 0x00000000FFFFFFFF);
    }
    else
    {
        rtiREG1->CNT[0].FRCx = 0;
        rtiREG1->CNT[0].UCx  = 0;
        rtiREG1->CNT[1].FRCx = 0;
        rtiREG1->CNT[1].UCx  = 0;
    }
}


void fnIosSaveRTICountersToNVM(void)
{
    uint64 ullNVMValue;
    Std_ReturnType retRead;


    ullNVMValue  = ((uint64)rtiREG1->CNT[0].FRCx) & 0x00000000FFFFFFFF;
    ullNVMValue |= ((((uint64)rtiREG1->CNT[1].FRCx) & 0x00000000FFFFFFFF) << 32);
    (void)Fbl_DataM_NvmFreeRunningCntrWriteFunc((uint8*)&ullNVMValue);

}
