/*!
* @file     Mcu_DbgMcu.c
*
* @version  1.0.0
*
* @brief    The underlying driver code of the debug MCU(DBGMCU) 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_DbgMcuReg.h"
#include "Mcu_DbgMcuTypes.h"
#include "Mcu_RcmReg.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_DBGMCU_VENDOR_ID                              0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_DBGMCU_AR_RELEASE_MAJOR_VERSION               0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_DBGMCU_AR_RELEASE_MINOR_VERSION               0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_DBGMCU_AR_RELEASE_REVISION_VERSION            0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */ 
#define MCU_DBGMCU_SW_MAJOR_VERSION                       0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_DBGMCU_SW_MINOR_VERSION                       0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_DBGMCU_SW_PATCH_VERSION                       0x00U

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

/** @defgroup MCU_Fuctions
  @{
*/

/*!
* @brief       Configures debug behavior during STOP mode
*
* @param       enable          Control whether debug remains active in STOP mode
*
* @retval      None
*
* @note        Determines if debug monitoring continues when MCU enters STOP mode
* @note        ENABLE = Debugger can wake MCU from STOP mode
* @note        DISABLE = Debugger disconnected during STOP mode
* @note        Critical for low-power debugging scenarios
*/
FUNC(void, MCU_CODE) Mcu_DbgMcu_ConfigStopMode(boolean enable)
{
    if (enable == (boolean)ENABLE)
    {
        REG_BIT_SET32(DBGMCU_CFG_REG_ADDR,DBGMCU_CFG_STOP_CLK_STS_MASK);
    }
    else
    {
        REG_BIT_CLEAR32(DBGMCU_CFG_REG_ADDR,DBGMCU_CFG_STOP_CLK_STS_MASK);
    }
}


/*!
* @brief       Configures debug behavior during STANDBY mode
*
* @param       enable          Control whether debug remains active in STANDBY mode
*
* @retval      None
*
* @note        Determines if debug monitoring continues when MCU enters STANDBY
* @note        ENABLE = Core debug registers accessible during STANDBY
* @note        DISABLE = Full power-down with debug disconnect
* @note        Affects wake-up behavior from deep sleep
*/
FUNC(void, MCU_CODE) Mcu_DbgMcu_ConfigStandbyMode(boolean enable)
{
    if (enable == (boolean)ENABLE)
    {
        REG_BIT_SET32(DBGMCU_CFG_REG_ADDR,DBGMCU_CFG_STANDBY_CLK_STS_MASK);
    }
    else
    {
        REG_BIT_CLEAR32(DBGMCU_CFG_REG_ADDR,DBGMCU_CFG_STANDBY_CLK_STS_MASK);
    }
}


/*!
* @brief       Configures peripheral debug freeze behavior
* 
* @param       dbgmcuMode   Peripheral selection (DBG_APBx_PER_XXX)
* @param       enable       Freeze control (ENABLE=run during debug, DISABLE=freeze)
* 
* @note        Controls peripheral behavior during debugger halt:
*              - ENABLE: Peripheral continues operation during debug halt
*              - DISABLE: Peripheral freezes when debugger halts CPU
* 
*/
FUNC(void, MCU_CODE) Mcu_DbgMcu_ConfigWorkStatus(Mcu_DbgMcu_APBPerType dbgmcuMode,boolean enable)
{
    if (enable == (boolean)DISABLE)
    {
        switch (dbgmcuMode)
        {
            case DBG_APB1_PER_TMR2 :
                REG_BIT_SET32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR2_STS_MASK);
                break;
            
            case DBG_APB1_PER_TMR3 :
                REG_BIT_SET32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR3_STS_MASK);
                break;
            
            case DBG_APB1_PER_TMR6 :
                REG_BIT_SET32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR6_STS_MASK);
                break;
            
            case DBG_APB1_PER_TMR4 :
                REG_BIT_SET32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR4_STS_MASK);
                break;
            
            case DBG_APB1_PER_RTC :
                REG_BIT_SET32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_RTC_STS_MASK);
                break;
            
            case DBG_APB1_PER_IWDT :
                REG_BIT_SET32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_IWDT_STS_MASK);
                break;
            
            case DBG_APB2_PER_TMR1 :
                REG_BIT_SET32(DBGMCU_APB2F_REG_ADDR,DBGMCU_APB2F_TMR1_STS_MASK);
                break; 
            
            case DBG_APB2_PER_TMR7 :
                REG_BIT_SET32(DBGMCU_APB2F_REG_ADDR,DBGMCU_APB2F_TMR7_STS_MASK);
                break; 
            
            case DBG_APB2_PER_TMR8 :
                REG_BIT_SET32(DBGMCU_APB2F_REG_ADDR,DBGMCU_APB2F_TMR8_STS_MASK);
                break; 
            
            default:
                {
                    /* nothing */
                }
                break;
        }
    }
    else
    {
        switch (dbgmcuMode)
        {
            case DBG_APB1_PER_TMR2 :
                REG_BIT_CLEAR32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR2_STS_MASK);
                break;
            
            case DBG_APB1_PER_TMR3 :
                REG_BIT_CLEAR32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR3_STS_MASK);
                break;
            
            case DBG_APB1_PER_TMR6 :
                REG_BIT_CLEAR32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR6_STS_MASK);
                break;
            
            case DBG_APB1_PER_TMR4 :
                REG_BIT_CLEAR32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_TMR4_STS_MASK);
                break;
            
            case DBG_APB1_PER_RTC :
                REG_BIT_CLEAR32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_RTC_STS_MASK);
                break;
            
            case DBG_APB1_PER_IWDT :
                REG_BIT_CLEAR32(DBGMCU_APB1F_REG_ADDR,DBGMCU_APB1F_IWDT_STS_MASK);
                break;
            
            case DBG_APB2_PER_TMR1 :
                REG_BIT_CLEAR32(DBGMCU_APB2F_REG_ADDR,DBGMCU_APB2F_TMR1_STS_MASK);
                break; 
            
            case DBG_APB2_PER_TMR7 :
                REG_BIT_CLEAR32(DBGMCU_APB2F_REG_ADDR,DBGMCU_APB2F_TMR7_STS_MASK);
                break; 
            
            case DBG_APB2_PER_TMR8 :
                REG_BIT_CLEAR32(DBGMCU_APB2F_REG_ADDR,DBGMCU_APB2F_TMR8_STS_MASK);
                break; 
            
            default:
                {
                    /* nothing */
                }
                break;
        }
    }
}

/*!
* @brief       Initializes debug mcu configuration module
*
* @param       dbgmcuConfig      Pointer to configuration structure
*
* @retval      None
*
*/
FUNC(void, MCU_CODE) Mcu_DbgMcu_Init(const Mcu_DbgMcu_CfgTypes * dbgmcuConfig)
{
    uint8 tempVal = 0;
    
    Mcu_DbgMcu_ConfigStopMode(dbgmcuConfig->stopEnable);
    Mcu_DbgMcu_ConfigStandbyMode(dbgmcuConfig->standbyEnable);
    
    for (tempVal = 0; tempVal <= (uint8)MCU_DBGMCU_MODE_CONUT; tempVal++)
    {
        Mcu_DbgMcu_ConfigWorkStatus(dbgmcuConfig->work[tempVal].module,
                                    dbgmcuConfig->work[tempVal].enable);
    }
}

/**@} 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 */
