/**
*   @file    Mcu_STCU.c
*   @version 1.0.0
*
*   @brief   AUTOSAR Mcu - STCU functions implementation.
*   @details Specific functions for STCU configuration and control.
*
*   @addtogroup MCU
*   @{
*/
/*==================================================================================================
*   Project              : AUTOSAR 4.3 MCAL
*   Platform             : PA
*   Peripheral           : MC
*   Dependencies         : none
*
*   Autosar Version      : 4.3.1
*   Autosar Revision     : ASR_REL_4_3_REV_0001
*   Autosar Conf.Variant :
*   SW Version           : 1.0.0
*   Build Version        : MPC5777C_MCAL_1_0_0_RTM_ASR_REL_4_3_REV_0001_20190315
*
*   (c) Copyright 2006-2016 Freescale Semiconductor, Inc. 
*       Copyright 2017-2019 NXP
*   All Rights Reserved.
==================================================================================================*/
/*==================================================================================================
==================================================================================================*/


#ifdef __cplusplus
extern "C"{
#endif

/**
* @page misra_violations MISRA-C:2012 violations
*
* @section Mcu_STCU_c_REF_1
* Violates MISRA 2012 Advisory Rule 20.1, #include directives should only be preceded by preprocessor
* directives or comments. AUTOSAR imposes the specification of the sections in which certain parts
* of the driver must be placed.
*
* @section Mcu_STCU_c_REF_2
* Violates MISRA 2012 Required Directive 4.10, Precautions shall be taken in order to prevent the contents
* of a header file being included more than once. This comes from the order of includes in the .c file
* and from include dependencies. As a safe approach, any file must include all its dependencies.
* Header files are already protected against double inclusions. The inclusion of Mcu_MemMap.h is as
* per AUTOSAR requirement [SWS_MemMap_00003].
*
* @section Mcu_STCU_c_REF_3
* Violates MISRA 2012 Required Rule 11.6, A cast shall not be performed between pointer to void and an
* arithmetic type.
* This is a false positive error. PC-Lint is checking against a more restrictive condition
* ("pointer/non-pointer to non-pointer/pointer" vs "pointer-to-void to arithmetic type").
*
* @section Mcu_STCU_c_REF_4
* Violates MISRA 2012 Advisory Rule 11.4, A conversion should not be performed between a pointer to object
* and an integer type.
* The cast is used to access memory mapped registers.
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.4, Macro identifiers shall be distinct.
* This violation is due to the AUTOSAR requirement [SWS_BSW_00036] (perform inter module checks).
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.1, External identifiers shall be distinct.
* This rule does not apply if either identifier is a macro identifier, because this case is covered
* by Rule 5.4 and Rule 5.5.
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.2, Identifiers declared in the same scope and name space shall be distinct.
* This rule does not apply if either identifier is a macro identifier, because this case is covered
* by Rule 5.4 and Rule 5.5.
*
* @section [global]
* Violates MISRA 2012 Required Rule 5.5, Identifiers shall be distinct from macro names.
* The identifier clash is between macro identifiers. Therefore, this rule is incorrectly reported by the tool.
*
*/


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

#if (MCU_DEV_ERROR_DETECT == STD_ON)
#include "Det.h"
#endif /* (MCU_DEV_ERROR_DETECT == STD_ON) */

#include "Mcu_Dem_Wrapper.h"

#define USER_MODE_REG_PROT_ENABLED (MCU_ENABLE_USER_MODE_SUPPORT)
#include "SilRegMacros.h"

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/

#define MCU_STCU_VENDOR_ID_C                        43
#define MCU_STCU_AR_RELEASE_MAJOR_VERSION_C         4
#define MCU_STCU_AR_RELEASE_MINOR_VERSION_C         3
#define MCU_STCU_AR_RELEASE_REVISION_VERSION_C      1
#define MCU_STCU_SW_MAJOR_VERSION_C                 1
#define MCU_STCU_SW_MINOR_VERSION_C                 0
#define MCU_STCU_SW_PATCH_VERSION_C                 0


/*==================================================================================================
                                      FILE VERSION CHECKS
==================================================================================================*/
/* Check if current file and Mcu_STCU header file are of the same vendor */
#if (MCU_STCU_VENDOR_ID_C != MCU_STCU_VENDOR_ID)
    #error "Mcu_STCU.c and Mcu_STCU.h have different vendor ids"
#endif
/* Check if current file and Mcu_STCU header file are of the same Autosar version */
#if ((MCU_STCU_AR_RELEASE_MAJOR_VERSION_C != MCU_STCU_AR_RELEASE_MAJOR_VERSION) || \
     (MCU_STCU_AR_RELEASE_MINOR_VERSION_C != MCU_STCU_AR_RELEASE_MINOR_VERSION) || \
     (MCU_STCU_AR_RELEASE_REVISION_VERSION_C != MCU_STCU_AR_RELEASE_REVISION_VERSION) \
    )
    #error "AutoSar Version Numbers of Mcu_STCU.c and Mcu_STCU.h are different"
#endif
/* Check if current file and Mcu_STCU header file are of the same Software version */
#if ((MCU_STCU_SW_MAJOR_VERSION_C != MCU_STCU_SW_MAJOR_VERSION) || \
     (MCU_STCU_SW_MINOR_VERSION_C != MCU_STCU_SW_MINOR_VERSION) || \
     (MCU_STCU_SW_PATCH_VERSION_C != MCU_STCU_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Mcu_STCU.c and Mcu_STCU.h are different"
#endif

/* Check if current file and DET header file are of the same version */
#if (MCU_DEV_ERROR_DETECT == STD_ON)
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    #if ((MCU_STCU_AR_RELEASE_MAJOR_VERSION_C != DET_AR_RELEASE_MAJOR_VERSION) || \
         (MCU_STCU_AR_RELEASE_MINOR_VERSION_C != DET_AR_RELEASE_MINOR_VERSION) \
        )
        #error "AutoSar Version Numbers of Mcu_STCU.c and Det.h are different"
    #endif
#endif
#endif

/* Check if current file and Mcu_Dem_Wrapper header file are of the same vendor */
#if (MCU_STCU_VENDOR_ID_C != MCU_DEM_WRAPPER_VENDOR_ID)
    #error "Mcu_STCU.c and Mcu_Dem_Wrapper.h have different vendor ids"
#endif
/* Check if current file and Mcu_Dem_Wrapper header file are of the same Autosar version */
#if ((MCU_STCU_AR_RELEASE_MAJOR_VERSION_C    != MCU_DEM_WRAPPER_AR_RELEASE_MAJOR_VERSION) || \
     (MCU_STCU_AR_RELEASE_MINOR_VERSION_C    != MCU_DEM_WRAPPER_AR_RELEASE_MINOR_VERSION) || \
     (MCU_STCU_AR_RELEASE_REVISION_VERSION_C != MCU_DEM_WRAPPER_AR_RELEASE_REVISION_VERSION) \
    )
    #error "AutoSar Version Numbers of Mcu_STCU.c and Mcu_Dem_Wrapper.h are different"
#endif
/* Check if current file and Mcu_Dem_Wrapper header file are of the same Software version */
#if ((MCU_STCU_SW_MAJOR_VERSION_C != MCU_DEM_WRAPPER_SW_MAJOR_VERSION) || \
     (MCU_STCU_SW_MINOR_VERSION_C != MCU_DEM_WRAPPER_SW_MINOR_VERSION) || \
     (MCU_STCU_SW_PATCH_VERSION_C != MCU_DEM_WRAPPER_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Mcu_STCU.c and Mcu_Dem_Wrapper.h are different"
#endif

/* Check if current file and SilRegMacros header file are of the same Autosar version */
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    #if ((MCU_STCU_AR_RELEASE_MAJOR_VERSION_C != SILREGMACROS_AR_RELEASE_MAJOR_VERSION) || \
         (MCU_STCU_AR_RELEASE_MINOR_VERSION_C != SILREGMACROS_AR_RELEASE_MINOR_VERSION) \
        )
        #error "AutoSar Version Numbers of Mcu_STCU.c and SilRegMacros.h are different"
    #endif
#endif

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


/*==================================================================================================
*                                       LOCAL MACROS
==================================================================================================*/


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


/*==================================================================================================
*                                      LOCAL VARIABLES
==================================================================================================*/


/*==================================================================================================
*                                      GLOBAL CONSTANTS
==================================================================================================*/


/*==================================================================================================
                                       GLOBAL VARIABLES
==================================================================================================*/


/*==================================================================================================
                                   LOCAL FUNCTION PROTOTYPES
==================================================================================================*/


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


/*==================================================================================================
*                                       GLOBAL FUNCTIONS
==================================================================================================*/
#define MCU_START_SEC_CODE
/**
* @violates @ref Mcu_STCU_c_REF_2 Precautions shall be taken in order to prevent the contents
* of a header file being included more than once.
*/
#include "Mcu_MemMap.h"

#if (MCU_GET_RAM_STATE_API == STD_ON)
/**
* @brief            This function check the STCU registers
* @details          This function evaluate the status of each IP RAM area.
*                   The default STCU configuration as it is located in the test flash memory leads
*                   to a full self-test after an STCU reset event (running all MBISTs and LBISTs).
*                   MBIST - Boot-time Memory Built-In Self-Test (volatile memories (SRAMs) and ROMs
*                   test).
*                   MBIST Engine - Manages the testing of embedded memory blocks.
*
* @return           Status of the RAM Content.
* @retval           MCU_RAMSTATE_INVALID   Ram content is not valid or unknown (default).
* @retval           MCU_RAMSTATE_VALID   Ram content is valid.
*
*/
FUNC (Mcu_RamStateType, MCU_CODE) Mcu_STCU_GetRamState( VAR( void, AUTOMATIC))
{
    /* Timeout value for specific wait operations. */
    VAR( uint32, AUTOMATIC) u32TimeOut = MCU_TIMEOUT_LOOPS;
    VAR( uint32, AUTOMATIC) u32TempReg ;
    /* Return from this function. */
    VAR( Mcu_RamStateType, AUTOMATIC) eRamState = MCU_RAMSTATE_INVALID;

    /* Stay in this loop until these registers are automatically updated following the
    completion of the MBIST run. */

    /*
    Memory (SRAM/ROM) Built-In Self-Test (MBIST) at startup and shutdown.

    STCU2 Off-Line MBIST Status Low Register (STCU_MBSL) 1 No Fault detected during the NMCUT
    BIST execution
    STCU2 Off-Line MBIST End Flag Low Register (STCU_MBEL) 1 MBIST execution finished

    STCU2 On-Line MBIST Status Low Register (STCU_MBSLSW) 1 No Fault detected during the NMCUT
    BIST execution
    STCU2 On-Line MBIST End Flag Low Register (STCU_MBELSW) 1 MBIST execution finished
    MBIST2 System RAM 1
    MBIST3 System RAM 0
    MBIST4 System RAM 3
    MBIST5 System RAM 2
    MBIST6 System RAM 5
    MBIST7 System RAM 4
    */
    /* check if those test finished and if their status is OK */
    /* check if the test are finished / wait for them */
    /** @violates @ref Mcu_STCU_c_REF_3 False positive error */
    /** @violates @ref Mcu_STCU_c_REF_4 The cast is used to access memory mapped registers.*/
    u32TempReg = REG_READ32( STCU_MBEL_ADDR32 ) & STCU_MBEL_RAM_TEST_MASK32 ;
    while((u32TimeOut > (uint32)0x0U ) && ( u32TempReg != STCU_MBEL_RAM_TEST_MASK32))
    {
        /** @violates @ref Mcu_STCU_c_REF_3 False positive error */
        /** @violates @ref Mcu_STCU_c_REF_4 The cast is used to access memory mapped registers.*/
        u32TempReg = REG_READ32( STCU_MBEL_ADDR32 ) & STCU_MBEL_RAM_TEST_MASK32 ;
        u32TimeOut --;
    }

    Mcu_Macro_ReportDemTimeoutError(u32TimeOut);

    if ( 0U != u32TimeOut)
    {
            /* It's not timeout, check the actual test status */
            /** @violates @ref Mcu_STCU_c_REF_3 False positive error */
            /** @violates @ref Mcu_STCU_c_REF_4 The cast is used to access memory mapped registers.*/
            if (STCU_MBSL_RAM_TEST_MASK32 == (REG_READ32( STCU_MBSL_ADDR32 ) & STCU_MBSL_RAM_TEST_MASK32))
            {
                eRamState = MCU_RAMSTATE_VALID;
            }
            else
            {
                eRamState = MCU_RAMSTATE_INVALID;
            }
    }

    return eRamState;
}
#endif /* (MCU_GET_RAM_STATE_API == STD_ON) */


#define MCU_STOP_SEC_CODE
/**
* @violates @ref Mcu_STCU_c_REF_1 #include directives should only be preceded by preprocessor
* directives or comments.
*
* @violates @ref Mcu_STCU_c_REF_2 Precautions shall be taken in order to prevent the contents
* of a header file being included more than once.
*/
#include "Mcu_MemMap.h"


#ifdef __cplusplus
}
#endif

/** @} */
