/*!
* @file     Mcu_Sysc.c
*
* @version  1.0.0
*
* @brief    The underlying driver code of the SYSCFG 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_SysCfgReg.h"
#include "Mcu_SysCfgTypes.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_SYSC_VENDOR_ID                              0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_SYSC_AR_RELEASE_MAJOR_VERSION               0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_SYSC_AR_RELEASE_MINOR_VERSION               0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define MCU_SYSC_AR_RELEASE_REVISION_VERSION            0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */ 
#define MCU_SYSC_SW_MAJOR_VERSION                       0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_SYSC_SW_MINOR_VERSION                       0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define MCU_SYSC_SW_PATCH_VERSION                       0x00U

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

/** @defgroup MCU_Fuctions
  @{
*/

/*!
* @brief       Configures DMA request remapping settings
*
* @param       module          Target DMA channel or peripheral
* @param       enable          Remapping state (REMAP_ENABLE/REMAP_DISABLE)
*
* @retval      None
*
* @note        Static internal function for DMA remapping control
* @note        Operates on SYSCFG_CFG1 register
*/
static FUNC(void, MCU_CODE) Mcu_Sysc_DmaRmpConfig(Mcu_Sysc_DamRemapType module, 
                                                  Mcu_Sysc_RemappingType enable)
{
    if (REMAP_ENABLE == enable)
    {
        REG_BIT_SET32(SYSCFG_CFG1_REG_ADDR,(uint32)module);
    }
    else if (REMAP_DISABLE == enable)
    {
        REG_BIT_CLEAR32(SYSCFG_CFG1_REG_ADDR,(uint32)module);
    }
    else
    {
        /* nothing */
    }
}

/*******************************************************************************
                              GLOBAL FUNCTION
*******************************************************************************/
/*!
* @brief       Configures external interrupt (EINT) pin-port mapping
*
* @param       portPin         Target external interrupt line (0-15)
* @param       port            Associated GPIO port
*
* @retval      None
*
* @note        Maps EINT lines to specific GPIO ports
* @note        Uses 4 configuration registers (EINTCFG1-EINTCFG4)
* @note        Groups pins 0-3, 4-7, 8-11, 12-15
*/
FUNC(void, MCU_CODE) Mcu_Sysc_EintConfig(Mcu_Sysc_PinType portPin, Mcu_Sysc_PortType port)
{
    if (portPin <= SYSCFG_PIN_3)
    {
        REG_BIT_CLEAR32(SYSCFG_EINTCFG1_REG_ADDR, (uint32)((uint32)0xF << (uint32)((uint32)portPin * (uint32)4)));
        REG_BIT_SET32(SYSCFG_EINTCFG1_REG_ADDR, (uint32)((uint32)port << (uint32)((uint32)portPin * (uint32)4)));
    }
    else if (((uint8)portPin > (uint8)SYSCFG_PIN_3) && ((uint8)portPin <= (uint8)SYSCFG_PIN_7))
    {
        REG_BIT_CLEAR32(SYSCFG_EINTCFG2_REG_ADDR, (uint32)((uint32)0xF << (((uint32)portPin - (uint32)0x4) * (uint32)4)));
        REG_BIT_SET32(SYSCFG_EINTCFG2_REG_ADDR, (uint32)((uint32)port << (((uint32)portPin - (uint32)0x4) * (uint32)4)));
    }
    else if (((uint8)portPin > (uint8)SYSCFG_PIN_7) && ((uint8)portPin <= (uint8)SYSCFG_PIN_11))
    {
        REG_BIT_CLEAR32(SYSCFG_EINTCFG3_REG_ADDR, (uint32)((uint32)0xF << (((uint32)portPin - (uint32)0x8) * (uint32)4)));
        REG_BIT_SET32(SYSCFG_EINTCFG3_REG_ADDR, (uint32)((uint32)port << (((uint32)portPin - (uint32)0x8) * (uint32)4)));
    }
    else if (((uint8)portPin > (uint8)SYSCFG_PIN_11) && ((uint8)portPin <= (uint8)SYSCFG_PIN_15))
    {
        REG_BIT_CLEAR32(SYSCFG_EINTCFG4_REG_ADDR, (uint32)((uint32)0xF << (((uint32)portPin - (uint32)0xC) * (uint32)4)));
        REG_BIT_SET32(SYSCFG_EINTCFG4_REG_ADDR, (uint32)((uint32)port << (((uint32)portPin - (uint32)0xC) * (uint32)4)));
    }
    else
    {
        /* nothing */
    }
}

/*!
* @brief       Initializes system configuration module
*
* @param       syscConfig      Pointer to configuration structure
*
* @retval      None
*
* @note        Main initialization for DMA remapping and EINT configuration
* @note        Enables SYSCFG clock before configuration
* @note        Processes DMA remap and EINT config arrays sequentially
* @note        Applies final lock setting to SYSCFG_CFG2 register
*/
FUNC(void, MCU_CODE) Mcu_Sysc_Init(const Mcu_Sysc_CfgTypes * syscConfig)
{
    uint8 tempVal = 0;
    
    REG_BIT_SET32(SYSCFG_CFG1_REG_ADDR,(uint32)syscConfig->memSel);
    REG_BIT_SET32(RCM_APBCLKEN2_REG_ADDR,(uint32)RCM_APB2_PERIPH_SYSCFG);
    for (tempVal = 0; tempVal < (uint8)MCU_SYSC_DMARMP_CONUT; tempVal++)
    {
        Mcu_Sysc_DmaRmpConfig(syscConfig->dmaRmpCfg[tempVal].module,
                              syscConfig->dmaRmpCfg[tempVal].enable);
    }
    
    for (tempVal = 0; tempVal < (uint8)MCU_SYSC_ENIT_CONUT; tempVal++)
    {
        Mcu_Sysc_EintConfig(syscConfig->enitCfg[tempVal].portPin,
                            syscConfig->enitCfg[tempVal].port);
    }
    
    REG_BIT_SET32(SYSCFG_CFG2_REG_ADDR,(uint32)syscConfig->lock);
}

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