/*!
 * @file        bmp561_wupt.c
 *
 * @brief       This file provides all the WUPT firmware functions
 *
 * @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.
 */

#include "bmp561_wupt.h"
#include "bmp561_sysctrl.h"

/** @addtogroup Peripherals_Library Standard Peripheral Library
  @{
*/

/** @addtogroup WUPT_Driver  WUPT Driver
  @{
*/

/** @addtogroup WUPT_Functions Functions
  @{
*/

/*!
 * @brief     Resets the WUPT peripheral registers to their default reset values.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WUPT_Reset(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphReset(SYSCTRL_APB_WUPT);
    SYSCTRL_DisableAPBPeriphReset(SYSCTRL_APB_WUPT);
    SYSCTRL_Lock();
}

/*!
 * @brief     Enables the WUPT module interrupt.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WUPT_EnableInterrupt(void)
{
    WUPT->CSTS_B.WUPIE = BIT_SET;
}

/*!
 * @brief     Disables the WUPT module interrupt.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WUPT_DisableInterrupt(void)
{
    WUPT->CSTS_B.WUPIE = BIT_RESET;
}

/*!
 * @brief     Enables Auto-wakeup.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WUPT_EnableAutoWakeup(void)
{
    WUPT->CSTS_B.WUPTEN = BIT_SET;
}

/*!
 * @brief     Disables Auto-wakeup.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WUPT_DisableAutoWakeup(void)
{
    WUPT->CSTS_B.WUPTEN = BIT_RESET;
}

/*!
 * @brief     Reads wake up signal valid status.
 *
 * @param     None
 *
 * @retval    The wake up signal valid status.
 *
 */
uint16_t WUPT_ReadWakeUpStatus(void)
{
    return WUPT->CSTS_B.WAKE_STATE;
}

/*!
 * @brief     Clears wake up signal valid status.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WUPT_ClearWakeUpStatus(void)
{
    WUPT->SCR_B.CLR_STATE = BIT_SET;
}

/*!
 * @brief     Sets asynchronous prescaler value.
 *
 * @param     prescaler: specifies the WUPT counter clock prescaler.
 *                       This parameter can be configured from 0x00 to 0x3F.
 *
 * @retval    None
 *
 * @note      Counter clock divisor =  prescaler + 2;
 *            eg: prescaler = 0x00, counter clock is divided by 2.
 *
 */
void WUPT_SetPrescaler(uint16_t prescaler)
{
    WUPT->CFG_B.DIV = (uint32_t)prescaler;
}

/*!
 * @brief     Sets auto-wakeup timebase.
 *
 * @param     cycle: specifies Auto-wakeup interrupt interval time.
 *                   This parameter can be configured from 0x00 to 0x3FFFF.
 *
 * @retval    None
 *
 * @note      Basetime = (1 / 65K) * (WUPT->CFG_B.DIV + 2) * (WUPT->CFG_B.TBC + 1), unit is "s"
 *
 */
void WUPT_SetAutoWakeupTimebase(uint32_t cycle)
{
    WUPT->CFG_B.TBC = cycle;
}

/*!
 * @brief     Sets auto-wakeup timebase.
 *
 * @param     None
 *
 * @retval    The specifies Auto-wakeup interrupt interval time.
 *
 */
uint32_t WUPT_GetAutoWakeupTimebase(void)
{
    return (uint32_t)(WUPT->CFG_B.TBC);
}

/*!
 * @brief     Clears asynchronous prescaler value and auto-wakeup timebase.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WUPT_ClearDivAndTimebase(void)
{
    WUPT->SCR_B.CLR_CNT = BIT_SET;
}

/**@} end of group WUPT_Functions*/
/**@} end of group WUPT_Driver*/
/**@} end of group Peripherals_Library*/
