/*!
* @file     Mcu.c
*
* @version  1.0.0
*
* @brief    The underlying driver code of the MCU 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.
*/

/*
* MISRA-C:2012 compliance checks
*
*      Mcu_c_MISRA_REF1
*          Breaks the required Rule-11.3 of MISRA 2012 guidelines,
*          A conversion shall not be performed between a pointer to object type and a pointer to a different object type
*
*/

#ifdef __cplusplus
extern "C"{
#endif

#include "Mcu_RcmTypes.h"
#include "Compiler_Cfg.h"
#include "Compiler.h"
#include "Mcu_Cfg.h"
#include "Mcu_PmuTypes.h"
#include "Std_Types.h"
#include "Mcu.h"

#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
#include "Det.h"
#endif

/** @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_VENDOR_ID                              0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_AR_RELEASE_MAJOR_VERSION               0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_AR_RELEASE_MINOR_VERSION               0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_AR_RELEASE_REVISION_VERSION            0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */ 
#define MCU_SW_MAJOR_VERSION                       0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_SW_MINOR_VERSION                       0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_SW_PATCH_VERSION                       0x00U

/**@} end of group MCU_Macros */

/*******************************************************************************
                              GLOBAL VARIABLES
*******************************************************************************/
#define MCU_START_SEC_CONST_UNSPECIFIED
#include "Mcu_MemMap.h"

/** @defgroup MCU_Variable
  @{
*/

static const Mcu_Rcm_AHBPeriphType g_rcmAHBPeriph[12] = {RCM_AHB_PERIPH_DMA1, RCM_AHB_PERIPH_SRAM, 
                                                 RCM_AHB_PERIPH_FMC,RCM_AHB_PERIPH_CRC, 
                                                 RCM_AHB_PERIPH_RNG, RCM_AHB_PERIPH_SHA,
                                                 RCM_AHB_PERIPH_AES, RCM_AHB_PERIPH_GPIOA, 
                                                 RCM_AHB_PERIPH_GPIOB,RCM_AHB_PERIPH_GPIOC, 
                                                 RCM_AHB_PERIPH_GPIOD, RCM_AHB_PERIPH_GPIOF};

static const Mcu_Rcm_APB2PeriphType g_rcmAPB2Periph[8] = {RCM_APB2_PERIPH_SYSCFG, RCM_APB2_PERIPH_ADC1, 
                                                   RCM_APB2_PERIPH_TMR1,RCM_APB2_PERIPH_SPI1, 
                                                   RCM_APB2_PERIPH_USART1, RCM_APB2_PERIPH_TMR8,
                                                   RCM_APB2_PERIPH_DBGMCU, RCM_APB2_PERIPH_SERM};

static const Mcu_Rcm_APB1PeriphType g_rcmAPB1Periph[8] = {RCM_APB1_PERIPH_TMR2, RCM_APB1_PERIPH_TMR3, 
                                                   RCM_APB1_PERIPH_TMR4, RCM_APB1_PERIPH_TMR6, 
                                                   RCM_APB1_PERIPH_TMR7, RCM_APB1_PERIPH_USART2,
                                                   RCM_APB1_PERIPH_FDCAN, RCM_APB1_PERIPH_PMU};

static const Mcu_RawResetType g_rcmResetValue[8] = {MCU_RCM_RESET_LOC, MCU_RCM_RESET_OBR, 
                                             MCU_RCM_RESET_IWDT, MCU_RCM_RESET_LPWR, 
                                             MCU_RCM_RESET_SW, MCU_RCM_RESET_PIN,
                                             MCU_RCM_RESET_POD, MCU_RCM_RESET_PWR};

static const VAR(uint8, MCU_VAR) Mcu_ramCfgIds[MCU_MAXRAMCONFIGS_COUNT] = {0};
                                             
/**@} end of group MCU_Variable */

#define MCU_STOP_SEC_CONST_UNSPECIFIED
#include "Mcu_MemMap.h"

#define MCU_START_SEC_VAR_INIT_UNSPECIFIED
#include "Mcu_MemMap.h"

/** @defgroup MCU_Variable
  @{
*/

/* The local replica points to the configuration data */
static P2CONST(Mcu_ConfigType, MCU_VAR, MCU_APPL_CONST) g_McuPointCfg = NULL_PTR;

static VAR(Mcu_StatusType, MCU_VAR) g_McuDriverStatus = MCU_DRIVER_UNINIT;

static uint8 g_rcmResetNum = sizeof(g_rcmResetValue) / sizeof(g_rcmResetValue[0]);

/**@} end of group MCU_Variable */

#define MCU_STOP_SEC_VAR_INIT_UNSPECIFIED
#include "Mcu_MemMap.h"

/*******************************************************************************
                               LOCAL FUNCTIONS
*******************************************************************************/
#define MCU_START_SEC_CODE
#include "Mcu_MemMap.h"

/** @defgroup MCU_Fuctions
  @{
*/

#if (MCU_INITCLK_ENABLED == STD_ON)
static FUNC(void, MCU_CODE) Mcu_PeriphClockConfig(Mcu_Rcm_ModuleNumType module, Mcu_Rcm_ClockEnType enable)
{
    if (CLK_ENABLE == enable)
    {
        if (module <= GPIOF_MODULE)
        {
            Mcu_Rcm_OperationPeriphClock(ENABLE,RCM_AHB_PERIPH,(uint32)g_rcmAHBPeriph[(uint8)module]);
        }
        else
        {
            if (module <= SERM_MODULE)
            {
                Mcu_Rcm_OperationPeriphClock(ENABLE,RCM_APB2_PERIPH,(uint32)g_rcmAPB2Periph[(uint8)module - (uint8)0x0C]);
            }
            else
            {
                Mcu_Rcm_OperationPeriphClock(ENABLE,RCM_APB1_PERIPH,(uint32)g_rcmAPB1Periph[(uint8)module - (uint8)0x14]);
            }
            
        }
    }
    else if (CLK_DISABLE == enable)
    {
        if (module <= GPIOF_MODULE)
        {
            Mcu_Rcm_OperationPeriphClock(DISABLE,RCM_AHB_PERIPH,(uint32)g_rcmAHBPeriph[(uint8)module]);
        }
        else
        {
            if (module <= SERM_MODULE)
            {
                Mcu_Rcm_OperationPeriphClock(DISABLE,RCM_APB2_PERIPH,(uint32)g_rcmAPB2Periph[(uint8)module - (uint8)0x0C]);
            }
            else
            {
                Mcu_Rcm_OperationPeriphClock(DISABLE,RCM_APB1_PERIPH,(uint32)g_rcmAPB1Periph[(uint8)module - (uint8)0x14]);
            }
        }
    }
    else
    {
        /* nothing */
    }
}
#endif

/*******************************************************************************
                              GLOBAL FUNCTION
*******************************************************************************/
/*!
* @brief         Used to initialize the MCU driver
*
* @param[in]     ConfigPtr:    Pointer to configuration structure.
*
* @retval        void
*
* @note          None
*/
FUNC(void, MCU_CODE) Mcu_Init(const Mcu_ConfigType * ConfigPtr)
{
    if ((Mcu_StatusType)MCU_DRIVER_UNINIT != g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INIT_SID, MCU_E_ALL_READY_INITIALIZED);
#endif
    }
    else if (NULL_PTR == ConfigPtr)	
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INIT_SID, MCU_E_INIT_FAILED);
#endif
    }
    else
    {
        g_McuPointCfg = ConfigPtr;
        
        if (STATUS_SUCCESS == Mcu_Rcm_Init(g_McuPointCfg->mcuRcmCfg))
        {
            g_McuDriverStatus = MCU_DRIVER_IDLE;
        }
        else
        {
            /* nothing */
        }
        
        Mcu_Sysc_Init(g_McuPointCfg->mcuSyscCfg);

    }
}


/*!
* @brief         Used to set the MCU mode.
*
* @param[in]     McuMode: the MCU mode setting ID in the configuration structure to be set
*
* @retval        void
*
* @note          None
*/
FUNC(void, MCU_CODE) Mcu_SetMode(const Mcu_ModeType McuMode)
{
    Mcu_Rcm_SyscSelType tempClk = RCM_SYSCLK_SEL_HSI;
    
    Mcu_Rcm_ClockEnType hsiVal = CLK_DISABLE;
    Mcu_Rcm_ClockEnType hseVal = CLK_DISABLE;
    Mcu_Rcm_ClockEnType pllVal = CLK_DISABLE;
    
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_SETMODE_SID, MCU_E_UNINIT);
#endif
    }
    else if (McuMode >= (Mcu_ModeType)4)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_SETMODE_SID, MCU_E_PARAM_MODE);
#endif
    }
    else
    {
        hsiVal = g_mcuConfig.mcuRcmCfg->hsiClockCfg.enableHsiClock;
        hseVal = g_mcuConfig.mcuRcmCfg->hseClockCfg.enableHseClock;
        pllVal = g_mcuConfig.mcuRcmCfg->pllClockCfg.enablePllClock;
        
        switch (McuMode)
        {
            case (Mcu_ModeType)MCU_SLEEP_MODE :
            {
                tempClk = Mcu_Rcm_ReadSYSCLKSource();
                if (RCM_SYSCLK_SEL_HSI == tempClk)
                {
                    Mcu_Rcm_ConfigHSE(RCM_HSE_CLOSE);
                    Mcu_Rcm_DisablePLL();
                }
                else if (RCM_SYSCLK_SEL_HSE == tempClk)
                {
                    Mcu_Rcm_DisableHSI();
                    Mcu_Rcm_DisablePLL();
                }
                else if (RCM_SYSCLK_SEL_PLL == tempClk)
                {
                    Mcu_Rcm_DisableHSI();
                    Mcu_Rcm_ConfigHSE(RCM_HSE_CLOSE);
                }
                else
                {
                    /* nothing */
                }
                Mcu_Pmu_EnterSleepMode(PMU_SLEEPENTRY_WFI);
                
                if ((uint8)0 == (uint8)hsiVal) 
                {
                    Mcu_Rcm_EnableHSI();
                }
                else
                {
                    /* nothing */
                }

                if ((uint8)0 == (uint8)hseVal) 
                {
                    Mcu_Rcm_ConfigHSE(RCM_HSE_OPEN);
                }
                else
                {
                    /* nothing */
                }

                if ((uint8)0 == (uint8)pllVal) 
                {
                    Mcu_Rcm_EnablePLL();
                }
                else
                {
                    /* nothing */
                }
            }
            break;
            
            case (Mcu_ModeType)MCU_STOP_MODE :
            {
                Mcu_Pmu_EnterStopMode(PMU_REGULATOR_ON,PMU_STOPENTRY_WFI);
            }
            break;
            
            case (Mcu_ModeType)MCU_STABDBY_MODE :
            {
                Mcu_Pmu_EnterStandbyMode();
            }
            break;
            
            default:
            {
                /* nothing */
            }
            break;
        };
        g_McuDriverStatus = MCU_DRIVER_IDLE;
    }
}


#if (MCU_INITCLK_ENABLED == STD_ON)
/*!
* @brief         Used to initialize the MCU driver clock
*
* @param[in]     ClockSetting: The clock setting ID in the configuration stucture to use
*
* @retval        E_OK:      The driver state allows the function to be executed,and the supplid
*                           arguments are in range
*                E_NOT_OK:  Driver status does not allow execution or parameters are invalid
*
* @note          None
*/
FUNC(Std_ReturnType, MCU_CODE) Mcu_InitClock(Mcu_ClockType ClockSetting)
{
    Std_ReturnType returnCode = (Std_ReturnType)E_NOT_OK;
    uint8 tempFlag = 0;

    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITCLOCK_SID, MCU_E_UNINIT);
#endif
    }
    else if (NULL_PTR == g_McuPointCfg)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITCLOCK_SID, MCU_E_PARAM_CLOCK);
#endif
    }
    else
    {
        Mcu_Rcm_ConfigAHB(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->AHBDiv);
        Mcu_Rcm_ConfigAPB(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->APBDiv);
        
        (void)Mcu_Rcm_OperationHSI14(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->hsi14ClkCfg);
        (void)Mcu_Rcm_OperationHSI14ADC(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->enableHsi14ADC);
        (void)Mcu_Rcm_OperationLSI(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->lsiEnable);
        
        for(tempFlag = 0; tempFlag < (uint8)MCU_RCM_REGISTERS_CONUT; tempFlag++)
        {
            Mcu_PeriphClockConfig(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->moduleConfig[tempFlag].module,
                                  g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->moduleConfig[tempFlag].enable);
        }
        
        Mcu_Rcm_ConfigUsartOrCanClk(RCM_USART1_SEL,g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->usart1ClkSele);
        Mcu_Rcm_ConfigUsartOrCanClk(RCM_USART2_SEL,g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->usart2ClkSele);
        Mcu_Rcm_ConfigUsartOrCanClk(RCM_CANFD_SEL,g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->canFdClkSele);
        
        Mcu_Rcm_OperationCCS(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->CCSEnable);
        
        Mcu_Pmu_Init(g_McuPointCfg->mcuPmuCfg);
        Mcu_DbgMcu_Init(g_McuPointCfg->mcuDbgMcuCfg);
        
        Mcu_Rcm_ConfigRTC(g_McuPointCfg->Mcu_ClockCfg[ClockSetting]->rtcCfg);
        
        
        g_McuDriverStatus = MCU_DRIVER_IDLE;
        
        returnCode = (Std_ReturnType)E_OK;
    }
    
    
    return returnCode;
    
}
#endif



/*!
* @brief         This function is used to return the ram reset value
*
* @param[in]     void
*
* @retval        Mcu_RawResetType   Code of the Raw reset value.
*
* @note          None
*/
FUNC(Mcu_RawResetType, MCU_CODE) Mcu_GetResetRawValue(void)
{
    Mcu_RawResetType RegValue = 0;
    uint8 tempVal = 0;
    
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_GETRESETRAWVALUE_SID, MCU_E_UNINIT);
#endif
    }
    else
    {
        RegValue = Mcu_Rcm_ReadResetFlag();
        
        for (tempVal = 0; tempVal < (uint8)g_rcmResetNum; tempVal++)
        {
            if ((uint32)0 != (RegValue & (uint32)g_rcmResetValue[g_rcmResetNum]))
            {
                RegValue = g_rcmResetValue[g_rcmResetNum];
                break;
            }
            else
            {
                /* nothing */
            }
        }
        g_McuDriverStatus = MCU_DRIVER_IDLE;
        Mcu_Rcm_ClearResetFlag();
    }
    
    return (Mcu_RawResetType)RegValue;
}


/*!
* @brief         This feature is used to return the locked state of the PLL
*
* @param[in]     void
*
* @retval        MCU_PLL_STATUS_UNDEFINED:  The PLL Status is unknown.
*                MCU_PLL_LOCKED:  The PLL is locked.
*                MCU_PLL_UNLOCKED:  The PLL is unlocked.
*
* @note          None
*/
FUNC(Mcu_PllStatusType, MCU_CODE) Mcu_GetPllStatus(void)
{
    Mcu_PllStatusType pllState = MCU_PLL_STATUS_UNDEFINED;
    
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_GETPLLSTATUS_SID, MCU_E_UNINIT);
#endif
    }
    else
    {
        pllState = Mcu_Rcm_GetPllState();
        
        g_McuDriverStatus = MCU_DRIVER_IDLE;
    }
    
    return (Mcu_PllStatusType)pllState;
}




#if (MCU_PLL_NONE == STD_ON)
/*!
* @brief         This feature activates PLL clock to MCU clock
*
* @param[in]     void
*
* @retval        E_OK: the order has been accepted
*                E_NOT_OK:  the order has not been accepted
*
* @note          None
*/
FUNC(Std_ReturnType, MCU_CODE) Mcu_DistributePllClock(void)
{
    VAR(Std_ReturnType, AUTOMATIC) PllState = (Std_ReturnType)E_NOT_OK;
    
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_DISTRIBUTEPLLCLOCK_SID, MCU_E_UNINIT);
#endif
    }
    else if (MCU_PLL_LOCKED == Mcu_Rcm_GetPllState())
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_DISTRIBUTEPLLCLOCK_SID, MCU_E_PLL_NOT_LOCKED);
#endif
    }
    else
    {
        Mcu_Rcm_ConfigSYSCLK(RCM_SYSCLK_SEL_PLL);
        PllState = (Std_ReturnType)E_OK;
        
        g_McuDriverStatus = MCU_DRIVER_IDLE;
    }
    
    return (Std_ReturnType)PllState;
}
#endif




#if (MCU_PERRST_API == STD_ON)
/*!
* @brief         This function will perform the MCU reset
*
* @param[in]     void
*
* @retval        void
*
* @note          None
*/
FUNC(void, MCU_CODE) Mcu_PerformReset(void)
{
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_PERFORMRESET_SID, MCU_E_UNINIT);
#endif
    }
    else
    {
        Mcu_Rcm_CM4SysReset();
        
        g_McuDriverStatus = MCU_DRIVER_IDLE;
    }
}
#endif



/*!
* @brief         Used to initialize the MCU driver Ram sections
*
* @param[in]     RamSec:    The index of the ram portion of the configuration structure that
*                           needs to be initialized
*
* @retval        E_OK:      parameter Valid, the driver status allowed execution,RAM check successful
*                E_NOT_OK:  parameter Invalid, the driver state did not allowed execution, RAM check was not successful
*
* @note          None
*/
FUNC(Std_ReturnType, MCU_CODE) Mcu_InitRamSection(VAR(Mcu_RamSectionType, AUTOMATIC) RamSection)
{
    VAR(Std_ReturnType, AUTOMATIC) RamState = (Std_ReturnType)E_NOT_OK;
    VAR(Mcu_RamIdType, AUTOMATIC) ramCount = 0;
    VAR(Mcu_RamSecSizeType, AUTOMATIC) ramCountMax = 0;
    P2CONST(Mcu_RamConfigType, AUTOMATIC, MCU_APPL_CONST) ramCfgPtr = NULL_PTR;
    
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITRAMSECTION_SID, MCU_E_UNINIT);
#endif
    }
    else if (NULL_PTR == g_McuPointCfg->Mcu_RamCfg)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITRAMSECTION_SID, MCU_E_PARAM_RAMSECTION);
#endif
    }
    else
    {
        RamState = (Std_ReturnType)E_OK;

        ramCfgPtr = &(*g_McuPointCfg->Mcu_RamCfg)[Mcu_ramCfgIds[RamSection]];
        ramCountMax = (Mcu_RamSecSizeType)((ramCfgPtr->mcuRamSectionSize) / (ramCfgPtr->mcuRamSectionWriteSize));

        for (ramCount = (Mcu_RamIdType)0UL; ramCount < ramCountMax; ramCount++)
        {
            if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)1U)
            {
                (*((uint8 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount] 
                    = (uint8)ramCfgPtr->mcuRamDefaultValue;
            }
            else if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)2U)
            {
                /*** @warning Mcu_c_MISRA_REF1 Pointer cast to stricter alignment. */
                /*** @warning Mcu_c_MISRA_REF1 Casting to different object pointer type. */
                (*((uint16 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount] 
                    = (uint16)ramCfgPtr->mcuRamDefaultValue;
            }
            else if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)4U)
            {
                /*** @warning Mcu_c_MISRA_REF1 Pointer cast to stricter alignment. */
                /*** @warning Mcu_c_MISRA_REF1 Casting to different object pointer type. */
                (*((uint32 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount] 
                    = (uint32)ramCfgPtr->mcuRamDefaultValue;
            }
            else if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)8U)
            {
                /*** @warning Mcu_c_MISRA_REF1 Pointer cast to stricter alignment. */
                /*** @warning Mcu_c_MISRA_REF1 Casting to different object pointer type. */
                (*((uint64 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount] 
                    = (uint64)ramCfgPtr->mcuRamDefaultValue;
            }
            else
            {
                RamState = (Std_ReturnType)E_NOT_OK;
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
                (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                                MCU_INITRAMSECTION_SID, MCU_E_PARAM_RAMSECTION);
#endif
                break;
            }
        }
        if ((Std_ReturnType)E_OK == RamState)
        {
            for (ramCount = (Mcu_RamIdType)0UL; ((ramCount < ramCountMax) && ((Std_ReturnType)E_OK == RamState)); ramCount++)
            {
                if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)1U)
                {
                    if ((uint8)ramCfgPtr->mcuRamDefaultValue 
                        != (*((uint8 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount])
                    {
                        RamState = (Std_ReturnType)E_NOT_OK;
                    }
                    else
                    {
                        /* nothing */
                    }
                }
                else if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)2U)
                {
                    /*** @warning Mcu_c_MISRA_REF1 Pointer cast to stricter alignment. */
                    /*** @warning Mcu_c_MISRA_REF1 Casting to different object pointer type. */
                    if ((uint16)ramCfgPtr->mcuRamDefaultValue 
                        != (*((uint16 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount])
                    {
                        RamState = (Std_ReturnType)E_NOT_OK;
                    }
                    else
                    {
                        /* nothing */
                    }
                }
                else if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)4U)
                {
                    /*** @warning Mcu_c_MISRA_REF1 Pointer cast to stricter alignment. */
                    /*** @warning Mcu_c_MISRA_REF1 Casting to different object pointer type. */
                    if ((uint32)ramCfgPtr->mcuRamDefaultValue 
                        != (*((uint32 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount])
                    {
                        RamState = (Std_ReturnType)E_NOT_OK;
                    }
                    else
                    {
                        /* nothing */
                    }
                }
                else if (ramCfgPtr->mcuRamSectionWriteSize == (Mcu_RamWrtSecSizeType)8U)
                {
                    /*** @warning Mcu_c_MISRA_REF1 Pointer cast to stricter alignment. */
                    /*** @warning Mcu_c_MISRA_REF1 Casting to different object pointer type. */
                    if ((uint64)ramCfgPtr->mcuRamDefaultValue 
                        != (*((uint64 (*)[1U]) (ramCfgPtr->mcuRamSectionBaseAddress)))[ramCount])
                    {
                        RamState = (Std_ReturnType)E_NOT_OK;
                    }
                    else
                    {
                        /* nothing */
                    }
                }
                else
                {
                    RamState = (Std_ReturnType)E_NOT_OK;
                }
            }
        }
        else
        {
            /* nothing */
        }
        if ((Std_ReturnType)E_OK == RamState)
        {
            g_McuDriverStatus = MCU_DRIVER_IDLE;
        }
        else
        {
            /* nothing */
        }
    }

    return (Std_ReturnType)RamState;
}


#if (MCU_GET_RAM_STATE_API == STD_ON)
FUNC( Mcu_RamStateType, MCU_CODE) Mcu_GetRamState(VAR( void, AUTOMATIC))
{
    VAR( Mcu_RamStateType, AUTOMATIC) ramState = MCU_RAMSTATE_INVALID;
    
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_GETRAMSTATE_SID, MCU_E_UNINIT);
#endif
    }
    else
    {
        /* nothing */
    }
    
    return ramState;
}
#endif

#if (MCU_VERSINFO_API == STD_ON)
/*!
* @brief         This function is used to return the MCU module version information
*
* @param[in]     VersionInfo:  A pointer to the variable where version information is stored.
*
* @retval        void
*
* @note          None
*/
FUNC(void, MCU_CODE) Mcu_GetVersionInfo(P2VAR(Std_VersionInfoType, AUTOMATIC, MCU_APPL_DATA) VersionInfo)
{
    if(NULL_PTR != VersionInfo)
    {
        (VersionInfo)->vendorID = (uint16)MCU_CFG_H_VENDOR_ID;
        (VersionInfo)->moduleID = (uint8)MCU_CFG_H_MODULE_ID;
        (VersionInfo)->sw_major_version = (uint8)MCU_CFG_H_SW_MAJOR_VERSION;
        (VersionInfo)->sw_minor_version = (uint8)MCU_CFG_H_SW_MINOR_VERSION;
        (VersionInfo)->sw_patch_version = (uint8)MCU_CFG_H_SW_PATCH_VERSION;
    }
    else
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_GETVERSIONINFO_SID, MCU_E_PARAM_POINTER);
#endif
    }
}
#endif


#if (MCU_CMU_MODE_ENABLE == STD_ON)
FUNC(void, MCU_CODE) Mcu_ClockMeasurement(uint32 cmuCfgCount)
{
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITRAMSECTION_SID, MCU_E_UNINIT);
#endif
    }
    else if (NULL_PTR == g_McuPointCfg->McuCmuCfg)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITRAMSECTION_SID, MCU_E_PARAM_RAMSECTION);
#endif
    }
    else
    {
        Mcu_Cmu_Enable(DISABLE);
        Mcu_Cmu_ConfigReferenceClock(g_McuPointCfg->McuCmuCfg[cmuCfgCount]->cmuRefPre);
        Mcu_Cmu_MeasureClockSelect(g_McuPointCfg->McuCmuCfg[cmuCfgCount]->selectClock);
        Mcu_Cmu_LOCResetEnable(g_McuPointCfg->McuCmuCfg[cmuCfgCount]->LOCResetEnable);
        Mcu_Cmu_ReferenceClockCounts(g_McuPointCfg->McuCmuCfg[cmuCfgCount]->clockCount);
        Mcu_Cmu_InterruptEnable(CMU_LOCIE_INTER,g_McuPointCfg->McuCmuCfg[cmuCfgCount]->locieEna);
        Mcu_Cmu_Enable(ENABLE);
    }
}



FUNC(uint16, MCU_CODE) Mcu_GetMeasValue(uint32 cmuCfgCount)
{
    uint16 tempVal1 = 0u;
    uint16 tempVal2 = 0u;
    uint16 returnVal = 0u;
    if (MCU_DRIVER_UNINIT == g_McuDriverStatus)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITRAMSECTION_SID, MCU_E_UNINIT);
#endif
    }
    else if (NULL_PTR == g_McuPointCfg->McuCmuCfg)
    {
#if (MCU_DEV_ERR_DETECT_ENABLE == STD_ON)
        (void)Det_ReportError((uint16)MCU_CFG_H_MODULE_ID, MCU_INSTANCE_ID,
                        MCU_INITRAMSECTION_SID, MCU_E_PARAM_RAMSECTION);
#endif
    }
    else
    {
        tempVal1 = Mcu_Cmu_GetMeasValue();
        if ((tempVal1 > g_McuPointCfg->McuCmuCfg[cmuCfgCount]->fhVal) || 
            (tempVal1 < g_McuPointCfg->McuCmuCfg[cmuCfgCount]->flVal))
        {
            returnVal = 0u;
        }
        else
        {
            /* nothing */
        }
        
        tempVal2 = Mcu_Cmu_GetMeasValue();
        if ((tempVal2 > g_McuPointCfg->McuCmuCfg[cmuCfgCount]->fhVal) || 
            (tempVal2 < g_McuPointCfg->McuCmuCfg[cmuCfgCount]->flVal))
        {
            returnVal = 0u;
        }
        else
        {
            /* nothing */
        }
        
        if (tempVal1 == tempVal2)
        {
            returnVal = tempVal2;
        }
        else
        {
            returnVal = 0u;
        }
    }
    return returnVal;
}


#endif

/**@} end of group MCU_Fuctions */

#define MCU_STOP_SEC_CODE
#include "Mcu_MemMap.h"


#ifdef __cplusplus
}
#endif //__cplusplus

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