/*!
* @file     Icu_Eint.c
*
* @version  1.0.0
*
* @brief    The source file of the EINT driver
*
* @attention
*
* Project Name      : AUTOSAR MCAL
*
* Platform          : Arm
*
* Autosar Standard  : Classic Platform 4.4.0
*
* Compiled Version  : G32A10xx_MCAL_1_0_0_01-Otc-25
*
* Copyright (C) 2026 Geehy Semiconductor
*
* You may not use this file except in compliance with the
* GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
*
* The program is only for reference, which is distributed in the hope
* that it will be usefull and instructional for customers to develop
* their software. Unless required by applicable law or agreed to in
* writing, the program is distributed on an "AS IS" BASIS, WITHOUT
* ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
* See the Geehy Semiconductor Software License Agreement for the governing permissions
* and limitations under the License.
*/

#ifdef __cplusplus
extern "C"{
#endif

#include "Mcal.h"
#include "StdRegMacros.h"
#include "Icu_Eint.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup ICU_Driver
  * @brief ICU driver modules
  @{
*/

/** @defgroup ICU_Macros
  @{
*/
/***********************************************************************************************************************
                                    VENDOR ID & AUTOSAR VERSION INFO
***********************************************************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define ICU_EINT_VENDOR_ID                        0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define ICU_EINT_AR_RELEASE_MAJOR_VERSION         0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define ICU_EINT_AR_RELEASE_MINOR_VERSION         0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define ICU_EINT_AR_RELEASE_REVISION_VERSION      0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */ 
#define ICU_EINT_SW_MAJOR_VERSION                 0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define ICU_EINT_SW_MINOR_VERSION                 0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define ICU_EINT_SW_PATCH_VERSION                 0x00U

/**@} end of group ICU_Macros */

/**********************************************************************************************************************
                                        VERSION INFOMATION VERIFICATION
***********************************************************************************************************************/
#ifdef ENABLE_INTERMODULE_MCAL_CHECK
    /* Make sure the AUTOSAR major version number is the same between Icu_Eint.c and Mcal.h */
    #if (ICU_EINT_AR_RELEASE_MAJOR_VERSION != MCAL_H_AR_RELEASE_MAJOR_VERSION)
        #error "AUTOSAR Major Version Numbers mismatch between Icu_Eint.c and Mcal.h"
    #endif
    /* Make sure the AUTOSAR minor version number is the same between Icu_Eint.c and Mcal.h */
    #if (ICU_EINT_AR_RELEASE_MINOR_VERSION != MCAL_H_AR_RELEASE_MINOR_VERSION)
        #error "AUTOSAR Minor Version Numbers mismatch between Icu_Eint.c and Mcal.h"
    #endif
    
    /* Make sure the AUTOSAR major version number is the same between Icu_Eint.c and StdRegMacros.h */
    #if (ICU_EINT_AR_RELEASE_MAJOR_VERSION != STD_REG_MACROS_H_AR_RELEASE_MAJOR_VERSION)
        #error "AUTOSAR Major Version Numbers mismatch between Icu_Eint.c and StdRegMacros.h"
    #endif
    /* Make sure the AUTOSAR minor version number is the same between Icu_Eint.c and StdRegMacros.h */
    #if (ICU_EINT_AR_RELEASE_MINOR_VERSION != STD_REG_MACROS_H_AR_RELEASE_MINOR_VERSION)
        #error "AUTOSAR Minor Version Numbers mismatch between Icu_Eint.c and StdRegMacros.h"
    #endif
#endif

/* Make sure the vendor id is the same between Icu_Eint.c and Icu_Eint.h */
#if (ICU_EINT_VENDOR_ID != ICU_EINT_H_VENDOR_ID)
    #error "Vendor ids mismatch between Icu_Eint.c and Icu_Eint.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Icu_Eint.c and Icu_Eint.h */
#if (ICU_EINT_AR_RELEASE_MAJOR_VERSION != ICU_EINT_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Numbers mismatch between Icu_Eint.c and Icu_Eint.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Icu_Eint.c and Icu_Eint.h */
#if (ICU_EINT_AR_RELEASE_MINOR_VERSION != ICU_EINT_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Numbers mismatch between Icu_Eint.c and Icu_Eint.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Icu_Eint.c and Icu_Eint.h */
#if (ICU_EINT_AR_RELEASE_REVISION_VERSION != ICU_EINT_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Numbers mismatch between Icu_Eint.c and Icu_Eint.h"
#endif
/* Make sure the software major version number is the same between Icu_Eint.c and Icu_Eint.h */
#if (ICU_EINT_SW_MAJOR_VERSION != ICU_EINT_H_SW_MAJOR_VERSION)
    #error "Software Major Version Numbers mismatch between Icu_Eint.c and Icu_Eint.h"
#endif
/* Make sure the software minor version number is the same between Icu_Eint.c and Icu_Eint.h */
#if (ICU_EINT_SW_MINOR_VERSION != ICU_EINT_H_SW_MINOR_VERSION)
    #error "Software Minor Version Numbers mismatch between Icu_Eint.c and Icu_Eint.h"
#endif
/* Make sure the software patch version number is the same between Icu_Eint.c and Icu_Eint.h */
#if (ICU_EINT_SW_PATCH_VERSION != ICU_EINT_H_SW_PATCH_VERSION)
    #error "Software Patch Version Numbers mismatch between Icu_Eint.c and Icu_Eint.h"
#endif

/**********************************************************************************************************************
                                                    GLOBAL FUNCTION
***********************************************************************************************************************/
#define ICU_START_SEC_CODE
#include "Icu_MemMap.h"

/** @defgroup ICU_Fuctions
  @{
*/

/*!
* @brief         Set the EINT peripheral registers to their default reset values
*
* @param[in]     None
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_Reset(void)
{
    REG_WRITE32(EINT_IMASK_REG_ADDR, EINT_INTMASK_RESET_VALUE);
    REG_WRITE32(EINT_EMASK_REG_ADDR, EINT_EVTMASK_RESET_VALUE);
    REG_WRITE32(EINT_RTEN_REG_ADDR,  EINT_RTSEL_RESET_VALUE);
    REG_WRITE32(EINT_FTEN_REG_ADDR,  EINT_FTSEL_RESET_VALUE);
    REG_WRITE32(EINT_IPEND_REG_ADDR, EINT_PEND_RESET_VALUE);
}

/*!
* @brief         Configure the EINT
*
* @param[in]     EintConfigPtr    Pointer to Icu_Eint_ConfigType structure
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_Config(P2CONST(Icu_Eint_ConfigType, AUTOMATIC, ICU_APPL_CONST) EintConfigPtr)
{
    if (EintConfigPtr->lineCmd  == (uint8)FUN_DISABLE)
    {
        if (EintConfigPtr->mode == EINT_MODE_INTERRUPT)
        {
            REG_BIT_CLEAR32(EINT_IMASK_REG_ADDR, EintConfigPtr->line);
        }
        else if (EintConfigPtr->mode == EINT_MODE_EVENT)
        {
            REG_BIT_CLEAR32(EINT_EMASK_REG_ADDR, EintConfigPtr->line);
        }
        else
        {
            /* Nothing to do */
        }
    }
    else
    {
        if (EintConfigPtr->mode == EINT_MODE_INTERRUPT)
        {
            REG_BIT_SET32(EINT_IMASK_REG_ADDR, EintConfigPtr->line);
        }
        else if (EintConfigPtr->mode == EINT_MODE_EVENT)
        {
            REG_BIT_SET32(EINT_EMASK_REG_ADDR, EintConfigPtr->line);
        }
        else
        {
            /* Nothing to do */
        }

        if (EintConfigPtr->trigger == EINT_TRIGGER_RISING)
        {
            REG_BIT_SET32(EINT_RTEN_REG_ADDR, EintConfigPtr->line);
        }
        else if (EintConfigPtr->trigger == EINT_TRIGGER_FALLING)
        {
            REG_BIT_SET32(EINT_FTEN_REG_ADDR, EintConfigPtr->line);
        }
        else
        {
            REG_BIT_SET32(EINT_RTEN_REG_ADDR, EintConfigPtr->line);
            REG_BIT_SET32(EINT_FTEN_REG_ADDR, EintConfigPtr->line);
        }
    }
}

/*!
* @brief         Fills each Icu_Eint_ConfigType member with its default value
*
* @param[in]     EintConfigPtr    Pointer to a Icu_Eint_ConfigType structure which will be initialized
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_ConfigStructInit(P2VAR(Icu_Eint_ConfigType, AUTOMATIC, ICU_APPL_CONST) EintConfigPtr)
{
    EintConfigPtr->line    = EINT_LINENONE;
    EintConfigPtr->mode    = EINT_MODE_INTERRUPT;
    EintConfigPtr->trigger = EINT_TRIGGER_FALLING;
    EintConfigPtr->lineCmd = (uint8)FUN_DISABLE;
}

/*!
* @brief         Select software interrupt on EINT line
*
* @param[in]     Line   specifies the EINT line on which the software interrupt will be generateds
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_SelectSWInterrupt(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_SET32(EINT_SWINTE_REG_ADDR, (uint32)Line);
}

/*!
* @brief         Enable the specified EINT line interrupt
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_EnableInt(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_SET32(EINT_IMASK_REG_ADDR, Line);
}

/*!
* @brief         Disable the specified EINT line interrupt
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_DisableInt(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_CLEAR32(EINT_IMASK_REG_ADDR, Line);
}

/*!
* @brief         Get the specified EINT line interrupt enable status
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*
* @retval   status: The new state of flag (SET or RESET)
*/
FUNC(Icu_Eint_FlagType, ICU_CODE) Icu_Eint_GetIntEnSts(VAR(uint32, AUTOMATIC) Line)
{
    Icu_Eint_FlagType status = RESET_STS;

    if ((REG_BIT_GET32(EINT_IMASK_REG_ADDR, Line) != (uint32)RESET_STS))
    {
        status = SET_STS;
    }
    else
    {
        status = RESET_STS;
    }

    return status;
}

/*!
* @brief         Enable the specified EINT line event
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_EnableEvent(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_SET32(EINT_EMASK_REG_ADDR, Line);
}

/*!
* @brief         Disable the specified EINT line event
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_DisableEvent(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_CLEAR32(EINT_EMASK_REG_ADDR, Line);
}

/*!
* @brief         Enable the specified EINT line rising edge trigger
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_EnableRisingTrig(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_SET32(EINT_RTEN_REG_ADDR, Line);
}

/*!
* @brief         Disable the specified EINT line rising edge trigger
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_DisableRisingTrig(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_CLEAR32(EINT_RTEN_REG_ADDR, Line);
}

/*!
* @brief         Enable the specified EINT line falling edge trigger
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_EnableFailingTrig(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_SET32(EINT_FTEN_REG_ADDR, Line);
}

/*!
* @brief         Disable the specified EINT line falling edge trigger
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_DisableFailingTrig(VAR(uint32, AUTOMATIC) Line)
{
    REG_BIT_CLEAR32(EINT_FTEN_REG_ADDR, Line);
}

/*!
* @brief         Read the specified EINT line flag
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*
* @retval   status: The new state of flag (SET or RESET)
*/
FUNC(Icu_Eint_FlagType, ICU_CODE) Icu_Eint_ReadStatusFlag(VAR(uint32, AUTOMATIC) Line)
{
    Icu_Eint_FlagType status = RESET_STS;

    if ((REG_READ32(EINT_IPEND_REG_ADDR) & Line) != (uint32)RESET_STS)
    {
        status = SET_STS;
    }
    else
    {
        status = RESET_STS;
    }

    return status;
}

/*!
* @brief         Clears the EINT line pending bits
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE) Icu_Eint_ClearStatusFlag(VAR(uint32, AUTOMATIC) Line)
{
    REG_WRITE32(EINT_IPEND_REG_ADDR, Line);
}

/*!
* @brief         Read the specified EINT line interrupt flag
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*
* @retval   SET_STS / RESET_STS
*/
FUNC(Icu_Eint_FlagType, ICU_CODE) Icu_Eint_ReadIntFlag(VAR(uint32, AUTOMATIC) Line)
{
    Icu_Eint_FlagType status = RESET_STS;
    uint32 enablestatus = 0;

    enablestatus = REG_BIT_GET32(EINT_IMASK_REG_ADDR, Line);

    if(REG_BIT_GET32(EINT_IPEND_REG_ADDR, Line) == (uint32)RESET_STS)
    {
        status = RESET_STS;
    }
    else if(enablestatus == (uint32)RESET_STS)
    {
        status = RESET_STS;
    }
    else
    {
        status = SET_STS;
    }

    return status;
}

/*!
* @brief         Clears the EINT line pending bits
*
* @param[in]     Line   Select the EINT line
* @param[out]    None
* @param[in,out] None
*/
FUNC(void, ICU_CODE)  Icu_Eint_ClearIntFlag(VAR(uint32, AUTOMATIC) Line)
{
    REG_WRITE32(EINT_IPEND_REG_ADDR, Line);
}

/**@} end of group ICU_Fuctions */

#define ICU_STOP_SEC_CODE
#include "Icu_MemMap.h"

#ifdef __cplusplus
}
#endif

/**@} end of group ICU_Driver */
/**@} end of group G32A10xx_StdPeriphDriver */
