1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include "esp_task_wdt.h"
18 #include "watchdog_if.h"
19 #include "cmsis_os2.h"
20 #include "hdf_log.h"
21 #include "ohos_run.h"
22
23 #define SYS_DELAY_TICKS 200
24 #define TASK_STACK_SIZE 4096
25 #define TASK_PRIO 25
26
27 #define HDF_WATCHDOG_TIMEOUT 5 // 5秒喂狗
28 #define HDF_WATCHDOG_TIMEOUT_START 100
29 #define HDF_WATCHDOG_TIMEOUT_OFFSET 100
30
31 osThreadId_t g_taskID = NULL;
32
watchdog_api_test(DevHandle handle)33 void watchdog_api_test(DevHandle handle)
34 {
35 int32_t ret;
36 int32_t status;
37 uint32_t timeOut = 0;
38 ret = WatchdogGetStatus(handle, &status);
39 if (ret != 0) {
40 printf("WatchdogGetStatus: failed, ret %d\r\n", ret);
41 return NULL;
42 } else
43 printf("WatchdogGetStatus: %d\r\n", status);
44
45 ret = WatchdogGetTimeout(handle, &timeOut);
46 if (ret != 0) {
47 printf("WatchdogGetTimeout: failed, ret %d\r\n", ret);
48 return NULL;
49 } else
50 printf("WatchdogGetTimeout: %d\r\n", timeOut);
51
52 ret = WatchdogSetTimeout(handle, HDF_WATCHDOG_TIMEOUT);
53 if (ret != 0) {
54 printf("WatchdogSetTimeout: failed, ret %d\r\n", ret);
55 return NULL;
56 } else
57 printf("WatchdogSetTimeout: %d\r\n", HDF_WATCHDOG_TIMEOUT);
58
59 ret = WatchdogGetTimeout(handle, &timeOut);
60 if (ret != 0) {
61 printf("WatchdogGetTimeout: failed, ret %d\r\n", ret);
62 return NULL;
63 } else
64 printf("WatchdogGetTimeout: %d\r\n", timeOut);
65
66 ret = WatchdogStart(handle);
67 if (ret != 0) {
68 printf("WatchdogStart: failed, ret %d\r\n", ret);
69 return NULL;
70 } else
71 printf("---> WatchdogStart.\r\n");
72
73 ret = WatchdogGetStatus(handle, &status);
74 if (ret != 0) {
75 printf("WatchdogGetStatus: failed, ret %d\r\n", ret);
76 return NULL;
77 } else
78 printf("WatchdogGetStatus: %d\r\n", status);
79 }
80
watchdog_test(void)81 static void watchdog_test(void)
82 {
83 int32_t ret;
84 DevHandle handle = NULL;
85 uint32_t delay_cnt = 0;
86
87 ret = WatchdogOpen(0, &handle);
88 if (HDF_SUCCESS != ret || handle == NULL) {
89 printf("WatchdogOpen: failed, ret %d\r\n", ret);
90 return NULL;
91 }
92
93 watchdog_api_test(handle);
94
95 delay_cnt = HDF_WATCHDOG_TIMEOUT_START;
96 while (1) {
97 ret = WatchdogFeed(handle);
98 if (ret != 0) {
99 printf("WatchdogFeed: failed, ret %d\r\n", ret);
100 return NULL;
101 } else
102 printf("--> WatchdogFeed success!\r\n");
103 osDelay(delay_cnt); // 延时越来越长,直到系统复位
104 delay_cnt += HDF_WATCHDOG_TIMEOUT_OFFSET;
105 }
106 }
107
watchdog_example_task(void)108 static void watchdog_example_task(void)
109 {
110 printf("into watchdog hdf example!\r\n");
111 osThreadAttr_t attr;
112 attr.name = "watchdog_test";
113 attr.attr_bits = 0U;
114 attr.cb_mem = NULL;
115 attr.cb_size = 0U;
116 attr.stack_mem = NULL;
117 attr.stack_size = TASK_STACK_SIZE;
118 attr.priority = TASK_PRIO;
119 g_taskID = osThreadNew((osThreadFunc_t)watchdog_test, NULL, &attr);
120 if (g_taskID == NULL) {
121 printf("Failed to create Test Watchdog thread!\r\n");
122 }
123 }
124
125 OHOS_APP_RUN(watchdog_example_task);
126