/******************************************************************************
 * @file     wdt.c
 * @brief    Watchog firmware functions.
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include "wdt.h"


#if defined(__CC_ARM)
    #pragma push
    #pragma O1
#endif




/******************************************************************************
 * @brief      Initialize the Watchdog Timer
 *
 * @param[in]  WDTx     :  Select the watchdog module
 * @param[in]  u32TimeMs:  Watchdog timeout in ms
 *
 * @return     none
 *
 ******************************************************************************/
#if defined(__ICCARM__)
#pragma optimize=low
#endif
void WDT_Init(WDT_REGS *WDTx, uint32_t u32TimeMs)
{
    uint32_t u32WdtClk;

    /* Check WDT lock status */
    if (WDT_GetLockStatus(WDTx) == 1)
    {
        /* Enable write access to all other registers */
        WDT_WALLOW(WDTx);
    }

    /* Enable and Get Watchdog module clock */
    if (WDTx == WDT0)
    {
        CLOCK_EnableModule(WDT0_MODULE);

        u32WdtClk = CLOCK_GetModuleClock(WDT0_MODULE);
    }
    else
    {
        CLOCK_EnableModule(WDT1_MODULE);

        u32WdtClk = CLOCK_GetModuleClock(WDT1_MODULE);
    }

    /* Watchdog load register value */
    WDTx->WDTLOAD.all = u32WdtClk / 1000 * u32TimeMs;

    /* Watchdog control - Reset output enable, lockup_run and halted_run enable */
    WDTx->WDTCTL.all = (WDTCTL_ALL_RSTEN_ENABLE | WDTCTL_ALL_HALTEDRUN_ENABLE | WDTCTL_ALL_LOCKUPRUN_ENABLE);

    /* Watchdog counter and interrupt enable */
//  WDTx->WDTCTL.all |= WDTCTL_ALL_INTEN_ENABLE;
}


#if defined(__CC_ARM)
    #pragma pop
#endif


/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
