/**
*   @file    Port_Igf.c
*
*   @version 1.0.0
*
*   @brief   AUTOSAR Port - IGF Low level driver implementation.
*   @details Implementation of the IGF low level driver.
*   @{
*/
/*=================================================================================================
*   Project              : AUTOSAR 4.3 MCAL
*   Platform             : PA
*   Peripheral           : SIU,IGF
*   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 Port_Igf_c_REF_1
*          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 violation is not fixed since the inclusion of 
*          Port_MemMap.h is as per AUTOSAR requirement [SWS_MemMap_00003].
*
* @section Port_Igf_c_REF_2
*          Violates MISRA 2012 Advisory Rule 20.1, #include directives should only be preceded by preprocessor
*          directives or comments. Port_MemMap.h is included after each section define in order to set the current 
*          memory section as defined by AUTOSAR
*
* @section Port_Igf_c_REF_3
*          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 Port_Igf_c_REF_4
*          Violates MISRA 2012 Required Rule 11.6, A cast shall not be performed between pointer to void and an
*          arithmetic type. This macro computes the address of any register by using the hardware offset of the controller. 
*          The address calculated as an unsigned int is passed to a macro for initializing the pointer with that address.
*
* @section Port_Igf_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 exporting the externally configured structures 
*          used by the "Port_Init" function.
*
* @section [global]
*          Violates MISRA 2012 Required Rule 5.1, External identifiers shall be distinct.
*          The used compilers use more than 31 chars for identifiers.
*
* @section [global]
*          Violates MISRA 2012 Required Rule 5.2, Identifiers declared in the same scope and name space shall be distinct.
*          The used compilers use more than 31 chars for identifiers.
*
* @section [global]
*          Violates MISRA 2012 Required Rule 5.4, Macro identifiers shall be distinct.
*          The used compilers use more than 31 chars for identifiers.
*
* @section [global]
*          Violates MISRA 2012 Required Rule 5.5, Identifiers shall be distinct from macro names.
*          The used compilers use more than 31 chars for identifiers.
*/

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

/*=================================================================================================
                               SOURCE FILE VERSION INFORMATION
=================================================================================================*/
/*
* @brief Parameters that shall be published within the driver header file and also in the
* module's description file
*/
#define PORT_IGF_VENDOR_ID_C                     43
#define PORT_IGF_AR_RELEASE_MAJOR_VERSION_C      4
#define PORT_IGF_AR_RELEASE_MINOR_VERSION_C      3
#define PORT_IGF_AR_RELEASE_REVISION_VERSION_C   1
#define PORT_IGF_SW_MAJOR_VERSION_C              1
#define PORT_IGF_SW_MINOR_VERSION_C              0
#define PORT_IGF_SW_PATCH_VERSION_C              0

/*=================================================================================================
*                                      FILE VERSION CHECKS
=================================================================================================*/
#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    /* Check if the files Port_Igf.c and Std_Types.h are of the same version */
   #if ((PORT_IGF_AR_RELEASE_MAJOR_VERSION_C != STD_AR_RELEASE_MAJOR_VERSION) || \
        (PORT_IGF_AR_RELEASE_MINOR_VERSION_C != STD_AR_RELEASE_MINOR_VERSION)    \
       )
       #error "AutoSar Version Numbers of Port_Igf.c and Std_Types.h are different"
   #endif
    /* Check if the files Port_Igf.c and StdRegMacros.h are of the same version */
   #if ((PORT_IGF_AR_RELEASE_MAJOR_VERSION_C != STDREGMACROS_AR_RELEASE_MAJOR_VERSION) || \
        (PORT_IGF_AR_RELEASE_MINOR_VERSION_C != STDREGMACROS_AR_RELEASE_MINOR_VERSION)    \
       )
       #error "AutoSar Version Numbers of Port_Igf.c and StdRegMacros.h are different"
   #endif
#endif


/* Check if Port_Igf.c and Port_Igf.h are of the same vendor */
#if (PORT_IGF_VENDOR_ID_C != PORT_IGF_VENDOR_ID_H)
    #error "Port_Igf.c and Port_Igf.h have different vendor ids"
#endif
/* Check if Port_Igf.c and Port_Igf.h are of the same Autosar version */
#if ((PORT_IGF_AR_RELEASE_MAJOR_VERSION_C    != PORT_IGF_AR_RELEASE_MAJOR_VERSION_H) || \
     (PORT_IGF_AR_RELEASE_MINOR_VERSION_C    != PORT_IGF_AR_RELEASE_MINOR_VERSION_H) || \
     (PORT_IGF_AR_RELEASE_REVISION_VERSION_C != PORT_IGF_AR_RELEASE_REVISION_VERSION_H) \
    )
  #error "AutoSar Version Numbers of Port_Igf.c and Port_Igf.h are different"
#endif
/* Check if Port_Igf.c and Port_Igf.h are of the same software version */
#if ((PORT_IGF_SW_MAJOR_VERSION_C != PORT_IGF_SW_MAJOR_VERSION_H) || \
     (PORT_IGF_SW_MINOR_VERSION_C != PORT_IGF_SW_MINOR_VERSION_H) || \
     (PORT_IGF_SW_PATCH_VERSION_C != PORT_IGF_SW_PATCH_VERSION_H)    \
    )
  #error "Software Version Numbers of Port_Igf.c and Port_Igf.h are different"
#endif

/* Check if Port_Igf.c and Port_Reg_eSys_Igf.h are of the same vendor */
#if (PORT_IGF_VENDOR_ID_C != PORT_REGESYS_IGF_VENDOR_ID)
    #error "Port_Igf.c and Port_Reg_eSys_Igf.h have different vendor ids"
#endif
/* Check if Port_Igf.c and Port_Reg_eSys_Igf.h are of the same Autosar version */
#if ((PORT_IGF_AR_RELEASE_MAJOR_VERSION_C    != PORT_REGESYS_IGF_AR_RELEASE_MAJOR_VERSION) || \
     (PORT_IGF_AR_RELEASE_MINOR_VERSION_C    != PORT_REGESYS_IGF_AR_RELEASE_MINOR_VERSION) || \
     (PORT_IGF_AR_RELEASE_REVISION_VERSION_C != PORT_REGESYS_IGF_AR_RELEASE_REVISION_VERSION) \
    )
   #error "AutoSar Version Numbers of Port_Igf.c and Port_Reg_eSys_Igf.h are different"
#endif
/* Check if Port_Igf.c and Port_Reg_eSys_Igf.h are of the same software version */
#if ((PORT_IGF_SW_MAJOR_VERSION_C != PORT_REGESYS_IGF_SW_MAJOR_VERSION) || \
     (PORT_IGF_SW_MINOR_VERSION_C != PORT_REGESYS_IGF_SW_MINOR_VERSION) || \
     (PORT_IGF_SW_PATCH_VERSION_C != PORT_REGESYS_IGF_SW_PATCH_VERSION)    \
    )
  #error "Software Version Numbers of Port_Igf.c and Port_Reg_eSys_Igf.h are different"
#endif

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


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


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


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


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


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

/**
* @brief IGF base address array declaration
*/
/** @violates @ref Port_Igf_c_REF_5 An object should be defined at block scope if its identifier only appears in a single function. */
static CONST(uint32, PORT_CONST) IGF_BASE_ADDR32[] = 
{
#ifdef  IGF0_BASEADDR
    /** @brief check if first IGF is available on the platform */
    IGF0_BASEADDR,
#endif

#ifdef  IGF_1_BASEADDR
    /** @brief check if second IGF is available on the platform */
    IGF_1_BASEADDR,
#endif
};

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

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


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


/*=================================================================================================
                                       GLOBAL FUNCTIONS
=================================================================================================*/
#define PORT_START_SEC_CODE
/** @violates @ref Port_Igf_c_REF_1 Precautions shall be taken in order to prevent the contents of a header file being included more than once. */
/** @violates @ref Port_Igf_c_REF_2 #include directives should only be preceded by preprocessor directives or comments. */
#include "Port_MemMap.h"

/**
* @brief        Initializes the IGF IP Driver
* @details      The function Port_Igf_Init will initialize ALL IGF channels with the
*               configuration set pointed to by the parameter pIgfConfigPtr.
*
* @param[in]    pIgfConfigPtr  A pointer to the structure which contains initialization parameters
*
* @return none
*
* @pre    none
*
*
*/
FUNC(void, PORT_CODE) Port_Igf_Init
(
    P2CONST(Port_IgfConfigType, AUTOMATIC, PORT_APPL_CONST) pIgfConfigPtr
)
{
    VAR(uint8,  AUTOMATIC) u8ChannelIndex;
    VAR(uint8,  AUTOMATIC) u8NumConfigChannels = (uint8)(pIgfConfigPtr->u8NumChannels);
    VAR(uint32, AUTOMATIC) u32RegValue;
    VAR(uint8,  AUTOMATIC) u8IfgInstance;
    VAR(uint8,  AUTOMATIC) u8IgfChannel;
    VAR(uint32, AUTOMATIC) u32RegIgfMcrAddress;
    VAR(uint32, AUTOMATIC) u32RegIgfPresrAddress;
    VAR(uint32, AUTOMATIC) u32RegIgfRthAddress;
    VAR(uint32, AUTOMATIC) u32RegIgfFthAddress;

    /* Initialize all IGF channels present in the configuration */
    for (u8ChannelIndex = (uint8)0U; u8ChannelIndex < u8NumConfigChannels; u8ChannelIndex++)
    {
        u8IfgInstance = (uint8)pIgfConfigPtr->pChannelConfig[u8ChannelIndex].u8IgfInstance;
        u8IgfChannel = (uint8)pIgfConfigPtr->pChannelConfig[u8ChannelIndex].u8IgfChannel;
        u32RegIgfMcrAddress = IGF_MCR_ADDR32(u8IfgInstance,u8IgfChannel);
        u32RegIgfPresrAddress = IGF_PRESR_ADDR32(u8IfgInstance,u8IgfChannel);
        u32RegIgfRthAddress = IGF_RTH_ADDR32(u8IfgInstance,u8IgfChannel);
        u32RegIgfFthAddress = IGF_FTH_ADDR32(u8IfgInstance,u8IgfChannel);
        /* Make sure that the IGF channel is disabled so we can update it with the configuration params */
        /** @violates @ref Port_Igf_c_REF_3 A conversion should not be performed between a pointer to object and an integer type. */
        /** @violates @ref Port_Igf_c_REF_4 A cast shall not be performed between pointer to void and an arithmetic type. */
        REG_BIT_CLEAR32(u32RegIgfMcrAddress, IGF_MCR_FGEN_ON_U32);

        /* Prepare and write the MCR register */
        u32RegValue = (uint32)pIgfConfigPtr->pChannelConfig[u8ChannelIndex].u32IgfMCR;
        /** @violates @ref Port_Igf_c_REF_3 A conversion should not be performed between a pointer to object and an integer type. */
        /** @violates @ref Port_Igf_c_REF_4 A cast shall not be performed between pointer to void and an arithmetic type. */
        REG_WRITE32(u32RegIgfMcrAddress, u32RegValue);
        
        /* Prepare and write the PRESR register, only if the register should be configured */
        u32RegValue = (uint32)pIgfConfigPtr->pChannelConfig[u8ChannelIndex].u32IgfPRESR;
        if(PORT_IGF_REG_NOT_CONFIGURED_U32 != u32RegValue)
        {
            /** @violates @ref Port_Igf_c_REF_3 A conversion should not be performed between a pointer to object and an integer type. */
            /** @violates @ref Port_Igf_c_REF_4 A cast shall not be performed between pointer to void and an arithmetic type. */
            REG_WRITE32(u32RegIgfPresrAddress, u32RegValue);
        }

        /* Prepare and write the RTH register, only if the register should be configured */
        u32RegValue = (uint32)pIgfConfigPtr->pChannelConfig[u8ChannelIndex].u32IgfRTH;
        if(PORT_IGF_REG_NOT_CONFIGURED_U32 != u32RegValue)
        {
            /** @violates @ref Port_Igf_c_REF_3 A conversion should not be performed between a pointer to object and an integer type. */
            /** @violates @ref Port_Igf_c_REF_4 A cast shall not be performed between pointer to void and an arithmetic type. */
            REG_WRITE32(u32RegIgfRthAddress, u32RegValue);
        }
        
        /* Prepare and write the FTH register, only if the register should be configured */
        u32RegValue = (uint32)pIgfConfigPtr->pChannelConfig[u8ChannelIndex].u32IgfFTH;
        if(PORT_IGF_REG_NOT_CONFIGURED_U32 != u32RegValue)
        {
            /** @violates @ref Port_Igf_c_REF_3 A conversion should not be performed between a pointer to object and an integer type. */
            /** @violates @ref Port_Igf_c_REF_4 A cast shall not be performed between pointer to void and an arithmetic type. */
            REG_WRITE32(u32RegIgfFthAddress, u32RegValue);
        }

        /* Enable the IGF Channel by setting the FGEN bit */
        /** @violates @ref Port_Igf_c_REF_3 A conversion should not be performed between a pointer to object and an integer type. */
        /** @violates @ref Port_Igf_c_REF_4 A cast shall not be performed between pointer to void and an arithmetic type. */
        REG_BIT_SET32(u32RegIgfMcrAddress, IGF_MCR_FGEN_ON_U32);
    }
}


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

#ifdef __cplusplus
}
#endif
/** @} */
