#include "../include/hal_lksmcu_iwdg.h"
#include "../hal_lks32mc09x.h"
#include "lks32mc09x_user_manual.h"
#include "../include/hal_lksmcu_reg.h"
#include "../include/hal_lks32mc09x_reg.h"
/**
 * @brief 模块复位
 * @param iwdg 看门狗模块
 */
void HAL_IWDG_Reset(HAL_IWDG_x iwdg)
{
    HAL_IWDG_Disable(iwdg);
}

/**
 * @brief 模块打开
 * @param iwdg 看门狗模块
 */
void HAL_IWDG_Enable(HAL_IWDG_x iwdg)
{
    HAL_REG_SET(HAL_IWDG[iwdg]->CFG, IWDG_CFG_WDG_EN_BIT);
}

/**
 * @brief 模块关闭
 * @param iwdg 看门狗模块
 */
void HAL_IWDG_Disable(HAL_IWDG_x iwdg)
{
    (void)iwdg;
    // 写0同时向[15:8]写入0x3C可清零
    HAL_REG_WRITE_BIT(IWDG_CFG, 0x3c00 | IWDG_CFG_WDG_EN_BIT, 0x3c00);
}

/**
 * @brief 设置看门狗超时复位时间
 * @param iwdg 看门狗模块
 * @param ms 超时复位时间,单位mS
 */
void HAL_IWDG_SetResetTimemS(HAL_IWDG_x iwdg, int32_t ms)
{
    uint32_t th;
    th = ((uint32_t)(32 * ms) >> IWDG_RTH_RTH_POS);
    HAL_REG_WRITE(HAL_IWDG[iwdg]->PSW, 0xA6B4);
    HAL_REG_WRITE_BIT_MASK_POS(IWDG_RTH, IWDG_RTH_RTH, th);
}

/**
 * @brief 喂狗
 * @param iwdg 看门狗模块
 */
void HAL_IWDG_Feed(HAL_IWDG_x iwdg)
{
    (void)iwdg;
    HAL_REG_WRITE(HAL_IWDG[0]->PSW, 0xA6B4);
    HAL_REG_WRITE(HAL_IWDG[0]->CLR, 0x798D);
}
