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

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

/** @addtogroup IWDT_Driver IWDT Driver
  @{
*/

/** @addtogroup IWDT_Functions Functions
  @{
*/

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

/*!
 * @brief     Sets IWDT prescaler values
 *
 * @param     prescaler: IWDT count reload values.
 *                       The parameter can be one of the following values:
 *                       @arg IWDT_PS_0 : IWDT counter clock = Fsys/2^0
 *                       @arg IWDT_PS_1 : IWDT counter clock = Fsys/2^1
 *                       @arg IWDT_PS_2 : IWDT counter clock = Fsys/2^2
 *                       @arg IWDT_PS_3 : IWDT counter clock = Fsys/2^3
 *                       @arg IWDT_PS_4 : IWDT counter clock = Fsys/2^4
 *                       @arg IWDT_PS_5 : IWDT counter clock = Fsys/2^5
 *                       @arg IWDT_PS_6 : IWDT counter clock = Fsys/2^6
 *                       @arg IWDT_PS_7 : IWDT counter clock = Fsys/2^7
 *                       @arg IWDT_PS_8 : IWDT counter clock = Fsys/2^8
 *                       @arg IWDT_PS_9 : IWDT counter clock = Fsys/2^9
 *                       @arg IWDT_PS_10: IWDT counter clock = Fsys/2^10
 *                       @arg IWDT_PS_11: IWDT counter clock = Fsys/2^11
 *                       @arg IWDT_PS_12: IWDT counter clock = Fsys/2^12
 *                       @arg IWDT_PS_13: IWDT counter clock = Fsys/2^13
 *                       @arg IWDT_PS_14: IWDT counter clock = Fsys/2^14
 *                       @arg IWDT_PS_15: IWDT counter clock = Fsys/2^15
 *
 * @retval    None
 */
void IWDT_ConfigPrescaler(IWDT_PS_T prescaler)
{
    IWDT->PS = (uint32_t)prescaler;
}

/*!
 * @brief     Configures register TRG function
 *
 * @param     type: select TRG function type.
 *                  The parameter can be one of the following value:
 *                  @arg IWDT_FUNC_FEEDDOG: feed dog
 *                  @arg IWDT_FUNC_UNLOCK : unlock PS and RV registers
 *                  @arg IWDT_FUNC_ENABLE : enable iwdg
 *
 * @retval    None
 */
void IWDT_ConfigFunction(IWDT_FUNC_T type)
{
    IWDT->TRG = (uint32_t)type;
}

/*!
 * @brief     Sets IWDT count reload value
 *
 * @param     reload: IWDT count reload values.
 *            The parameter can be configured between 0 and 0xFF.
 *
 * @retval    None
 */
void IWDT_ConfigReload(uint16_t reload)
{
    IWDT->RV = (uint32_t)reload;
}

/*!
 * @brief     Read the IWDT flag.
 *
 * @param     flag: status flag of IWDT
 *                  This parameter can be any combination of the following values:
 *                  @arg IWDT_FLAG_TU: reload value update on going
 *                  @arg IWDT_FLAG_PU: prescaler value update on going
 *                  @arg IWDT_FLAG_RU: EEPROM single bit error
 *
 * @retval    IWDT flag.
 */
uint8_t IWDT_ReadFlag(IWDT_FLAG_T flag)
{
    return (IWDT->SR & (uint32_t)flag) ? SET : RESET;
}

/**@} end of group IWDT_Functions*/
/**@} end of group IWDT_Driver*/
/**@} end of group Peripherals_Library*/
