/*!
 * @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 "board_delay.h"
#include <stdio.h>
#include "bmp561_wupt.h"
#include "bmp561_gpio.h"
#include "bmp561_sysctrl.h"
#include "bmp561_misc.h"

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

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

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

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

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

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

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

/*!
 * @brief   Main program
 *
 * @param   None
 *
 * @retval  None
 */
int main(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_ConfigLowPowerMode(SYSCTRL_LP_SLEEP);
    SYSCTRL_Lock();

    GPIO_Init();
    WUPT_Init();
    GPIO_SetBit(GPIO_PIN_4);
    /* Enter low power mode */
    __WFI();
    GPIO_ResetBit(GPIO_PIN_4);

    while (1)
    {
    }
}

/*!
 * @brief   WUPT initialization
 *
 * @param   None
 *
 * @retval  None
 */
void WUPT_Init(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_WUPT);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_WUPT);
    SYSCTRL_Lock();

    /* Prescaler = 0x3F + 2 = 65 */   
    WUPT_SetPrescaler(0x3F);
    /* Wake up time base = 999 + 1 = 1000 */
    while (WUPT_GetAutoWakeupTimebase() != 999)
    {
        WUPT_SetAutoWakeupTimebase(999);
    }

    WUPT_EnableInterrupt();
    WUPT_ClearWakeUpStatus();
    NVIC_EnableIRQRequest(WUPT_IRQn, 0xF);

    WUPT_EnableAutoWakeup();
}

/*!
 * @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.pin = GPIO_PIN_4;
    gpioConfig.mode = GPIO_MODE_OUT;
    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_PP;
    gpioConfig.pullEn = DISABLE;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.drive = GPIO_DS_HIGH;
    GPIO_Config(&gpioConfig);
    GPIO_ResetBit(GPIO_PIN_4);
}

/*!
 * @brief   WUPT interrupt server handler
 *
 * @param   None
 *
 * @retval  None
 */
void WUPT_IRQ_Handler(void)
{
    if (WUPT_ReadWakeUpStatus())
    {
        WUPT_ClearWakeUpStatus();
    }
}
