/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2025-01-01
 *
 * @attention
 *
 *  Copyright (C) 2025 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  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 SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes ***************************************************************/
#include "main.h"
#include <stdio.h>
#include "bmp561_sysctrl.h"
#include "bmp561_misc.h"
#include "bmp561_gpio.h"
#include "bmp561_eint.h"

/* Private includes *******************************************************/

/* Private macro **********************************************************/

/* Private typedef ********************************************************/

/* Private variables ******************************************************/

/* Private function prototypes ********************************************/
void EINT_Init(void);
void GPIO_Init(void);

/* External variables *****************************************************/

/* External functions *****************************************************/

/*!
 * @brief   Main program
 *
 * @param   None
 *
 * @retval  None
 */
int main(void)
{
    BOARD_Delay_Config();
    GPIO_Reset();
    BOARD_Delay_Ms(3000);
    BOARD_Delay_DeInit();

    SYSCTRL_Unlock();
    SYSCTRL_ConfigLowPowerMode(SYSCTRL_LP_SLEEP);
    SYSCTRL_Lock();

    EINT_Init();
    GPIO_Init();

    /* Enter low power mode */
    __WFI();

    BOARD_Delay_Config();

    while (1)
    {
        BOARD_Delay_Ms(500);
        GPIO_SetBit(GPIO_PIN_3);
        BOARD_Delay_Ms(500);
        GPIO_ResetBit(GPIO_PIN_3);
    }
}

/*!
 * @brief   EINT initialization
 *
 * @param   None
 *
 * @retval  None
 */
void EINT_Init(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAHBPeriphClock(SYSCTRL_AHB_GPIO);
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_EINT);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_EINT);
    SYSCTRL_Lock();

    GPIO_Config_T gpioConfig;
    gpioConfig.pin = GPIO_PIN_4;
    gpioConfig.mode = GPIO_MODE_AF;
    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_PP;
    gpioConfig.pullEn = ENABLE;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.drive = GPIO_DS_HIGH;
    GPIO_Config(&gpioConfig);

    GPIO_ConfigPinReuse(GPIO_PIN_4, GPIO_REUSE_AF1);

    EINT_Reset();
    EINT_ConfigClockDiv(EINT_PORT_2, EINT_DIV_1);
    EINT_ConfigFilter(EINT_PORT_2, EINT_FILTER_0);
    EINT_ConfigTrig(EINT_PORT_2, EINT_TRIGGER_FALLING);
    EINT_EnableInterrupt(EINT_PORT_2);
    EINT_ClearInterruptStatus(EINT_PORT_2);
    NVIC_EnableIRQRequest(EINT2_IRQn, 0xF);

    EINT_EnableModule(EINT_PORT_2);
}

/*!
 * @brief   GPIO initialization
 *
 * @param   None
 *
 * @retval  None
 */
void GPIO_Init(void)
{
    GPIO_Config_T gpioConfig;

    SYSCTRL_Unlock();
    SYSCTRL_EnableAHBPeriphClock(SYSCTRL_AHB_GPIO);
    SYSCTRL_Lock();

    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_PP;
    gpioConfig.pullEn = DISABLE;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.drive = GPIO_DS_HIGH;
    gpioConfig.pin = GPIO_PIN_3;
    gpioConfig.mode = GPIO_MODE_OUT;
    GPIO_Config(&gpioConfig);
}

/*!
 * @brief   EINT interrupt server handler
 *
 * @param   None
 *
 * @retval  None
 */
void EINT_IRQ_Handler(void)
{
    if (EINT_ReadInterruptStatus(EINT_PORT_2))
    {
        EINT_ClearInterruptStatus(EINT_PORT_2);
    }
}
