/**
*   @file    Eth_Irq.c
*   @version 1.0.0
*
*  @brief    AUTOSAR Eth driver interrupt handlers
*  @details  Implementation of interrupt handling routines for the Ethernet
*            Driver.
*
*   @addtogroup ETH_DRIVER
*   @{
*/
/*==================================================================================================
*   Project              : AUTOSAR 4.3 MCAL
*   Platform             : PA
*   Peripheral           : FEC
*   Dependencies         : 
*
*   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 Eth_Irq_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 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 Eth_Memmap.h is as
* per AUTOSAR requirement (SWS_MemMap_00003)
*
* @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.
*
* @section Eth_Irq_c_REF_2
* 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 Eth_Irq_c_REF_3
* Violates MISRA 2012 Advisory Rule 8.7,  Functions and objects should not be defined with external 
* linkage if they are referenced in only one translation unit.
*
*/

/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
#include "Eth.h" /* Includes also Eth_GeneralTypes.h, Eth_Cfg.h, 
                    Eth_ComStack_Types.h, Dem.h, Det.h and Mcal.h */
#include "Eth_Irq.h"     /* Own interface */
/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define ETH_VENDOR_ID_C                      43
#define ETH_MODULE_ID_C                      88
#define ETH_AR_RELEASE_MAJOR_VERSION_C       4
#define ETH_AR_RELEASE_MINOR_VERSION_C       3
#define ETH_AR_RELEASE_REVISION_VERSION_C    1
#define ETH_SW_MAJOR_VERSION_C               1
#define ETH_SW_MINOR_VERSION_C               0
#define ETH_SW_PATCH_VERSION_C               0

/*==================================================================================================
*                                     FILE VERSION CHECKS
==================================================================================================*/
/* Check if current file and ETH header file are of the same vendor */
#if (ETH_VENDOR_ID_C != ETH_VENDOR_ID)
    #error "Eth_Irq.c and Eth.h have different vendor ids"
#endif
/* Check if current file and ETH header file are of the same module */
#if (ETH_MODULE_ID_C != ETH_MODULE_ID)
    #error "Eth_Irq.c and Eth.h have different vendor ids"
#endif
/* Check if current file and ETH header file are of the same Autosar version */
#if ((ETH_AR_RELEASE_MAJOR_VERSION_C    != ETH_AR_RELEASE_MAJOR_VERSION) || \
     (ETH_AR_RELEASE_MINOR_VERSION_C    != ETH_AR_RELEASE_MINOR_VERSION) || \
     (ETH_AR_RELEASE_REVISION_VERSION_C != ETH_AR_RELEASE_REVISION_VERSION))
    #error "AutoSar Version Numbers of Eth_Irq.c and Eth.h are different"
#endif
/* Check if current file and ETH header file are of the same Software version */
#if ((ETH_SW_MAJOR_VERSION_C != ETH_SW_MAJOR_VERSION) || \
     (ETH_SW_MINOR_VERSION_C != ETH_SW_MINOR_VERSION) || \
     (ETH_SW_PATCH_VERSION_C != ETH_SW_PATCH_VERSION))
    #error "Software Version Numbers of Eth_Irq.c and Eth.h are different"
#endif
/* Check if current file and Eth_Irq header file are of the same Autosar version */
#if ((ETH_AR_RELEASE_MAJOR_VERSION_C    != ETH_AR_RELEASE_MAJOR_VERSION_ETHIRQ) || \
     (ETH_AR_RELEASE_MINOR_VERSION_C    != ETH_AR_RELEASE_MINOR_VERSION_ETHIRQ) || \
     (ETH_AR_RELEASE_REVISION_VERSION_C != ETH_AR_RELEASE_REVISION_VERSION_ETHIRQ))
    #error "AutoSar Version Numbers of Eth_Irq.c and Eth_Irq.h are different"
#endif
/* Check if current file and Eth_Irq header file are of the same Software version */
#if ((ETH_SW_MAJOR_VERSION_C != ETH_SW_MAJOR_VERSION_ETHIRQ) || \
     (ETH_SW_MINOR_VERSION_C != ETH_SW_MINOR_VERSION_ETHIRQ) || \
     (ETH_SW_PATCH_VERSION_C != ETH_SW_PATCH_VERSION_ETHIRQ))
    #error "Software Version Numbers of Eth_Irq.c and Eth_Irq.h are different"
#endif

#if STD_ON == ETH_DEM_EVENT_DETECT
    #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
        /* Check if current file and Dem header file are of the same version */
        #if ((ETH_AR_RELEASE_MAJOR_VERSION_C != DEM_AR_RELEASE_MAJOR_VERSION) || \
             (ETH_AR_RELEASE_MINOR_VERSION_C != DEM_AR_RELEASE_MINOR_VERSION))
            #error "AutoSar Version Numbers of Eth_Irq.c and Dem.h are different"
        #endif
    #endif /* DISABLE_MCAL_INTERMODULE_ASR_CHECK */
#endif /* ETH_DEM_EVENT_DETECT */

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

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


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


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


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


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


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


/*==================================================================================================
*                                   LOCAL FUNCTION PROTOTYPES
==================================================================================================*/
LOCAL_INLINE void Eth_RxIrqHdlr (CONST(uint8, AUTOMATIC) u8CtrlIdx, CONST(uint8, AUTOMATIC) u8DMAChanel);
LOCAL_INLINE void Eth_TxIrqHdlr (CONST(uint8, AUTOMATIC) u8CtrlIdx, CONST(uint8, AUTOMATIC) u8DMAChannel);

/*==================================================================================================
*                                       LOCAL FUNCTIONS
==================================================================================================*/
#define ETH_START_SEC_CODE
/* @violates @ref Eth_Irq_c_REF_1 This violation is not fixed since the inclusion of Eth_MemMap.h is as per AUTOSAR requirement*/
/* @violates @ref Eth_Irq_c_REF_2 Eth_MemMap.h is included after each section define in order to set the current memory section as defined by AUTOSAR */
#include "Eth_MemMap.h"

/**
* @brief         Reception interrupt handler for specific controller 
* @details       All receive buffers are checked and upper layers are notified
*                about received frames. Received data are passed to the upper
*                layers.
* @param[in]     u8CtrlIdx Index of controller to be processed
* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export" 
* the ISRs to let the application register them in the IVT (interrupt vector table).
*/
LOCAL_INLINE void Eth_RxIrqHdlr (CONST(uint8, AUTOMATIC) u8CtrlIdx, CONST(uint8, AUTOMATIC) u8DMAChanel)
{
    VAR(uint8, AUTOMATIC) u8QueueIdx;
    
    if (ETH_STATE_INIT != Eth_CtrlState[u8CtrlIdx])
    {
        /* Clear the interrupt flag to avoid isr flood on failure */
        Eth_Ipw_ClearRxInterruptFlag(u8CtrlIdx, u8DMAChanel);
    }
    else
    {
        /* Check whether the interrupt flag is set */
        if ((boolean)TRUE == Eth_Ipw_IsRxInterruptFlagSet(u8CtrlIdx, u8DMAChanel))
        { 
            Eth_Ipw_ClearRxInterruptFlag(u8CtrlIdx, u8DMAChanel); /* Clear the interrupt flag */
            /* Check whether the interrupt is enabled */
            if ((boolean)TRUE == Eth_Ipw_IsRxInterruptEnabled(u8CtrlIdx, u8DMAChanel))
            {  
                u8QueueIdx = (uint8)Eth_Ipw_FindQueueIdx(u8CtrlIdx, u8DMAChanel);
                /* The interrupt flag is set and the interrupt is enabled, 
                   proceed with the handler */
                /* Process the receive buffers.
                   This call also handles interrupt flags. */
                (void)Eth_Ipw_ReportReception(u8CtrlIdx, u8QueueIdx, (boolean)TRUE);
            }
        }
    }
}

/**
* @brief         Transmission interrupt handler for specific controller
* @details       All transmit buffers are checked and upper layers are notified
*                about successfully transmitted frames. Buffers containing
*                transmitted frames are unlocked after the confirmation.
* @param[in]     u8CtrlIdx Index of controller to be processed
* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export" 
* the ISRs to let the application register them in the IVT (interrupt vector table).
*/
LOCAL_INLINE void Eth_TxIrqHdlr (CONST(uint8, AUTOMATIC) u8CtrlIdx, CONST(uint8, AUTOMATIC) u8DMAChannel)
{
    if (ETH_STATE_INIT != Eth_CtrlState[u8CtrlIdx])
    {
        Eth_Ipw_ClearTxInterruptFlag(u8CtrlIdx, u8DMAChannel); /* Clear the interrupt flag */
    }
    else
    {
         /* Check whether the interrupt flag is set */
        if ((boolean)TRUE == Eth_Ipw_IsTxInterruptFlagSet(u8CtrlIdx, u8DMAChannel))
        {
            Eth_Ipw_ClearTxInterruptFlag(u8CtrlIdx, u8DMAChannel); /* Clear the interrupt flag */
            /* Check whether the interrupt is enabled */
            if ((boolean)TRUE == Eth_Ipw_IsTxInterruptEnabled(u8CtrlIdx, u8DMAChannel))
            {   /* The interrupt flag is set and the interrupt is enabled, 
                   proceed with the handler */
                /* There is no mean to report an error - ignore return value. 
                   The interrupt flag has been already cleared at the function
                   beginning */
                (void)Eth_Ipw_ReportTransmission(u8CtrlIdx, u8DMAChannel);      
            }   
        }
    }
}

/*==================================================================================================
*                                       GLOBAL FUNCTIONS
==================================================================================================*/

/*================================================================================================*/
/**
* @brief         Reception interrupt handler for the controller 0
* @details       All receive buffers are checked and upper layers are notified
*                about received frames. Received data are passed to the upper
*                layers.
* @isr
* 
*/
/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export" 
the ISRs to let the application register them in the IVT (interrupt vector table). */
ISR(Eth_RxIrqHdlr_0)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)0U);
    
    EXIT_INTERRUPT(); 
}
/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_RxIrqHdlr_1)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)1U);
    
    EXIT_INTERRUPT(); 
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_RxIrqHdlr_2)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)2U);
    
    EXIT_INTERRUPT(); 
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_RxIrqHdlr_3)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)3U);
    
    EXIT_INTERRUPT(); 
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_RxIrqHdlr_4)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)4U);
    
    EXIT_INTERRUPT(); 
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_RxIrqHdlr_5)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)5U);
    
    EXIT_INTERRUPT(); 
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_RxIrqHdlr_6)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)6U);
    
    EXIT_INTERRUPT(); 
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_RxIrqHdlr_7)
{
    Eth_RxIrqHdlr((uint8)0U, (uint8)7U);
    
    EXIT_INTERRUPT(); 
}

/*================================================================================================*/
/**
* @brief         Transmission interrupt handler for the controller 0
* @details       All transmit buffers are checked and upper layers are notified
*                about successfully transmitted frames. Buffers containing
*                transmitted frames are unlocked after the confirmation.
* @isr
* 
*/
/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export" 
the ISRs to let the application register them in the IVT (interrupt vector table). */
ISR(Eth_TxIrqHdlr_0)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)0U);
    
    EXIT_INTERRUPT();     
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_TxIrqHdlr_1)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)1U);
    
    EXIT_INTERRUPT();     
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_TxIrqHdlr_2)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)2U);
    
    EXIT_INTERRUPT();     
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_TxIrqHdlr_3)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)3U);
    
    EXIT_INTERRUPT();     
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_TxIrqHdlr_4)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)4U);
    
    EXIT_INTERRUPT();     
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_TxIrqHdlr_5)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)5U);
    
    EXIT_INTERRUPT();     
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_TxIrqHdlr_6)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)6U);
    
    EXIT_INTERRUPT();     
}

/* @violates @ref Eth_Irq_c_REF_3  For ISRs (Eth_Irq.c): These functions represent the ISRs of the driver. External linkage is needed to "export"*/
ISR(Eth_TxIrqHdlr_7)
{
    Eth_TxIrqHdlr((uint8)0U, (uint8)7U);
    
    EXIT_INTERRUPT();     
}
#define ETH_STOP_SEC_CODE
/* @violates @ref Eth_Irq_c_REF_1 This violation is not fixed since the inclusion of Eth_MemMap.h is as per AUTOSAR requirement*/
/* @violates @ref Eth_Irq_c_REF_2 Eth_MemMap.h is included after each section define in order to set the current memory section as defined by AUTOSAR */
#include "Eth_MemMap.h"

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