1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #include "aos/kernel.h"
10 #include "aos/hal/gpio.h"
11 #include "gpio_test.h"
12
13 static aos_timer_t gpio_test_timer;
14 static aos_sem_t gpio_test_sem;
15 static gpio_dev_t gpio_out;
16 static gpio_dev_t gpio_in;
17
18 int gpio_irq_flag = 0;
19
20 static void gpio_test_end(void);
21 static void gpio_test_task(void *arg1 , void *arg2);
22 static void gpio_output_irq_test(void);
23 static void gpio_output_in_test(void);
24 static void gpio_in_handler(void *arg);
25
hal_gpio_test(void)26 void hal_gpio_test(void)
27 {
28 int ret = -1;
29
30 printf("*********** gpio test start ! ***********\n");
31
32 /* init gpio out in */
33 gpio_out.port = PORT_GPIO_TEST_OUT;
34 gpio_out.config = OUTPUT_PUSH_PULL;
35 gpio_in.port = PORT_GPIO_TEST_IN;
36
37 ret = hal_gpio_init(&gpio_out);
38 if (ret != 0) {
39 printf("gpio_out init error !\n");
40 }
41
42 /* create a sem and a timer to test gpio */
43 aos_sem_new(&gpio_test_sem, 0);
44 aos_timer_new(&gpio_test_timer, gpio_test_task, NULL, 100, 1);
45
46 /* wait the test end */
47 aos_sem_wait(&gpio_test_sem, AOS_WAIT_FOREVER);
48
49 hal_gpio_finalize(&gpio_in);
50 hal_gpio_finalize(&gpio_out);
51
52 aos_sem_free(&gpio_test_sem);
53 aos_timer_free(&gpio_test_timer);
54
55 printf("*********** gpio test end ! ***********\n");
56 }
57
gpio_test_end(void)58 void gpio_test_end(void)
59 {
60 aos_task_exit(0);
61 aos_sem_signal(&gpio_test_sem);
62 }
63
gpio_test_task(void * arg1,void * arg2)64 void gpio_test_task(void *arg1 , void *arg2)
65 {
66 static int gpio_test_cnt = 0;
67
68 gpio_test_cnt++;
69
70 //printf("gpio_test_cnt %d\n", gpio_test_cnt);
71
72 if ((gpio_test_cnt >= 1) && (gpio_test_cnt <= 100)) {
73 gpio_output_irq_test();
74 }
75
76 if ((gpio_test_cnt >= 101) && (gpio_test_cnt <= 200)) {
77 gpio_output_in_test();
78 }
79
80 if (gpio_test_cnt > 200) {
81 gpio_test_end();
82 }
83 }
84
gpio_output_irq_test(void)85 void gpio_output_irq_test(void)
86 {
87 static int test_cnt = 0;
88 static int test_fail_flag = 0;
89 static int arg = 0;
90
91 int ret = -1;
92
93 test_cnt++;
94
95 //printf("test_cnt %d\n", test_cnt);
96
97 if (test_cnt == 1) {
98 printf("gpio_output_irq_test begin !\n");
99
100 ret = hal_gpio_init(&gpio_out);
101 if (ret != 0) {
102 printf("gpio_out init error !\n");
103 }
104
105 hal_gpio_output_low(&gpio_out);
106
107 /* init gpio in */
108 gpio_in.config = IRQ_MODE;
109
110 ret = hal_gpio_init(&gpio_in);
111 if (ret != 0) {
112 printf("gpio_in init error !\n");
113 }
114
115 /* enable the irg mode */
116 ret = hal_gpio_enable_irq(&gpio_in, IRQ_TRIGGER_BOTH_EDGES, gpio_in_handler, &arg);
117 if (ret != 0) {
118 printf("gpio_in init irq error !\n");
119 }
120 }
121
122 hal_gpio_output_toggle(&gpio_out);
123
124 if (gpio_irq_flag == 1) {
125 gpio_irq_flag = 0;
126 } else {
127 test_fail_flag = 1;
128 }
129
130 if (test_cnt == 100) {
131 if (test_fail_flag == 1) {
132 printf("gpio_output_irq_test failed !\n");
133 } else {
134 printf("gpio_output_irq_test succeed !\n");
135 }
136 }
137 }
138
gpio_output_in_test(void)139 void gpio_output_in_test(void)
140 {
141 static int test_cnt = 0;
142 static int test_fail_flag = 0;
143
144 uint32_t value = 0;
145 int ret = -1;
146
147 test_cnt++;
148
149 if (test_cnt == 1) {
150 printf("gpio_output_in_test begin !\n");
151
152 hal_gpio_finalize(&gpio_in);
153
154 gpio_in.config = INPUT_HIGH_IMPEDANCE;
155 ret = hal_gpio_init(&gpio_in);
156 if (ret != 0) {
157 printf("gpio_in init error !\n");
158 }
159 }
160
161 hal_gpio_output_high(&gpio_out);
162 hal_gpio_input_get(&gpio_in, &value);
163
164 if (value == 1) {
165 gpio_irq_flag = 0;
166 } else {
167 test_fail_flag = 1;
168 }
169
170 hal_gpio_output_low(&gpio_out);
171 hal_gpio_input_get(&gpio_in, &value);
172
173 if (value == 0) {
174 gpio_irq_flag = 0;
175 } else {
176 test_fail_flag = 1;
177 }
178
179 hal_gpio_output_toggle(&gpio_out);
180 hal_gpio_input_get(&gpio_in, &value);
181
182 if (value == 1) {
183 gpio_irq_flag = 0;
184 } else {
185 test_fail_flag = 1;
186 }
187
188 if (test_cnt == 100) {
189 if (test_fail_flag == 1) {
190 printf("gpio_output_in_test failed !\n");
191 } else {
192 printf("gpio_output_in_test succeed !\n");
193 }
194 }
195 }
196
gpio_in_handler(void * arg)197 void gpio_in_handler(void *arg)
198 {
199 gpio_irq_flag = 1;
200 }
201