1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/ioctl.h>
10 #include <aos/errno.h>
11 #include "aos_hal_wdg.h"
12 #ifndef AOS_BOARD_HAAS700
13 #include <vfsdev/wdg_dev.h>
14 #endif
15
aos_hal_wdg_init(wdg_dev_t * wdg)16 int32_t aos_hal_wdg_init(wdg_dev_t *wdg)
17 {
18 #ifndef AOS_BOARD_HAAS700
19 uint32_t flags = 0;
20 int32_t ret = 0;
21 int32_t *p_fd = NULL;
22 char name[16] = {0};
23
24 if (!wdg || wdg->priv)
25 return -EINVAL;
26
27 p_fd = (int32_t *)malloc(sizeof(int32_t));
28 if (!p_fd)
29 return -ENOMEM;
30 *p_fd = -1;
31
32 snprintf(name, sizeof(name), "/dev/wdg%d", wdg->port);
33
34 *p_fd = open(name, 0);
35
36 if (*p_fd < 0) {
37 printf ("open %s failed, fd:%d\r\n", name, *p_fd);
38 ret = -EIO;
39 goto out;
40 }
41
42 out:
43 if (!ret) {
44 wdg->priv = p_fd;
45 } else {
46 if (*p_fd >= 0)
47 close(*p_fd);
48 free(p_fd);
49 p_fd = NULL;
50 }
51 return ret;
52 #else
53 return -1;
54 #endif
55 }
56
aos_hal_wdg_reload(wdg_dev_t * wdg)57 void aos_hal_wdg_reload(wdg_dev_t *wdg)
58 {
59 #ifndef AOS_BOARD_HAAS700
60 int32_t *p_fd = NULL;
61 int32_t ret = -1;
62
63 if (!wdg)
64 return;
65
66 p_fd = (int32_t *)wdg->priv;
67
68 if (!p_fd || *p_fd < 0)
69 return;
70
71 ret = ioctl(*p_fd, IOC_WDG_RELOAD, 0);
72 if (ret) {
73 printf ("reload wdg%d failed, ret:%d\r\n", wdg->port, ret);
74 }
75
76 return;
77 #else
78 return -1;
79 #endif
80 }
81
aos_hal_wdg_finalize(wdg_dev_t * wdg)82 int32_t aos_hal_wdg_finalize(wdg_dev_t *wdg)
83 {
84 #ifndef AOS_BOARD_HAAS700
85 int32_t ret = 0;
86 int32_t port = 0;
87 int32_t *p_fd = NULL;
88
89 if (!wdg || !wdg->priv)
90 return -EINVAL;
91
92 p_fd = (int32_t *)wdg->priv;
93
94 if (*p_fd < 0)
95 return -EIO;
96
97 if (*p_fd >= 0) {
98 close(*p_fd);
99 } else
100 ret = -EALREADY;
101
102 wdg->priv = NULL;
103
104 *p_fd = -1;
105 free(p_fd);
106
107 return ret;
108 #else
109 return -1;
110 #endif
111 }
112