1 /**************************************************************************//**
2  * @file     wdt.c
3  * @version  V3.00
4  * @brief    Watchdog Timer(WDT) driver source file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #include "NuMicro.h"
10 
11 
12 /** @addtogroup Standard_Driver Standard Driver
13   @{
14 */
15 
16 /** @addtogroup WDT_Driver WDT Driver
17   @{
18 */
19 
20 /** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions
21   @{
22 */
23 
24 /**
25   * @brief      Initialize WDT and start counting
26   *
27   * @param[in]  u32TimeoutInterval  Time-out interval period of WDT module. Valid values are:
28   *                                 - \ref WDT_TIMEOUT_2POW4
29   *                                 - \ref WDT_TIMEOUT_2POW6
30   *                                 - \ref WDT_TIMEOUT_2POW8
31   *                                 - \ref WDT_TIMEOUT_2POW10
32   *                                 - \ref WDT_TIMEOUT_2POW12
33   *                                 - \ref WDT_TIMEOUT_2POW14
34   *                                 - \ref WDT_TIMEOUT_2POW16
35   *                                 - \ref WDT_TIMEOUT_2POW18
36   *                                 - \ref WDT_TIMEOUT_2POW20
37   * @param[in]  u32ResetDelay       Configure WDT time-out reset delay period. Valid values are:
38   *                                 - \ref WDT_RESET_DELAY_1026CLK
39   *                                 - \ref WDT_RESET_DELAY_130CLK
40   *                                 - \ref WDT_RESET_DELAY_18CLK
41   *                                 - \ref WDT_RESET_DELAY_3CLK
42   * @param[in]  u32EnableReset      Enable WDT time-out reset system function. Valid values are TRUE and FALSE.
43   * @param[in]  u32EnableWakeup     Enable WDT time-out wake-up system function. Valid values are TRUE and FALSE.
44   *
45   * @retval     WDT_OK              WDT operation OK.
46   * @retval     WDT_ERR_TIMEOUT     WDT operation abort due to timeout error.
47   *
48   * @details    This function makes WDT module start counting with different time-out interval, reset delay period and choose to \n
49   *             enable or disable WDT time-out reset system or wake-up system.
50   * @note       Please make sure that Register Write-Protection Function has been disabled before using this function.
51   */
WDT_Open(uint32_t u32TimeoutInterval,uint32_t u32ResetDelay,uint32_t u32EnableReset,uint32_t u32EnableWakeup)52 int32_t WDT_Open(uint32_t u32TimeoutInterval,
53               uint32_t u32ResetDelay,
54               uint32_t u32EnableReset,
55               uint32_t u32EnableWakeup)
56 {
57     uint32_t u32TimeOutCnt = WDT_TIMEOUT;
58 
59     WDT->ALTCTL = u32ResetDelay;
60 
61     WDT->CTL = u32TimeoutInterval | WDT_CTL_WDTEN_Msk |
62                (u32EnableReset << WDT_CTL_RSTEN_Pos) |
63                (u32EnableWakeup << WDT_CTL_WKEN_Pos);
64 
65     while((WDT->CTL & WDT_CTL_SYNC_Msk) == WDT_CTL_SYNC_Msk) /* Wait enable WDTEN bit completed, it needs 2 * WDT_CLK. */
66     {
67         if(--u32TimeOutCnt == 0) return WDT_ERR_TIMEOUT;
68     }
69 
70     return WDT_OK;
71 }
72 
73 /**@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */
74 
75 /**@}*/ /* end of group WDT_Driver */
76 
77 /**@}*/ /* end of group Standard_Driver */
78