/*!
 * @file        bmp561_wwdt.c
 *
 * @brief       This file provides all the WWDT 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_wwdt.h"
#include "bmp561_sysctrl.h"

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

/** @addtogroup WWDT_Driver  WWDT Driver
  @{
*/

/** @addtogroup WWDT_Functions Functions
  @{
*/

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

/*!
 * @brief     Sets WWDT prescaler values
 *
 * @param     prescaler: WWDT count reload values.
 *                       The parameter can be one of the following values:
 *                       @arg WWDT_PS_0 : WWDT counter clock = Fsys/2^0
 *                       @arg WWDT_PS_1 : WWDT counter clock = Fsys/2^1
 *                       @arg WWDT_PS_2 : WWDT counter clock = Fsys/2^2
 *                       @arg WWDT_PS_3 : WWDT counter clock = Fsys/2^3
 *                       @arg WWDT_PS_4 : WWDT counter clock = Fsys/2^4
 *                       @arg WWDT_PS_5 : WWDT counter clock = Fsys/2^5
 *                       @arg WWDT_PS_6 : WWDT counter clock = Fsys/2^6
 *                       @arg WWDT_PS_7 : WWDT counter clock = Fsys/2^7
 *                       @arg WWDT_PS_8 : WWDT counter clock = Fsys/2^8
 *                       @arg WWDT_PS_9 : WWDT counter clock = Fsys/2^9
 *                       @arg WWDT_PS_10: WWDT counter clock = Fsys/2^10
 *                       @arg WWDT_PS_11: WWDT counter clock = Fsys/2^11
 *                       @arg WWDT_PS_12: WWDT counter clock = Fsys/2^12
 *                       @arg WWDT_PS_13: WWDT counter clock = Fsys/2^13
 *                       @arg WWDT_PS_14: WWDT counter clock = Fsys/2^14
 *                       @arg WWDT_PS_15: WWDT counter clock = Fsys/2^15
 *
 * @retval    None
 */
void WWDT_ConfigPrescaler(WWDT_PS_T prescaler)
{
    WWDT->PS = (uint32_t)prescaler;
}

/*!
 * @brief     Enables WWDT and load the counter value.
 *
 * @param     None
 *
 * @retval    None
 */
void WWDT_FeedDog(void)
{
    WWDT->TRG = WWDT_FEEDDOG;
}

/*!
 * @brief     Sets WWDT window value.
 *
 * @param     WindowVal: specifies the window value to be compared to the downcounter.
 *                       The parameter can be configured between 0 and 0xFFFF.
 *
 * @retval    None
 *
 * @note      Feed Dog should be done before the counter arrived windowValue, otherwise occurs system reset.
 */
void WWDT_SetWindowValue(uint16_t windowVal)
{
    WWDT->WIND = (uint32_t)windowVal;
}

/*!
 * @brief     Sets WWDT counter value.
 *
 * @param     counter: specifies the watchdog counter value.
 *                     The parameter can be configured between 0 and 0xFFFF.
 *
 * @retval    None
 *
 * @note      When execute Feed Dog, register RV value will reload to counter.
 */
void WWDT_SetCounter(uint16_t counter)
{
    WWDT->RV = (uint32_t)counter;
}

/*!
 * @brief     Gets WWDT counter value.
 *
 * @param     None
 *
 * @retval    The window watchdog counter value
 *
 */
uint16_t WWDT_GetCounter(void)
{
    return (uint16_t)WWDT->CNT;
}

/*!
 * @brief     Enables WWDT window counter match interrupt.
 *
 * @param     None
 *
 * @retval    None
 *
 * @note      When counter equal to WindowValue occurs match interrupt.
 *
 */
void WWDT_EnableInterrupt(void)
{
    WWDT->IEN_B.WINDMIE = BIT_SET;
}

/*!
 * @brief     Disables WWDT window counter match interrupt.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WWDT_DisableInterrupt(void)
{
    WWDT->IEN_B.WINDMIE = BIT_RESET;
}

/*!
 * @brief     Reads the window counter match interrupt flag.
 *
 * @param     None
 *
 * @retval    The new state of the window counter match interrupt flag.
 *
 */
uint8_t WWDT_ReadIntFlag(void)
{
    return WWDT->SR_B.WINDMF;
}

/*!
 * @brief     Clears the window counter match interrupt flag.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void WWDT_ClearIntFlag(void)
{
    WWDT->SCR_B.WINDMFC = BIT_SET;
}

/**@} end of group WWDT_Functions*/
/**@} end of group WWDT_Driver*/
/**@} end of group Peripherals_Library*/
