1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 
9 #include "aos/kernel.h"
10 #include "aos/hal/wdg.h"
11 #include "aos/hal/flash.h"
12 #include "wdg_test.h"
13 
14 /* if WGD_TIMEOUT_MS is 1000, WGD_TIMEOUT_TOLERANCE_RANGE is 0.3f, wdg test will be
15    OK if the real timeout in the range [700, 1300] */
16 #define WGD_TIMEOUT_MS 1000
17 #define WGD_TIMEOUT_TOLERANCE_RANGE 0.2f
18 
19 static wdg_dev_t wdg_dev;
20 
21 static hal_logic_partition_t *partition_info = NULL;
22 
23 static int success_flag = 0;
24 
25 static void wdg_timeout_test(void);
26 static void wdg_normal_test(void);
27 static uint16_t read_wdg_flag(void);
28 static void write_wdg_flag(uint16_t value);
29 static void clear_wdg_flag(void);
30 
hal_wdg_test(void)31 void hal_wdg_test(void)
32 {
33     int ret = -1;
34 
35     uint16_t wdg_flag = 0;
36 
37     printf("*********** wdg test begin ! ***********\n");
38 
39     /* init the wdg */
40     wdg_dev.port = PORT_WDG_TEST;
41     wdg_dev.config.timeout = WGD_TIMEOUT_MS;
42     ret = hal_wdg_init(&wdg_dev);
43     if (ret != 0) {
44         printf("hal_wdg_init error !\n");
45         return;
46     }
47 
48     partition_info = hal_flash_get_info(PARTITION_WDG_TEST);
49     if (partition_info == NULL) {
50         printf("wdg_flag get error !\n");
51     }
52 
53     /* get the wdg flag */
54     wdg_flag = read_wdg_flag();
55 
56     switch(wdg_flag) {
57         case 0xF000:
58             printf("wdg_normal_test failed !\n");
59             success_flag = 0;
60             break;
61         case 0xFF00:
62             printf("wdg_timeout_test failed !\n");
63             success_flag = 0;
64             break;
65         case 0xFFF0:
66             printf("wdg_normal_test begin !\n");
67             wdg_normal_test();
68 
69             wdg_flag = read_wdg_flag();
70             if (wdg_flag == 0x0000) {
71                 success_flag = 1;
72             } else {
73                 printf("wdg_normal_test failed !\n");
74                 success_flag = 0;
75             }
76             break;
77         case 0xFFFF:
78             printf("wdg_timeout_test begin !\n");
79             wdg_timeout_test();
80             printf("wdg_timeout_test failed !\n");
81             success_flag = 0;
82             break;
83         default:
84             printf("wdg flag error !\n");
85             success_flag = 0;
86             break;
87     }
88 
89     ret = hal_wdg_finalize(&wdg_dev);
90     if (ret != 0) {
91         printf("hal_wdg_finalize error !\n");
92     }
93 
94     if (success_flag == 0) {
95         printf("wdg test result: failed !\n");
96     } else {
97         printf("wdg test result: succeed !\n");
98     }
99 
100     clear_wdg_flag();
101 
102     printf("*********** wdg test end ! ***********\n");
103 }
104 
wdg_timeout_test(void)105 void wdg_timeout_test(void)
106 {
107     write_wdg_flag(0xFFF0);
108     hal_wdg_reload(&wdg_dev);
109 
110     /* delay timeout to make cpu restart */
111     aos_msleep(WGD_TIMEOUT_MS * (1 + WGD_TIMEOUT_TOLERANCE_RANGE));
112 
113     /* this code will not be executed because wdg timeout and cpu restart */
114     write_wdg_flag(0xFF00);
115 }
116 
wdg_normal_test(void)117 void wdg_normal_test(void)
118 {
119     write_wdg_flag(0xF000);
120     hal_wdg_reload(&wdg_dev);
121 
122     /* delay no timeout */
123     aos_msleep(WGD_TIMEOUT_MS * (1 - WGD_TIMEOUT_TOLERANCE_RANGE));
124 
125     /* this code must be executed because sleep time is shorter than timeout */
126     write_wdg_flag(0x0000);
127 }
128 
read_wdg_flag(void)129 uint16_t read_wdg_flag(void)
130 {
131     int      ret    = -1;
132     uint16_t value  = 0;
133     uint32_t offset = 0;
134 
135     offset = 0;
136     ret = hal_flash_read(PARTITION_WDG_TEST, &offset, &value, sizeof(uint16_t));
137     if (ret != 0) {
138         printf("hal_flash_read error ! test failed\n");
139         return 0;
140     }
141 
142     return value;
143 }
144 
write_wdg_flag(uint16_t value)145 void write_wdg_flag(uint16_t value)
146 {
147     int      ret    = -1;
148     uint32_t offset = 0;
149 
150     offset = 0;
151     ret = hal_flash_write(PARTITION_WDG_TEST, &offset, &value, sizeof(uint16_t));
152     if (ret != 0) {
153         printf("hal_flash_write error ! test failed\n");
154     }
155 }
156 
clear_wdg_flag(void)157 void clear_wdg_flag(void)
158 {
159     int ret = -1;
160 
161     ret = hal_flash_erase(PARTITION_WDG_TEST, 0, partition_info->partition_length);
162     if (ret != 0) {
163         printf("hal_flash_erase error ! test failed\n");
164     }
165 }
166