/**
*   @file    Mcu_SSCM.c
*   @version 1.0.0
*
*   @brief   AUTOSAR Mcu - SSCM functions implementation.
*   @details Specific functions for SSCM 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_SSCM_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_SSCM_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_SSCM_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_SSCM_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_SSCM.h"

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

#if (MCU_DISABLE_DEM_REPORT_ERROR_STATUS == STD_OFF)
#include "Dem.h"
#endif /* (MCU_DISABLE_DEM_REPORT_ERROR_STATUS == STD_OFF) */

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

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

#define MCU_SSCM_VENDOR_ID_C                        43
#define MCU_SSCM_AR_RELEASE_MAJOR_VERSION_C         4
#define MCU_SSCM_AR_RELEASE_MINOR_VERSION_C         3
#define MCU_SSCM_AR_RELEASE_REVISION_VERSION_C      1
#define MCU_SSCM_SW_MAJOR_VERSION_C                 1
#define MCU_SSCM_SW_MINOR_VERSION_C                 0
#define MCU_SSCM_SW_PATCH_VERSION_C                 0


/*==================================================================================================
                                      FILE VERSION CHECKS
==================================================================================================*/
/* Check if current file and Mcu_SSCM header file are of the same vendor */
#if (MCU_SSCM_VENDOR_ID_C != MCU_SSCM_VENDOR_ID)
    #error "Mcu_SSCM.c and Mcu_SSCM.h have different vendor ids"
#endif
/* Check if current file and Mcu_SSCM header file are of the same Autosar version */
#if ((MCU_SSCM_AR_RELEASE_MAJOR_VERSION_C    != MCU_SSCM_AR_RELEASE_MAJOR_VERSION) || \
     (MCU_SSCM_AR_RELEASE_MINOR_VERSION_C    != MCU_SSCM_AR_RELEASE_MINOR_VERSION) || \
     (MCU_SSCM_AR_RELEASE_REVISION_VERSION_C != MCU_SSCM_AR_RELEASE_REVISION_VERSION) \
    )
    #error "AutoSar Version Numbers of Mcu_SSCM.c and Mcu_SSCM.h are different"
#endif
/* Check if current file and Mcu_SSCM header file are of the same Software version */
#if ((MCU_SSCM_SW_MAJOR_VERSION_C != MCU_SSCM_SW_MAJOR_VERSION) || \
     (MCU_SSCM_SW_MINOR_VERSION_C != MCU_SSCM_SW_MINOR_VERSION) || \
     (MCU_SSCM_SW_PATCH_VERSION_C != MCU_SSCM_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Mcu_SSCM.c and Mcu_SSCM.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_SSCM_AR_RELEASE_MAJOR_VERSION_C != DET_AR_RELEASE_MAJOR_VERSION) || \
         (MCU_SSCM_AR_RELEASE_MINOR_VERSION_C != DET_AR_RELEASE_MINOR_VERSION) \
        )
        #error "AutoSar Version Numbers of Mcu_SSCM.c and Det.h are different"
    #endif
#endif
#endif

/* Check if current file and DEM header file are of the same version */
#if (MCU_DISABLE_DEM_REPORT_ERROR_STATUS == STD_OFF)
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    #if ((MCU_SSCM_AR_RELEASE_MAJOR_VERSION_C != DEM_AR_RELEASE_MAJOR_VERSION) || \
         (MCU_SSCM_AR_RELEASE_MINOR_VERSION_C != DEM_AR_RELEASE_MINOR_VERSION) \
        )
        #error "AutoSar Version Numbers of Mcu_SSCM.c and Dem.h are different"
    #endif
#endif
#endif

/* Check if current file and SilRegMacros header file are of the same Autosar version */
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    #if ((MCU_SSCM_AR_RELEASE_MAJOR_VERSION_C != SILREGMACROS_AR_RELEASE_MAJOR_VERSION) || \
         (MCU_SSCM_AR_RELEASE_MINOR_VERSION_C != SILREGMACROS_AR_RELEASE_MINOR_VERSION) \
        )
        #error "AutoSar Version Numbers of Mcu_SSCM.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
==================================================================================================*/
#define MCU_START_SEC_CODE
/**
* @violates @ref Mcu_SSCM_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 MCU_ENABLE_USER_MODE_SUPPORT
#if (STD_ON == MCU_ENABLE_USER_MODE_SUPPORT)
#ifdef MCAL_SSCM_REG_PROT_AVAILABLE
#if (STD_ON == MCAL_SSCM_REG_PROT_AVAILABLE)
/**
* @brief            This function will enable writing in User mode by configuring REG_PROT
*/
FUNC( void, MCU_CODE) Mcu_SSCM_SetUserAccessAllowed(void)
{
    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    SET_USER_ACCESS_ALLOWED(SSCM_BASEADDR, SSCM_PROT_MEM_U32);
}
#endif /* (STD_ON == MCAL_SSCM_REG_PROT_AVAILABLE) */
#endif /* MCAL_SSCM_REG_PROT_AVAILABLE */
#endif /* (STD_ON == MCU_ENABLE_USER_MODE_SUPPORT) */
#endif /* MCU_ENABLE_USER_MODE_SUPPORT */

/*==================================================================================================
*                                       GLOBAL FUNCTIONS
==================================================================================================*/
#if (defined(MCU_DISABLE_SSCM_INIT) && (STD_OFF == MCU_DISABLE_SSCM_INIT))
/**
* @brief            Initialize SSCM registers
* @details
*
* @param[in]        SSCM_pConfigPtr - Pointer to SSCM configuration structure.
*
* @return           void
*
*/
/** @violates @ref Mcu_SSCM_c_REF_5 Global declaration of function */
FUNC( void, MCU_CODE) Mcu_SSCM_Init(P2CONST( Mcu_SSCM_ConfigType, AUTOMATIC, MCU_APPL_CONST) SSCM_pConfigPtr)
{
    VAR( uint16, AUTOMATIC) u16State = (uint16)0U;

    /* check SSCM errors */
    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    u16State = REG_READ16(SSCM_STATUS_ADDR16) ;
    /* clear only the CER flag if set - W1 to clear */
    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    REG_WRITE16(SSCM_STATUS_ADDR16, ((u16State & SSCM_STATUS_CER_MASK16 ) | SSCM_STATUS_RESBITS_MASK16));

    /* check if error and report it */
    if ( SSCM_STATUS_CER_MASK16 == (SSCM_STATUS_CER_MASK16 & u16State) )
    {
#if (MCU_DISABLE_DEM_REPORT_ERROR_STATUS == STD_OFF)
        if ((uint32)STD_ON == Mcu_pDemCfgPtr->Mcu_E_SscmCerFailureCfg.state)
        {
            (void)Dem_SetEventStatus((Dem_EventIdType)Mcu_pDemCfgPtr->Mcu_E_SscmCerFailureCfg.id, DEM_EVENT_STATUS_FAILED);
        }
#endif
    }
#if ((IPV_SSCM==IPV_SSCM_05_04_00_00) || (IPV_SSCM==IPV_SSCM_05_03_00_00) ||\
     (IPV_SSCM==IPV_SSCM_06_00_00_01) ||(IPV_SSCM==IPV_SSCM_06_00_00_06)\
    )
    #if (IPV_SSCM==IPV_SSCM_06_00_00_06)
        #if (MCU_SSCM_ERROR_PRESENT == STD_ON)
            /* write PAE and RAE bits */
            /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
            /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
            REG_WRITE16(SSCM_ERROR_ADDR16, SSCM_pConfigPtr->u16SscmError );
        #else
            MCU_PARAM_UNUSED(SSCM_pConfigPtr);
        #endif
    #else
        /* write PAE and RAE bits */
        /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
        /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
        REG_WRITE16(SSCM_ERROR_ADDR16, SSCM_pConfigPtr->u16SscmError );
    #endif
#else
    MCU_PARAM_UNUSED(SSCM_pConfigPtr);
#endif
}
#endif /* #if (defined(MCU_DISABLE_SSCM_INIT) && (STD_OFF == MCU_DISABLE_SSCM_INIT)) */

#if (MCU_GET_SYSTEM_STATE_API == STD_ON)
/**
* @brief            Get the System Status and Configuration Module information.
* @details          Return information about execution mode, security, vle, jtag id, etc.
*                       - Mcu_IPW_GetSystem_State() from IPW.
*
* @return           Status read from SSCM module - STATUS and MEMCONFIG registers.
* @retval           uint32   Platform register specific.
*
*/
FUNC( uint32, MCU_CODE) Mcu_SSCM_GetSystemState ( VAR( void, AUTOMATIC) )
{

    /* Temporary variable. */
    VAR( uint32, AUTOMATIC) u32State = (uint32)0U;

    /* Get the STATUS content register. */
    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    u32State = (uint32)REG_READ16(SSCM_STATUS_ADDR16);
    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    u32State |= (uint32)((uint32)REG_READ16(SSCM_MEMCONFIG_ADDR16) << (uint32)0x10U);

    return (uint32)u32State;
}
#endif /* (MCU_GET_SYSTEM_STATE_API == STD_ON) */

#if (MCU_GET_MEM_CONFIG_API == STD_ON)

/**
* @brief            This function returns the value of the SSCM_MEMCONFIG register.
* @details          Returns the System Memory and ID register which reflects the memory
*                   configuration of the system and the JTAG ID.
*
* @return           The value of the SSCM_MEMCONFIG register.
*
* @api
*
* @requirements
*
* @note
*
*/
FUNC( Mcu_SSCM_MemConfigType, MCU_CODE) Mcu_SSCM_GetMemConfig( VAR( void, AUTOMATIC) )
{
    /* Temporary variable. */
    VAR( Mcu_SSCM_MemConfigType, AUTOMATIC) State = (Mcu_SSCM_MemConfigType)0U;

    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    State = (Mcu_SSCM_MemConfigType)REG_READ16(SSCM_MEMCONFIG_ADDR16);

    return (Mcu_SSCM_MemConfigType)(State);
}

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

#if (MCU_SSCM_GET_STATUS_API == STD_ON)
/**
* @brief            This function returns the value of the SSCM_STATUS register.
* @details          Return information about execution mode, security, vle.
*                       - Mcu_IPW_GetStatus() from IPW.
*
* @return           The value of the SSCM_STATUS register.
*
* @api
*
* @requirements
*
* @note
*
*/
FUNC (Mcu_SSCM_StatusType, MCU_CODE) Mcu_SSCM_GetStatus(VAR( void, AUTOMATIC))
{
    VAR(Mcu_SSCM_StatusType, AUTOMATIC) Status = (Mcu_SSCM_StatusType)0U;

    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    Status = (Mcu_SSCM_StatusType)REG_READ16(SSCM_STATUS_ADDR16);
    return Status;
}
#endif /* MCU_SSCM_GET_STATUS_API == STD_ON */

#ifdef MCU_SSCM_GET_UOPS_API
#if (MCU_SSCM_GET_UOPS_API == STD_ON)
/**
* @brief            This function returns the value of the SSCM_UOPS register.
* @details          Return information about User Option Bits, HSM Boot Configuration.
*                       - Mcu_Ipw_SscmGetUops() from IPW.
*
* @return           The value of the SSCM_UOPS register.
*
* @api
*
* @requirements
*
* @note
*
*/
FUNC (Mcu_SSCM_UopsType, MCU_CODE) Mcu_SSCM_GetUops(VAR( void, AUTOMATIC))
{
    VAR(Mcu_SSCM_UopsType, AUTOMATIC) State = (Mcu_SSCM_UopsType)0U;

    /** @violates @ref Mcu_SSCM_c_REF_3 False positive error */
    /** @violates @ref Mcu_SSCM_c_REF_4 The cast is used to access memory mapped registers.*/
    State = (Mcu_SSCM_UopsType)REG_READ32(SSCM_UOPS_ADDR32);
    return State;
}
#endif /* MCU_SSCM_GET_UOPS_API == STD_ON */
#endif /* MCU_SSCM_GET_UOPS_API */

#define MCU_STOP_SEC_CODE
/**
* @violates @ref Mcu_SSCM_c_REF_1 #include directives should only be preceded by preprocessor
* directives or comments.
*
* @violates @ref Mcu_SSCM_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

/** @} */
