/*==================================================================================================
* Project : RTD AUTOSAR 4.4
* Platform : CORTEXM
* Peripheral : S32K3XX
* Dependencies : none
*
* Autosar Version : 4.4.0
* Autosar Revision : ASR_REL_4_4_REV_0000
* Autosar Conf.Variant :
* SW Version : 0.9.0
* Build Version : S32K3_RTD_0_9_0__ASR_REL_4_4_REV_0000_20210326
*
* (c) Copyright 2020 - 2021 NXP Semiconductors
* All Rights Reserved.
*
* NXP Confidential. This software is owned or controlled by NXP and may only be
* used strictly in accordance with the applicable license terms. By expressly
* accepting such terms or by downloading, installing, activating and/or otherwise
* using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms. If you do not agree to be
* bound by the applicable license terms, then you may not retain, install,
* activate or otherwise use the software.
==================================================================================================*/

/**
*   @file
*
*   @addtogroup osif_drv
*   @{
*/

#ifdef __cplusplus
extern "C"{
#endif

/*==================================================================================================
*                                          INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
#include "OsIf.h"
#include "OsIf_Cfg.h"
#include "OsIf_Cfg_TypesDef.h"
#include "OsIf_Timer_System.h"

#if (OSIF_USE_SYSTEM_TIMER == STD_ON)

#if defined(USING_OS_AUTOSAROS)
#include "Os.h"
#elif defined(USING_OS_FREERTOS)
#include "FreeRTOSConfig.h"
#else
    /* Baremetal, make sure USING_OS_BAREMETAL is defined */
#ifndef USING_OS_BAREMETAL
#define USING_OS_BAREMETAL
#endif /* ifndef USING_OS_BAREMETAL */
#endif /* defined(USING_OS_AUTOSAROS) */

/* Timer specific includes to define:
 - OsIf_Timer_System_Internal_Init
 - OsIf_Timer_System_Internal_GetCounter
 - OsIf_Timer_System_Internal_GetElapsed
 */
#if OSIF_USE_SYSTICK == STD_ON
#include "OsIf_Timer_System_Internal_Systick.h"
#endif /* OSIF_USE_SYSTICK == STD_ON */

/*==================================================================================================
*                                 SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define OSIF_TIMER_SYSTEM_VENDOR_ID_C                    43
#define OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C     4
#define OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C     4
#define OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C  0
#define OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C             0
#define OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C             9
#define OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C             0

/*==================================================================================================
*                                       FILE VERSION CHECKS
==================================================================================================*/
/* Checks against OsIf.h */
#if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_VENDOR_ID)
    #error "OsIf_Timer_System.c and OsIf.h have different vendor ids"
#endif
#if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_AR_RELEASE_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_AR_RELEASE_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf.h are different"
#endif
#if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_SW_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_SW_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of OsIf_Timer_System.c and OsIf.h are different"
#endif

/* Checks against OsIf_Cfg.h */
#if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_CFG_VENDOR_ID)
    #error "OsIf_Timer_System.c and OsIf_Cfg.h have different vendor ids"
#endif
#if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_AR_RELEASE_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Cfg.h are different"
#endif
#if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_CFG_SW_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_CFG_SW_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_CFG_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Cfg.h are different"
#endif

/* Checks against OsIf_Cfg_TypesDef.h */
#if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_CFG_TYPESDEF_VENDOR_ID)
    #error "OsIf_Timer_System.c and OsIf_Cfg_TypesDef.h have different vendor ids"
#endif
#if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_CFG_TYPESDEF_AR_RELEASE_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_CFG_TYPESDEF_AR_RELEASE_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_TYPESDEF_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Cfg_TypesDef.h are different"
#endif
#if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_CFG_TYPESDEF_SW_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_CFG_TYPESDEF_SW_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_CFG_TYPESDEF_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Cfg_TypesDef.h are different"
#endif

/* Checks against OsIf_Timer_System.h */
#if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_TIMER_SYSTEM_VENDOR_ID)
    #error "OsIf_Timer_System.c and OsIf_Timer_System.h have different vendor ids"
#endif
#if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System.h are different"
#endif
#if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_TIMER_SYSTEM_SW_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_TIMER_SYSTEM_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System.h are different"
#endif

#if OSIF_USE_SYSTICK == STD_ON
/* Checks against OsIf_Timer_System_Internal_Systick.h */
#if (OSIF_TIMER_SYSTEM_VENDOR_ID_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_VENDOR_ID)
    #error "OsIf_Timer_System.c and OsIf_Timer_System_Internal_Systick.h have different vendor ids"
#endif
#if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C    != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_AR_RELEASE_REVISION_VERSION))
    #error "AUTOSAR Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System_Internal_Systick.h are different"
#endif
#if ((OSIF_TIMER_SYSTEM_SW_MAJOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MAJOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_MINOR_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_MINOR_VERSION) || \
     (OSIF_TIMER_SYSTEM_SW_PATCH_VERSION_C != OSIF_TIMER_SYSTEM_INTERNAL_SYSTICK_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of OsIf_Timer_System.c and OsIf_Timer_System_Internal_Systick.h are different"
#endif
#endif /* OSIF_USE_SYSTICK == STD_ON */

#if defined(USING_OS_AUTOSAROS)
/* Checks against Os.h */
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    #if ((OSIF_TIMER_SYSTEM_AR_RELEASE_MAJOR_VERSION_C != OS_AR_RELEASE_MAJOR_VERSION) || \
         (OSIF_TIMER_SYSTEM_AR_RELEASE_MINOR_VERSION_C != OS_AR_RELEASE_MINOR_VERSION))
        #error "AutoSar Version Numbers of OsIf_Timer_System.c and Os.h are different"
    #endif
#endif
#endif /* defined(USING_OS_AUTOSAROS) */

/*==================================================================================================
*                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
==================================================================================================*/

/*==================================================================================================
*                                          LOCAL MACROS
==================================================================================================*/
#if (STD_ON == OSIF_ENABLE_USER_MODE_SUPPORT)
    #define Trusted_OsIf_Timer_System_Internal_Init()                   OsIf_Trusted_Call(OsIf_Timer_System_Internal_Init)
    #define Trusted_OsIf_Timer_System_Internal_GetCounter()             OsIf_Trusted_Call_Return(OsIf_Timer_System_Internal_GetCounter)
    #define Trusted_OsIf_Timer_System_Internal_GetElapsed(CurrentRef)   OsIf_Trusted_Call_Return1param(OsIf_Timer_System_Internal_GetElapsed, (CurrentRef))
#else
    #define Trusted_OsIf_Timer_System_Internal_Init()                   OsIf_Timer_System_Internal_Init()
    #define Trusted_OsIf_Timer_System_Internal_GetCounter()             OsIf_Timer_System_Internal_GetCounter()
    #define Trusted_OsIf_Timer_System_Internal_GetElapsed(CurrentRef)   OsIf_Timer_System_Internal_GetElapsed(CurrentRef)
#endif

#if STD_ON == OSIF_ENABLE_MULTICORE_SUPPORT
    #define OsIfGetCoreID()        (OsIf_GetCoreID())
#else
    #define OsIfGetCoreID()        (0U)
#endif

/*==================================================================================================
*                                         LOCAL CONSTANTS
==================================================================================================*/

/*==================================================================================================
*                                         LOCAL VARIABLES
==================================================================================================*/
#define BASE_START_SEC_VAR_NO_INIT_BOOLEAN
#include "Base_MemMap.h"

static boolean OsIf_abMdlInit[OSIF_MAX_COREIDX_SUPPORTED];

#define BASE_STOP_SEC_VAR_NO_INIT_BOOLEAN
#include "Base_MemMap.h"

#define BASE_START_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Base_MemMap.h"

static const OsIf_ConfigType *OsIf_apxInternalCfg[OSIF_MAX_COREIDX_SUPPORTED];

#define BASE_STOP_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Base_MemMap.h"

#define BASE_START_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Base_MemMap.h"

static uint32 OsIf_au32InternalFrequencies[OSIF_MAX_COREIDX_SUPPORTED];

#define BASE_STOP_SEC_VAR_NO_INIT_UNSPECIFIED
#include "Base_MemMap.h"
/*==================================================================================================
*                                        GLOBAL CONSTANTS
==================================================================================================*/

/*==================================================================================================
*                                        GLOBAL VARIABLES
==================================================================================================*/
#define BASE_START_SEC_CONFIG_DATA_UNSPECIFIED
#include "Base_MemMap.h"

extern const OsIf_ConfigType *const OsIf_apxPredefinedConfig[OSIF_MAX_COREIDX_SUPPORTED];

#define BASE_STOP_SEC_CONFIG_DATA_UNSPECIFIED
#include "Base_MemMap.h"
/*==================================================================================================
*                                    LOCAL FUNCTION PROTOTYPES
==================================================================================================*/

/*==================================================================================================
*                                         LOCAL FUNCTIONS
==================================================================================================*/

/*==================================================================================================
*                                        GLOBAL FUNCTIONS
==================================================================================================*/
#define BASE_START_SEC_CODE
#include "Base_MemMap.h"

void OsIf_Timer_System_Init(void)
{
    uint32 CoreId = OsIfGetCoreID();

#if (STD_ON == OSIF_DEV_ERROR_DETECT)
    if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxPredefinedConfig[CoreId]))
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_INIT, OSIF_E_INV_CORE_IDX);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
#endif

    OsIf_abMdlInit[CoreId] = TRUE;
    (void)OsIf_abMdlInit[CoreId];
    OsIf_apxInternalCfg[CoreId] = OsIf_apxPredefinedConfig[CoreId];
#if !defined(USING_OS_FREERTOS)
    OsIf_au32InternalFrequencies[CoreId] = OsIf_apxInternalCfg[CoreId]->counterFrequency;
#endif

#if defined(USING_OS_FREERTOS)
    /* FreeRTOS */
    OsIf_au32InternalFrequencies[CoreId] = configCPU_CLOCK_HZ;
#elif defined(USING_OS_BAREMETAL)
    /* Baremetal */
    (void)CoreId;
    Trusted_OsIf_Timer_System_Internal_Init();
#endif
}


uint32 OsIf_Timer_System_GetCounter(void)
{
    uint32 Counter = 0u;
    uint32 CoreId = OsIfGetCoreID();

#if STD_ON == OSIF_DEV_ERROR_DETECT
    if (TRUE != OsIf_abMdlInit[CoreId])
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETCOUNTER, OSIF_E_UNINIT);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETCOUNTER, OSIF_E_INV_CORE_IDX);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else
    {
#endif /* OSIF_DEV_ERROR_DETECT  */

#if defined(USING_OS_AUTOSAROS)
    StatusType Status;
    TickType value;
    Status = GetCounterValue(OsIf_apxInternalCfg[CoreId]->counterId, &value);
    OSIF_DEV_ASSERT(Status == E_OK);
    Counter = (uint32)value;
#elif defined(USING_OS_FREERTOS) || defined(USING_OS_BAREMETAL)
    /* FreeRTOS and Baremetal*/
    (void)CoreId;
    Counter = Trusted_OsIf_Timer_System_Internal_GetCounter();
#endif

#if STD_ON == OSIF_DEV_ERROR_DETECT
    }
#endif

    return Counter;
}


uint32 OsIf_Timer_System_GetElapsed(uint32 * const CurrentRef)
{
    uint32 Elapsed = 0u;
    uint32 CoreId = OsIfGetCoreID();

#if STD_ON == OSIF_DEV_ERROR_DETECT
    if (TRUE != OsIf_abMdlInit[CoreId])
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETELAPSED, OSIF_E_UNINIT);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_GETELAPSED, OSIF_E_INV_CORE_IDX);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else
    {
#endif /* OSIF_DEV_ERROR_DETECT  */

#if defined(USING_OS_AUTOSAROS)
    TickType ElapsedTickType;
    StatusType Status;
    Status = GetElapsedValue(OsIf_apxInternalCfg[CoreId]->counterId, (TickType*)CurrentRef, &ElapsedTickType);
    OSIF_DEV_ASSERT(Status == E_OK);
    Elapsed = (uint32)ElapsedTickType;
#elif defined(USING_OS_FREERTOS) || defined(USING_OS_BAREMETAL)
    /* FreeRTOS and Baremetal*/
    (void)CoreId;
    Elapsed = Trusted_OsIf_Timer_System_Internal_GetElapsed(CurrentRef);
#endif

#if STD_ON == OSIF_DEV_ERROR_DETECT
    }
#endif

    return Elapsed;
}


void OsIf_Timer_System_SetTimerFrequency(uint32 Freq)
{
    uint32 CoreId = OsIfGetCoreID();
#if STD_ON == OSIF_DEV_ERROR_DETECT

    if (TRUE != OsIf_abMdlInit[CoreId])
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_SETTIMERFREQ, OSIF_E_UNINIT);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_SETTIMERFREQ, OSIF_E_INV_CORE_IDX);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else
    {
#endif /* OSIF_DEV_ERROR_DETECT  */

#if defined(USING_OS_AUTOSAROS)
    (void)CoreId;
    (void)Freq;
#elif defined(USING_OS_FREERTOS) || defined(USING_OS_BAREMETAL)
    /* FreeRTOS and Baremetal*/
    OsIf_au32InternalFrequencies[CoreId] = Freq;
#endif

#if STD_ON == OSIF_DEV_ERROR_DETECT
    }
#endif
}


uint32 OsIf_Timer_System_MicrosToTicks(uint32 Micros)
{
    uint64 interim;
    uint32 ticks = 0u;
    uint32 CoreId = OsIfGetCoreID();

#if STD_ON == OSIF_DEV_ERROR_DETECT
    if (TRUE != OsIf_abMdlInit[CoreId])
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_US2TICKS, OSIF_E_UNINIT);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else if ((CoreId >= OSIF_MAX_COREIDX_SUPPORTED) || (NULL_PTR == OsIf_apxInternalCfg[CoreId]))
    {
        #if defined(USING_OS_AUTOSAROS)
        (void)Det_ReportError(OSIF_MODULE_ID, OSIF_DRIVER_INSTANCE, OSIF_SID_US2TICKS, OSIF_E_INV_CORE_IDX);
        #else
        OSIF_DEV_ASSERT(FALSE);
        #endif
    }
    else
    {
#endif /* OSIF_DEV_ERROR_DETECT  */

    interim = Micros * (uint64)OsIf_au32InternalFrequencies[CoreId];
    interim /= 1000000u;
    OSIF_DEV_ASSERT(interim <= 0xFFFFFFFFu); /* check that computed value fits in 32 bits */
    ticks = (uint32)(interim & 0xFFFFFFFFu);

#if STD_ON == OSIF_DEV_ERROR_DETECT
    }
#endif

    return ticks;
}

#define BASE_STOP_SEC_CODE
#include "Base_MemMap.h"

#endif /* (OSIF_USE_SYSTEM_TIMER == STD_ON) */

#ifdef __cplusplus
}
#endif

/** @} */
