/*!
* @file     Mcu_Irq.c
*
* @version  1.0.0
*
* @brief    The underlying driver code of the mcu irq module.
*
* @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 "Mcu.h"
#include "Mcu_Cfg.h"
#include "Std_Types.h"

#include "Mcu_RcmTypes.h"
#include "Mcu_RcmReg.h"

#include "Mcu_PmuTypes.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @addtogroup MCU_Driver
  * @brief MCU driver modules
  @{
*/

/** @defgroup MCU_Macros
  @{
*/
/*******************************************************************************
                    VENDOR ID & AUTOSAR VERSION INFO
*******************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define MCU_IRQ_VENDOR_ID                              0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_IRQ_AR_RELEASE_MAJOR_VERSION               0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_IRQ_AR_RELEASE_MINOR_VERSION               0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_IRQ_AR_RELEASE_REVISION_VERSION            0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */ 
#define MCU_IRQ_SW_MAJOR_VERSION                       0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_IRQ_SW_MINOR_VERSION                       0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_IRQ_SW_PATCH_VERSION                       0x00U

/**@} end of group MCU_Macros */
/*******************************************************************************
                               LOCAL FUNCTIONS
*******************************************************************************/
/** @defgroup MCU_Fuctions
  @{
*/

#define MCU_START_SEC_VAR_INIT_UNSPECIFIED
#include "Mcu_MemMap.h"

#ifdef MCU_ERR_ISR_NOTIFICATION
extern FUNC(void, MCU_CODE) MCU_ERR_ISR_NOTIFICATION(Mcu_Rcm_IrqType errCode);
#endif

#ifdef MCU_RCM_ISR_NOTIFICATION
extern FUNC(void, MCU_CODE) MCU_RCM_ISR_NOTIFICATION(Mcu_Rcm_IrqType code);
#endif

#ifdef MCU_CMU_ISR_NOTIFICATION
extern FUNC(void, MCU_CODE) MCU_CMU_ISR_NOTIFICATION(Mcu_Rcm_IrqType code);
#endif

#ifdef MCU_CSS_ISR_NOTIFICATION
extern FUNC(void, MCU_CODE) MCU_CSS_ISR_NOTIFICATION();
#endif

#define MCU_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "Mcu_MemMap.h"


#ifdef MCU_ERR_ISR_NOTIFICATION
static FUNC(void, USART_CODE) Mcu_Irq_ErrorHandler(void)
{
    if (REG_BIT_GET32(RCM_INT2_REG_ADDR, RCM_INT2_FLLFLG_MASK))
    {
        MCU_ERR_ISR_NOTIFICATION(RCM_FREQ_UNDER_ERR_IRQ);
        REG_BIT_SET32(RCM_INT2_REG_ADDR, RCM_INT2_FLLCLR_MASK);
    }
    else if (REG_BIT_GET32(RCM_INT2_REG_ADDR, RCM_INT2_FHHFLG_MASK))
    {
        MCU_ERR_ISR_NOTIFICATION(RCM_FREQ_OVER_ERR_IRQ);
        REG_BIT_SET32(RCM_INT2_REG_ADDR, RCM_INT2_FHHCLR_MASK);
    }
    else if (REG_BIT_GET32(RCM_INT2_REG_ADDR, RCM_INT2_LOCFLG_MASK))
    {
        MCU_ERR_ISR_NOTIFICATION(RCM_CLOCK_LOST_ERR_IRQ);
        REG_BIT_SET32(RCM_INT2_REG_ADDR, RCM_INT2_LOCCLR_MASK);
    }
    else if (REG_BIT_GET32(RCM_INT1_REG_ADDR, RCM_INT1_CSSFLG_MASK))
    {
        MCU_ERR_ISR_NOTIFICATION(RCM_CLOCK_SYS_ERR_IRQ);
        REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_CSSCLR_MASK);
    }
    else
    {
        /* nothing */
    }
}
#endif


#ifdef MCU_RCM_ISR_NOTIFICATION
static FUNC(void, USART_CODE) Mcu_Irq_ClockHandler(void)
{
    if (REG_BIT_GET32(RCM_INT1_REG_ADDR, RCM_INT1_LSIRDYFLG_MASK))
    {
        MCU_RCM_ISR_NOTIFICATION(RCM_LSI_READY_INT_IRQ);
        REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_LSIRDYCLR_MASK);
    }
    else if (REG_BIT_GET32(RCM_INT1_REG_ADDR, RCM_INT1_HSIRDYFLG_MASK))
    {
        MCU_RCM_ISR_NOTIFICATION(RCM_HSI_READY_INT_IRQ);
        REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_HSIRDYCLR_MASK);
    }
    else if (REG_BIT_GET32(RCM_INT1_REG_ADDR, RCM_INT1_HSERDYFLG_MASK))
    {
        MCU_RCM_ISR_NOTIFICATION(RCM_HSE_READY_INT_IRQ);
        REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_HSERDYCLR_MASK);
    }
    else if (REG_BIT_GET32(RCM_INT1_REG_ADDR, RCM_INT1_PLLRDYFLG_MASK))
    {
        MCU_RCM_ISR_NOTIFICATION(RCM_PLL_READY_INT_IRQ);
        REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_PLLRDYCLR_MASK);
    }
    else if (REG_BIT_GET32(RCM_INT1_REG_ADDR, RCM_INT1_HSI14RDYFLG_MASK))
    {
        MCU_RCM_ISR_NOTIFICATION(RCM_HSI14_READY_INT_IRQ);
        REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_HSI14RDYCLR_MASK);
    }
    else
    {
        /* nothing */
    }
}
#endif


#ifdef MCU_CMU_ISR_NOTIFICATION
static FUNC(void, USART_CODE) Mcu_Irq_CmuHandler(void)
{
    if (REG_BIT_GET32(RCM_INT2_REG_ADDR, RCM_INT2_LOCFLG_MASK))
    {
        MCU_CMU_ISR_NOTIFICATION(RCM_CMU_LOC_INT_IRQ);
        REG_BIT_SET32(RCM_INT2_REG_ADDR, RCM_INT2_LOCCLR_MASK);
        REG_BIT_SET32(RCM_CFG4_REG_ADDR, RCM_CFG4_LOC_MASK);
    }
    else
    {
        /* nothing */
    }
}
#endif

/**@} end of group MCU_Fuctions */
/*******************************************************************************
                              GLOBAL FUNCTION
*******************************************************************************/
#define MCU_START_SEC_CODE
#include "Mcu_MemMap.h"

/** @defgroup MCU_Fuctions
  @{
*/

/*!
* @brief        This function handles RCM interrupt Handler
*
* @param        None
*
*/
FUNC(void, USART_CODE) RCM_IRQHandler(void)
{
#ifdef MCU_ERR_ISR_NOTIFICATION
    Mcu_Irq_ErrorHandler();
#endif
    
#ifdef MCU_RCM_ISR_NOTIFICATION
    Mcu_Irq_ClockHandler();
#endif
    
#ifdef MCU_CMU_ISR_NOTIFICATION
    Mcu_Irq_CmuHandler();
#endif
}

FUNC(void, USART_CODE) PVD_IRQHandler(void)
{
    g_mcuConfig.mcuPmuCfg->mcuLvdNotification();
}

#if (STD_ON == MCU_CSS_MODE_ENABLE)
FUNC(void, USART_CODE) NMI_Handler(void)
{
    if (REG_BIT_GET32(RCM_INT1_REG_ADDR, RCM_INT1_CSSFLG_MASK))
    {
        if (((REG_BIT_GET32(RCM_CTRL1_REG_ADDR, RCM_CTRL1_HSERDYFLG_MASK))) == (uint8)1U)
        {
            REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_CSSCLR_MASK);
        }
        else
        {
            MCU_CSS_ISR_NOTIFICATION();
            REG_BIT_SET32(RCM_INT1_REG_ADDR, RCM_INT1_CSSCLR_MASK);
        }
    }
    else
    {
        /* nothing */
    }
}
#endif

/**@} end of group MCU_Fuctions */

#define MCU_STOP_SEC_CODE
#include "Mcu_MemMap.h"

#ifdef __cplusplus
}
#endif

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