/*!
* @file     Irme_SmsIrq.c
*
* @version  1.0.0
*
* @brief    This source file includes the IRME internal API implementation of the IRME 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 "StdRegMacros.h"
#include "Irme_SmsReg.h"
#include "CDD_Irme_Cfg.h"
#include "Irme_Sms.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup IRME_Driver
  * @brief IRME driver modules
  @{
*/

/** @defgroup IRME_Macros
  @{
*/
/**********************************************************************************************************************
                                    VENDOR ID & AUTOSAR VERSION INFO
***********************************************************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define IRME_SMS_IRQ_VENDOR_ID                                 0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define IRME_SMS_IRQ_AR_RELEASE_MAJOR_VERSION                  0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define IRME_SMS_IRQ_AR_RELEASE_MINOR_VERSION                  0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define IRME_SMS_IRQ_AR_RELEASE_REVISION_VERSION               0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */  
#define IRME_SMS_IRQ_SW_MAJOR_VERSION                          0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define IRME_SMS_IRQ_SW_MINOR_VERSION                          0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define IRME_SMS_IRQ_SW_PATCH_VERSION                          0x00U

/**@} end of group IRME_Macros */

/**********************************************************************************************************************           
                                      VERSION INFOMATION VERIFICATION
***********************************************************************************************************************/
/* Make sure the vendor ids are the same between Irme_SmsIrq.c and Irme_Sms.h */
#if (IRME_SMS_IRQ_VENDOR_ID != IRME_SMS_H_VENDOR_ID)
    #error "Vendor ids mismatch between Irme_SmsIrq.c and Irme_Sms.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Irme_SmsIrq.c and Irme_Sms.h */
#if (IRME_SMS_IRQ_AR_RELEASE_MAJOR_VERSION    != IRME_SMS_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Irme_SmsIrq.c and Irme_Sms.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Irme_SmsIrq.c and Irme_Sms.h */
#if (IRME_SMS_IRQ_AR_RELEASE_MINOR_VERSION    != IRME_SMS_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Irme_SmsIrq.c and Irme_Sms.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Irme_SmsIrq.c and Irme_Sms.h */
#if (IRME_SMS_IRQ_AR_RELEASE_REVISION_VERSION != IRME_SMS_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Irme_SmsIrq.c and Irme_Sms.h"
#endif
/* Make sure the software major version number is the same between Irme_SmsIrq.c and Irme_Sms.h */
#if (IRME_SMS_IRQ_SW_MAJOR_VERSION != IRME_SMS_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Irme_SmsIrq.c and Irme_Sms.h"
#endif
/* Make sure the software minor version number is the same between Irme_SmsIrq.c and Irme_Sms.h */
#if (IRME_SMS_IRQ_SW_MINOR_VERSION != IRME_SMS_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Irme_SmsIrq.c and Irme_Sms.h"
#endif
/* Make sure the software patch version number is the same between Irme_SmsIrq.c and Irme_Sms.h */
#if (IRME_SMS_IRQ_SW_PATCH_VERSION != IRME_SMS_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Irme_SmsIrq.c and Irme_Sms.h"
#endif

/* Make sure the vendor ids are the same between Irme_SmsIrq.c and CDD_Irme_Cfg.h */
#if (IRME_SMS_IRQ_VENDOR_ID != CDD_IRME_CFG_H_VENDOR_ID)
    #error "Vendor ids mismatch between Irme_SmsIrq.c and CDD_Irme_Cfg.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Irme_SmsIrq.c and CDD_Irme_Cfg.h */
#if (IRME_SMS_IRQ_AR_RELEASE_MAJOR_VERSION != CDD_IRME_CFG_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Irme_SmsIrq.c and CDD_Irme_Cfg.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Irme_SmsIrq.c and CDD_Irme_Cfg.h */
#if (IRME_SMS_IRQ_AR_RELEASE_MINOR_VERSION != CDD_IRME_CFG_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Irme_SmsIrq.c and CDD_Irme_Cfg.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Irme_SmsIrq.c and CDD_Irme_Cfg.h */
#if (IRME_SMS_IRQ_AR_RELEASE_REVISION_VERSION != CDD_IRME_CFG_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Irme_SmsIrq.c and CDD_Irme_Cfg.h"
#endif
/* Make sure the software major version number is the same between Irme_SmsIrq.c and CDD_Irme_Cfg.h */
#if (IRME_SMS_IRQ_SW_MAJOR_VERSION != CDD_IRME_CFG_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Irme_SmsIrq.c and CDD_Irme_Cfg.h"
#endif
/* Make sure the software minor version number is the same between Irme_SmsIrq.c and CDD_Irme_Cfg.h*/
#if (IRME_SMS_IRQ_SW_MINOR_VERSION != CDD_IRME_CFG_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Irme_SmsIrq.c and CDD_Irme_Cfg.h"
#endif
/* Make sure the software patch version number is the same between Irme_SmsIrq.c and CDD_Irme_Cfg.h */
#if (IRME_SMS_IRQ_SW_PATCH_VERSION != CDD_IRME_CFG_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Irme_SmsIrq.c and CDD_Irme_Cfg.h"
#endif

/* Make sure the vendor ids are the same between Irme_SmsIrq.c and Irme_SmsReg.h */
#if (IRME_SMS_IRQ_VENDOR_ID != IRME_SMS_REG_H_VENDOR_ID)
    #error "Vendor ids mismatch between Irme_SmsIrq.c and Irme_SmsReg.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Irme_SmsIrq.c and Irme_SmsReg.h */
#if (IRME_SMS_IRQ_AR_RELEASE_MAJOR_VERSION != IRME_SMS_REG_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Irme_SmsIrq.c and Irme_SmsReg.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Irme_SmsIrq.c and Irme_SmsReg.h */
#if (IRME_SMS_IRQ_AR_RELEASE_MINOR_VERSION != IRME_SMS_REG_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Irme_SmsIrq.c and Irme_SmsReg.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Irme_SmsIrq.c and Irme_SmsReg.h */
#if (IRME_SMS_IRQ_AR_RELEASE_REVISION_VERSION != IRME_SMS_REG_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Irme_SmsIrq.c and Irme_SmsReg.h"
#endif
/* Make sure the software major version number is the same between Irme_SmsIrq.c and Irme_SmsReg.h */
#if (IRME_SMS_IRQ_SW_MAJOR_VERSION != IRME_SMS_REG_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Irme_SmsIrq.c and Irme_SmsReg.h"
#endif
/* Make sure the software minor version number is the same between Irme_SmsIrq.c and Irme_SmsReg.h */
#if (IRME_SMS_IRQ_SW_MINOR_VERSION != IRME_SMS_REG_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Irme_SmsIrq.c and Irme_SmsReg.h"
#endif
/* Make sure the software patch version number is the same between Irme_SmsIrq.c and Irme_SmsReg.h */
#if (IRME_SMS_IRQ_SW_PATCH_VERSION != IRME_SMS_REG_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Irme_SmsIrq.c and Irme_SmsReg.h"
#endif

#ifdef ENABLE_INTERMODULE_MCAL_CHECK
    /* Make sure the AUTOSAR major version number is the same between Irme_SmsIrq.c and StdRegMacros.h */
    #if (IRME_SMS_IRQ_AR_RELEASE_MAJOR_VERSION != STD_REG_MACROS_H_AR_RELEASE_MAJOR_VERSION)
        #error "AUTOSAR Major Version Number mismatch between Irme_SmsIrq.c and StdRegMacros.h"
    #endif
    /* Make sure the AUTOSAR minor version number is the same between Irme_SmsIrq.c and StdRegMacros.h */
    #if (IRME_SMS_IRQ_AR_RELEASE_MINOR_VERSION != STD_REG_MACROS_H_AR_RELEASE_MINOR_VERSION)
        #error "AUTOSAR Minor Version Number mismatch between Irme_SmsIrq.c and StdRegMacros.h"
    #endif
#endif  

/**********************************************************************************************************************
                                                FUNCTION DECLARATION
***********************************************************************************************************************/
#define IRME_START_SEC_CODE
#include "Irme_MemMap.h"

/** @defgroup IRME_Fuctions
  @{
*/

void SERM_IRQHandler(void);

#if (STD_ON == IRME_BIT_FAULT_ISR_ENABLE)
static ISR(Irme_SmsIrq_BitsFaultIsr);
#endif

/**********************************************************************************************************************
                                                    GLOBAL FUNCTION
***********************************************************************************************************************/
/*!
* @brief          Check the STS0 and CFG0 registers and call the corresponding notification callback
*
* @param[in]      RegVal       The Collected fault status structure of Irme_FaultPoolType.
* @param[in]      CircleCnt    0:RD_ERR_SIN error.
*                              1:RD_ERR_DBL error.
*                              2:WR_ERR_SIN error.
*                              3:WR_ERR_DBL error.
* @param[out]     None
* @param[in,out]  None
*/
#if (STD_ON == IRME_BIT_FAULT_ISR_ENABLE)
static void Irme_SmsIrq_CheckRegBit(VAR(uint32, AUTOMATIC) RegVal, VAR(uint8, AUTOMATIC) CircleCnt)
{
    VAR(uint32, AUTOMATIC) errorAddr;

    if (0U != (RegVal & ((uint32)1U << g_irmeSmsStatBitPosition[CircleCnt])))
    {
        if(NULL_PTR != g_smsConfigPtr->errNotificationFunction)
        {
            errorAddr = REG_READ32(SMS_ECC_LOGn_REG_ADDR);
            errorAddr = (((errorAddr >> SMS_ECC_LOGn_ADDR_SHIFT) * 4U ) + SMS_RAM_START_ADDRESS);
            g_smsConfigPtr->errNotificationFunction((Irme_FaultIdType)CircleCnt, errorAddr);
        } 
        else
        {
            /* do nothing */
        }
    }
    else
    {
        /* do nothing */
    }
}
#endif

/*!
* @brief          The ISR of non-correctable double bit error 
*
* @param[in]      None
* @param[out]     None
* @param[in,out]  None
*/
static ISR(Irme_SmsIrq_BitsFaultIsr)
{
#if (STD_ON == IRME_BIT_FAULT_ISR_ENABLE)
    VAR(uint32, AUTOMATIC) regVal;
    VAR(uint8, AUTOMATIC) circleCnt = 0U;
    
    /* Clear the state flgs */
    REG_READ32(SMS_STAT_REG_ADDR);

    regVal = REG_READ32(SMS_INTRSn_REG_ADDR);

    if ((uint32)0U != regVal)
    {
        if(NULL_PTR != g_smsConfigPtr)
        {
            while(circleCnt < IRME_FAULTS_TOTAL_COUNT)
            {
                Irme_SmsIrq_CheckRegBit(regVal, circleCnt); 
                circleCnt += 1U;
            }
        }
        else
        {
            /* do nothing */
        }

        /* while the IRME driver is not initialized, only clear the error flag , and the error will not notify the user */
        REG_WRITE32(SMS_INTRRn_REG_ADDR, 0U);
    }
    else
    {
        /* do nothing */
    }
#endif

    EXIT_INTERRUPT();
}

/*!
* @brief          This function handles SMS exception
*
* @param[in]      None
* @param[out]     None
* @param[in,out]  None
*/
void SERM_IRQHandler(void)
{
    Irme_SmsIrq_BitsFaultIsr();
}

/**@} end of group IRME_Fuctions */

#define IRME_STOP_SEC_CODE
#include "Irme_MemMap.h"

#ifdef __cplusplus
}
#endif

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