21d0a8b6b49c0e9944cc44e7a6841464e1905126
[platform/core/api/peripheral-io.git] / test / peripheral-io-test.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "peripheral_io.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <gio/gio.h>
23
24 extern int gpio_test();
25 extern int i2c_test();
26 extern int adc_test();
27
28 GMainLoop *loop;
29
30 int gpio_test(void)
31 {
32         int num;
33         int cnt = 0;
34         peripheral_gpio_h handle = NULL;
35
36         printf("artik5 : 135 \n");
37         printf("artik10 : 22 \n");
38         printf(">> PIN NUMBER : ");
39
40         if (scanf("%d", &num) < 0)
41                 return 0;
42         printf("num %d\n", num);
43
44         if (peripheral_gpio_open(num, &handle) != PERIPHERAL_ERROR_NONE) {
45                 printf("handle is null\n");
46                 return 0;
47         }
48
49         if (peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT) != PERIPHERAL_ERROR_NONE) {
50                 printf("set direction error!!!");
51                 goto error;
52         }
53
54         while (cnt++ < 5) {
55                 printf("write~\n");
56                 peripheral_gpio_write(handle, 1);
57                 sleep(1);
58                 peripheral_gpio_write(handle, 0);
59                 sleep(1);
60         }
61         printf("write finish\n");
62         peripheral_gpio_close(handle);
63         return 1;
64
65 error:
66         peripheral_gpio_close(handle);
67         return 0;
68 }
69
70 void gpio_irq_test_isr(void *user_data)
71 {
72         int pin;
73         peripheral_gpio_h gpio = user_data;
74
75         peripheral_gpio_get_pin(gpio, &pin);
76
77         printf("gpio_irq_test_isr: GPIO %d interrupt occurs.\n", pin);
78 }
79
80 void *gpio_irq_test_thread(void *data)
81 {
82         peripheral_gpio_h gpio = data;
83         int num;
84
85         printf(">> Press any key to exit GPIO IRQ Test : \n");
86         if (scanf("%d", &num) < 0)
87                 return 0;
88
89         peripheral_gpio_unregister_cb(gpio);
90         peripheral_gpio_close(gpio);
91
92         g_main_loop_quit(loop);
93         return 0;
94 }
95
96 int gpio_irq_test(void)
97 {
98         GThread *test_thread;
99         int num;
100         peripheral_gpio_h gpio = NULL;
101         peripheral_gpio_edge_e edge = PERIPHERAL_GPIO_EDGE_NONE;
102
103         printf("artik710 : 27 \n");
104         printf(">> PIN NUMBER : ");
105
106         if (scanf("%d", &num) < 0)
107                 return 0;
108
109         if (peripheral_gpio_open(num, &gpio) != PERIPHERAL_ERROR_NONE) {
110                 printf("test dev is null\n");
111                 return 0;
112         }
113
114         if (peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN) != 0) {
115                 printf("test set direction error!!!");
116                 goto error;
117         }
118
119         printf(">> Select Edge Mode (0 = None, 1 = Falling, 2 = Rising, 3 = Both) : ");
120         if (scanf("%d", &num) < 0)
121                 return 0;
122
123         if (num >= 0 && num <= 3)
124                 edge = num;
125
126         peripheral_gpio_set_edge_mode( gpio, edge);
127         peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio);
128
129         test_thread = g_thread_new("key input thread", &gpio_irq_test_thread, gpio);
130         loop = g_main_loop_new(NULL, FALSE);
131         g_main_loop_run(loop);
132
133         g_thread_join(test_thread);
134         if (loop != NULL)
135                 g_main_loop_unref(loop);
136
137         return 0;
138
139 error:
140         peripheral_gpio_close(gpio);
141         return 0;
142 }
143
144 /* Address of GY30 light sensor */
145 #define GY30_ADDR 0x23
146
147 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
148 #define GY30_CONT_HIGH_RES_MODE 0x10
149
150 #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
151
152 int i2c_test(void)
153 {
154         int cnt = 0;
155         int bus_num;
156         unsigned char buf[10];
157         peripheral_i2c_h i2c;
158
159         printf(">> I2C bus number : ");
160         if (scanf("%d", &bus_num) < 0)
161                 return 0;
162
163         if ((peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) != 0) {
164                 printf("Failed to open I2C communication\n");
165                 return 0;
166         }
167
168         buf[0] = GY30_CONT_HIGH_RES_MODE;
169         if (peripheral_i2c_write(i2c, buf, 1) != 0) {
170                 printf("Failed to write\n");
171                 goto error;
172         }
173
174         while (cnt++ < 15) {
175                 int result;
176                 sleep(1);
177                 peripheral_i2c_read(i2c, buf, 2);
178                 result = GY30_READ_INTENSITY(buf);
179                 printf("Result [%d]\n", result);
180         }
181
182         peripheral_i2c_close(i2c);
183         return 1;
184
185 error:
186         peripheral_i2c_close(i2c);
187         return 0;
188 }
189
190 int adc_test(void)
191 {
192 #if 0
193         int channel = 0;
194         int data = 0;
195         adc_context_h dev = NULL;
196
197         printf(">>channel :");
198         scanf("%d", &channel);
199
200         dev = peripheral_adc_open(channel);
201
202         if (!dev) {
203                 printf("open error!\n");
204                 return 1;
205         }
206
207         peripheral_adc_read(dev, &data);
208
209         peripheral_adc_close(dev);
210 #endif
211         return 1;
212 }
213
214 int pwm_test_led(void)
215 {
216         int device = 0, channel = 0;
217         int period = 1 * 1000;
218         int duty_cycle = 1 * 1000 / 100;
219         int cnt = 0;
220
221         int set_duty_cycle;
222         int get_period, get_duty_cycle;
223         peripheral_pwm_h dev;
224
225         printf("<<< pwm_test >>>\n");
226
227         dev = peripheral_pwm_open(device, channel);
228         peripheral_pwm_set_period(dev, period); /* period: nanosecond */
229         peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */
230         peripheral_pwm_set_enabled(dev, 1);     /* 0: disable, 1: enable */
231
232         while (cnt < 5) {
233                 for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) {
234                         /* set duty cycle */
235                         peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
236                         peripheral_pwm_get_period(dev, &get_period);
237                         peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
238                         printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
239                         usleep(500000);
240                 }
241                 for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) {
242                         /* set duty cycle */
243                         peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
244                         peripheral_pwm_get_period(dev, &get_period);
245                         peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
246                         printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
247                         usleep(500000);
248                 }
249                 cnt++;
250         }
251         peripheral_pwm_set_enabled(dev, 0);     /* 0: disable, 1: enable */
252         peripheral_pwm_close(dev);
253
254         return 0;
255 }
256
257 int pwm_test_motor(void)
258 {
259         int device = 0, channel = 0;
260         int period = 20000000;
261         int duty_cycle = 1500000;
262         int cnt = 0, idx = 0;
263         int degree[3] = {0, 45, 90};
264         peripheral_pwm_h dev;
265
266         printf("<<< pwm_test_motor >>>\n");
267
268         dev = peripheral_pwm_open(device, channel);
269         for (cnt = 0; cnt < 5; cnt++) {
270                 for (idx = 0; idx < 3; idx++) {
271                         switch (degree[idx]) {
272                         case 0:
273                                 duty_cycle = 1000000;
274                                 break;
275                         case 45:
276                                 duty_cycle = 1500000;
277                                 break;
278                         case 90:
279                                 duty_cycle = 2000000;
280                                 break;
281                         default:
282                                 duty_cycle = 2000000;
283                                 break;
284                         }
285                         printf("set degree: %d\n", degree[idx]);
286                         peripheral_pwm_set_period(dev, period);
287                         peripheral_pwm_set_duty_cycle(dev, duty_cycle);
288                         peripheral_pwm_set_enabled(dev, 1);             /* 0: disable, 1: enable */
289                         usleep(500000);
290                 }
291         }
292
293         peripheral_pwm_set_enabled(dev, 0);     /* 0: disable, 1: enable */
294         peripheral_pwm_close(dev);
295
296         return 0;
297 }
298
299 int main(int argc, char **argv)
300 {
301         int num = 1;
302         int ret;
303
304         printf("===================\n");
305         printf("  test Menu\n");
306         printf("===================\n");
307         printf(" 1. GPIO Test\n");
308         printf(" 2. I2C Test\n");
309         printf(" 3. pwm led test\n");
310         printf(" 4. pwm motor test\n");
311
312         printf(" 11. GPIO Interrupt Test\n");
313         printf(" 12. H/W IF I2C Test\n");
314         printf(" 13. H/W IF PWM Test\n");
315         printf(" 14. H/W IF SPI Test\n");
316
317         if (scanf("%d", &num) < 0)
318                 return 0;
319
320         switch (num) {
321         case 1:
322                 ret = gpio_test();
323                 break;
324         case 2:
325                 ret = i2c_test();
326                 break;
327         case 3:
328                 ret = pwm_test_led();
329                 break;
330         case 4:
331                 ret = pwm_test_motor();
332                 break;
333         case 11:
334                 ret = gpio_irq_test();
335                 break;
336         case 12:
337                 ret = i2c_test();
338                 break;
339         case 14:
340                 ret = adc_test();
341                 break;
342         default:
343                 printf("Not support \n");
344         }
345         printf(" return : %d\n", ret);
346
347         return 1;
348 }