/**
*   @file    Mcu_eMios.c
*   @version 1.0.0
*
*   @brief   AUTOSAR Mcu - eMios functions implementation.
*   @details Specific functions for eMios 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_EMIOS_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_EMIOS_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_EMIOS_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_EMIOS_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 MCU_EMIOS_C_REF_5
* Violates MISRA 2012 Advisory Rule 8.9, An object should be defined at block scope if its identifier only
* appears in a single function.
* This violation is due to some AUTOSAR requirements which mandate that flash access code routines
* shall be accessed by means of function pointers, whether these routines have been loaded to RAM
* or whether they can be executed directly from flash.
*
*/

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

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

/*==================================================================================================
                               SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define MCU_EMIOS_VENDOR_ID_C                           43
#define MCU_EMIOS_AR_RELEASE_MAJOR_VERSION_C            4
#define MCU_EMIOS_AR_RELEASE_MINOR_VERSION_C            3
#define MCU_EMIOS_AR_RELEASE_REVISION_VERSION_C         1
#define MCU_EMIOS_SW_MAJOR_VERSION_C                    1
#define MCU_EMIOS_SW_MINOR_VERSION_C                    0
#define MCU_EMIOS_SW_PATCH_VERSION_C                    0

/*==================================================================================================
*                                      FILE VERSION CHECKS
==================================================================================================*/

/* Check if current file and Mcu_eMios header file are of the same vendor */
#if (MCU_EMIOS_VENDOR_ID_C != MCU_EMIOS_H_VENDOR_ID)
    #error "Mcu_eMios.c and Mcu_eMios.h have different vendor ids"
#endif
/* Check if current file and Mcu_eMios header file are of the same Autosar version */
#if ((MCU_EMIOS_AR_RELEASE_MAJOR_VERSION_C    != MCU_EMIOS_H_AR_RELEASE_MAJOR_VERSION) || \
     (MCU_EMIOS_AR_RELEASE_MINOR_VERSION_C    != MCU_EMIOS_H_AR_RELEASE_MINOR_VERSION) || \
     (MCU_EMIOS_AR_RELEASE_REVISION_VERSION_C != MCU_EMIOS_H_AR_RELEASE_REVISION_VERSION) \
    )
    #error "AutoSar Version Numbers of Mcu_eMios.c and Mcu_eMios.h are different"
#endif
/* Check if current file and Mcu_eMios header file are of the same Software version */
#if ((MCU_EMIOS_SW_MAJOR_VERSION_C != MCU_EMIOS_H_SW_MAJOR_VERSION) || \
     (MCU_EMIOS_SW_MINOR_VERSION_C != MCU_EMIOS_H_SW_MINOR_VERSION) || \
     (MCU_EMIOS_SW_PATCH_VERSION_C != MCU_EMIOS_H_SW_PATCH_VERSION) \
    )
    #error "Software Version Numbers of Mcu_eMios.c and Mcu_eMios.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_EMIOS_AR_RELEASE_MAJOR_VERSION_C != SILREGMACROS_AR_RELEASE_MAJOR_VERSION) || \
         (MCU_EMIOS_AR_RELEASE_MINOR_VERSION_C != SILREGMACROS_AR_RELEASE_MINOR_VERSION) \
        )
        #error "AutoSar Version Numbers of Mcu_eMios.c and SilRegMacros.h are different"
    #endif
#endif

/*==================================================================================================
*                                        LOCAL MACROS
==================================================================================================*/
#if ((MCU_EMIOS_SUPPORT == STD_ON) || (MCU_EMIOS_CONFIGURE_GPREN_API == STD_ON))
/* doesn't have any EMIOS which is available on the platform. */
#define EMIOS3_MCR_NONE_ADDR32            ((uint32)0xFFFFFFFFUL)
#endif

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

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


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

/*==================================================================================================
*                                       GLOBAL CONSTANTS
==================================================================================================*/
#define MCU_START_SEC_CONST_32
/**
* @violates @ref MCU_EMIOS_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_EMIOS_SUPPORT == STD_ON) || (MCU_EMIOS_CONFIGURE_GPREN_API == STD_ON))
/**
* @violates @ref MCU_EMIOS_C_REF_5 An object should be defined at block scope if its identifier only
* appears in a single function.
*/
static CONST(uint32, MCU_CONST) EMIOS_MCR_ADDR32[] = 
{
#ifdef EMIOS_0_BASEADDR
    /** @brief check if first emios is available on the platform */
    EMIOS_0_BASEADDR,
#endif

#ifdef EMIOS_1_BASEADDR
    /** @brief check if second emios is available on the platform */
    EMIOS_1_BASEADDR,
#endif

#ifdef EMIOS_2_BASEADDR
    /** @brief check if third emios is available on the platform */
    EMIOS_2_BASEADDR,
#endif

#ifdef EMIOS_3_BASEADDR
    /** @brief check if for fourth emios is available on the platform */
    EMIOS_3_BASEADDR,
#endif

    /** @brief doesn't have any EMIOS which is available on the platform. */
    EMIOS3_MCR_NONE_ADDR32
};
#endif

#define MCU_STOP_SEC_CONST_32
/**
* @violates @ref MCU_EMIOS_C_REF_1 #include directives should only be preceded by preprocessor
* directives or comments.
*
* @violates @ref MCU_EMIOS_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"

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

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

/*==================================================================================================
*                                       LOCAL FUNCTIONS
==================================================================================================*/
#define MCU_START_SEC_CODE
/**
* @violates @ref MCU_EMIOS_C_REF_1 #include directives should only be preceded by preprocessor
* directives or comments.
*
* @violates @ref MCU_EMIOS_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"


/*==================================================================================================
*                                       GLOBAL FUNCTIONS
==================================================================================================*/
#if (MCU_EMIOS_SUPPORT == STD_ON)
/**
* @brief            Initializes the EMIOS MCR register for EMIOS_0, EMIOS_1
* @details          Function to initialize the Emios MCR register value configured.
*                   Called by:
*                       - Mcu_IPW_InitClock() and Mcu_IPW_DistributePllClock() from IPW.
*
* @param[in]        pEmiosConfigPtr   Pointer to eMios configuration structure.
*
* @return           void
*
*/
FUNC (void, MCU_CODE) Mcu_eMios_Init(VAR (uint8, AUTOMATIC) u8Module, P2CONST(Mcu_eMios_ConfigType, AUTOMATIC, MCU_APPL_CONST) pConfigPtr)
{
    /* Enter critical region*/
    SchM_Enter_Mcu_MCU_EXCLUSIVE_AREA_00();

    /** @violates @ref MCU_EMIOS_C_REF_3 False positive error */
    /** @violates @ref MCU_EMIOS_C_REF_4 The cast is used to access memory mapped registers.*/
    REG_WRITE32(EMIOS_MCR_ADDR32[u8Module], pConfigPtr->au32EmiosData[u8Module]);

    /* Exit critical region. */
    SchM_Exit_Mcu_MCU_EXCLUSIVE_AREA_00();
}
#endif

#if (MCU_EMIOS_CONFIGURE_GPREN_API == STD_ON)
/**
 * @brief   eMios Global Prescaler Enable.
 * @details This function enable/disable the GPREN bit of the EMIOSMCR register of an
 *          addressed eMIOS instance.
 *                   Called by:
 *                       - Mcu_IPW_CheckEmiosStatus() from IPW.
 *
 * @param[in] u8Module       MCU_EMIOS_MODULE_0 --> Select eMios 0
 *                           MCU_EMIOS_MODULE_1 --> Select eMios 1
 *                           MCU_EMIOS_MODULE_2 --> Select eMios 2
 *                           MCU_EMIOS_MODULE_3 --> Select eMios 3
 *
 * @param[in] u8Value        MCU_EMIOS_GPREN_BIT_ENABLE   --> Global Prescaler Enabled
 *                           MCU_EMIOS_GPREN_BIT_DISABLE  --> Global Prescaler Disabled
 *
 * @return                   void
 *
 */
FUNC( void, MCU_CODE ) Mcu_eMios_ConfigureGpren(VAR (uint8, AUTOMATIC) u8Module, VAR (uint8, AUTOMATIC) u8Value)
{
    /* Enter critical region*/
    SchM_Enter_Mcu_MCU_EXCLUSIVE_AREA_01();
    
    if (MCU_EMIOS_GPREN_BIT_ENABLE == u8Value)
    {
        /** @violates @ref MCU_EMIOS_C_REF_3 False positive error */
        /** @violates @ref MCU_EMIOS_C_REF_4 The cast is used to access memory mapped registers.*/
        REG_BIT_SET32(EMIOS_MCR_ADDR32[u8Module], EMIOS_MCR_GPREN_MASK32);
    }
    else
    {
        /** @violates @ref MCU_EMIOS_C_REF_3 False positive error */
        /** @violates @ref MCU_EMIOS_C_REF_4 The cast is used to access memory mapped registers.*/
        REG_BIT_CLEAR32(EMIOS_MCR_ADDR32[u8Module], EMIOS_MCR_GPREN_MASK32);
    }
    
    /* Exit critical region. */
    SchM_Exit_Mcu_MCU_EXCLUSIVE_AREA_01();
}
#endif

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