/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  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 useful 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.
 */

/* Includes */
#include "Board.h"
#include "stdio.h"
#include "g32a10xx_int.h"
#include "user_eint_config.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup EINT_Examples
  @{
  */

/** @addtogroup EINT_Driver
  @{
  */

/** @defgroup EINT_Macros Macros
  @{
  */

/**@} end of group EINT_Macros */

/** @defgroup EINT_Functions Functions
  @{
  */

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Board_LedInit(LED2);
    Board_LedInit(LED3);

    /* Enable the BUTTON Clock */
    Rcm_EnableAhbPeriphClock(KEY1_BUTTON_GPIO_CLK);
    Rcm_EnableAhbPeriphClock(KEY2_BUTTON_GPIO_CLK);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_SYSCFG);

    /* Configure key1/2 pin as input floating */
    Gpio_Config(KEY1_BUTTON_GPIO_PORT, &gpioConfigKey1);
    Gpio_Config(KEY2_BUTTON_GPIO_PORT, &gpioConfigKey2);

    /* Configure key1/2 pin used as EINT Line */
    Syscfg_EintLine(SYSCFG_PORT_GPIOC, SYSCFG_PIN_0);
    Syscfg_EintLine(SYSCFG_PORT_GPIOC, SYSCFG_PIN_1);

    /* Configure key1/2 pin exit line */
    Eint_Config(&eintConfigKey1);
    Eint_Config(&eintConfigKey2);

    /* Configure NVIC_IRQRequest */
    Nvic_EnableIrqRequest(EINT0_1_IRQn, 0x0f);

    while (1)
    {
        /* nothing */
    }
}

/**
 * @brief       The interrupt will happen when the button is press ,
 *              and the Led will be on
 *
 * @param       None
 *
 * @retval      None
 *
 * @note        This function should put into EINT0_1_IRQHandler() in g32a10xx_int.c
 */
void Eint_G32aEval_PB_LED_Isr(void)
{
    if (Eint_ReadStatusFlag((uint32_t)KEY1_BUTTON_EXTI_LINE) == (uint8_t)SET)
    {
        /* The interrupt flag bit must be cleared first. */
        Eint_ClearStatusFlag((uint32_t)KEY1_BUTTON_EXTI_LINE);
        Board_LedToggle(LED2);
    }
    else
    {
        /* nothing */
    }

    if (Eint_ReadStatusFlag((uint32_t)KEY2_BUTTON_EXTI_LINE) == (uint8_t)SET)
    {
        /* The interrupt flag bit must be cleared first. */
        Eint_ClearStatusFlag((uint32_t)KEY2_BUTTON_EXTI_LINE);
        Board_LedToggle(LED3);
    }
    else
    {
        /* nothing */
    }
}

/**@} end of group EINT_Functions */
/**@} end of group EINT_Driver */
/**@} end of group EINT_Examples */
/**@} end of group Examples */
