1 /*
2  * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author        Notes
8  * 2018-11-9      fanwenl       1st version
9  */
10 
11 #include <rtdevice.h>
12 #include "wm_watchdog.h"
13 
14 #ifdef BSP_USING_WDT
15 
wm_wdg_init(rt_watchdog_t * wdt)16 static rt_err_t wm_wdg_init(rt_watchdog_t *wdt)
17 {
18     /*init for 10S*/
19     tls_watchdog_init(1000000);
20     tls_watchdog_stop();
21     return RT_EOK;
22 }
23 
wm_wdg_control(rt_watchdog_t * wdt,int cmd,void * arg)24 static rt_err_t wm_wdg_control(rt_watchdog_t *wdt, int cmd, void *arg)
25 {
26     uint64_t  timeout_us = 0;
27     switch (cmd)
28     {
29     case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
30         timeout_us = *((rt_uint32_t *)arg) * 1000000;
31         if (timeout_us >= 0xFFFFFFFF)
32             timeout_us = 0xFFFFFFFF;
33         tls_watchdog_set_timeout((rt_uint32_t)timeout_us);
34         break;
35     case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
36         timeout_us = tls_watchdog_get_timeout();
37         *((rt_uint32_t *)arg) = timeout_us / 1000000;
38         break;
39     case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
40         timeout_us = tls_watchdog_get_timeleft();
41         *((rt_uint32_t *)arg) = timeout_us / 1000000;
42         break;
43     case RT_DEVICE_CTRL_WDT_KEEPALIVE:
44         tls_watchdog_clr();
45         break;
46     case RT_DEVICE_CTRL_WDT_START:
47         tls_watchdog_start();
48         break;
49     case RT_DEVICE_CTRL_WDT_STOP:
50         tls_watchdog_stop();
51         break;
52     default:
53         return -RT_EIO;
54     }
55     return RT_EOK;
56 }
57 
58 static const struct rt_watchdog_ops wm_wdg_pos =
59 {
60     wm_wdg_init,
61     wm_wdg_control,
62 };
63 
64 static rt_watchdog_t wm_wdg;
65 
wm_hw_wdg_init(void)66 int wm_hw_wdg_init(void)
67 {
68     wm_wdg.ops = &wm_wdg_pos;
69     rt_hw_watchdog_register(&wm_wdg, "wdg", 0, RT_NULL);
70     return RT_EOK;
71 }
72 
73 INIT_DEVICE_EXPORT(wm_hw_wdg_init);
74 
75 #endif /*BSP_USING_WDT */
76