c9b46093fc99f639c5b476040c96416bd47a789d
[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
23 extern int gpio_test();
24 extern int i2c_test();
25 extern int adc_test();
26
27 int gpio_test(void)
28 {
29         int num;
30         int cnt = 0;
31         peripheral_gpio_h handle = NULL;
32
33         printf("artik5 : 135 \n");
34         printf("artik10 : 22 \n");
35         printf(">> PIN NUMBER : ");
36
37         if (scanf("%d", &num) < 0)
38                 return 0;
39         printf("num %d\n", num);
40
41         if (peripheral_gpio_open(num, &handle) != PERIPHERAL_ERROR_NONE) {
42                 printf("handle is null\n");
43                 return 0;
44         }
45
46         if (peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT) != PERIPHERAL_ERROR_NONE) {
47                 printf("set direction error!!!");
48                 goto error;
49         }
50
51         while (cnt++ < 5) {
52                 printf("write~\n");
53                 peripheral_gpio_write(handle, 1);
54                 sleep(1);
55                 peripheral_gpio_write(handle, 0);
56                 sleep(1);
57         }
58         printf("write finish\n");
59         peripheral_gpio_close(handle);
60         return 1;
61
62 error:
63         peripheral_gpio_close(handle);
64         return 0;
65 }
66
67
68 /* Address of GY30 light sensor */
69 #define GY30_ADDR 0x23
70
71 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
72 #define GY30_CONT_HIGH_RES_MODE 0x10
73
74 #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
75
76 int i2c_test(void)
77 {
78         int cnt = 0;
79         int bus_num;
80         unsigned char buf[10];
81         peripheral_i2c_h i2c;
82
83         printf(">> I2C bus number : ");
84         if (scanf("%d", &bus_num) < 0)
85                 return 0;
86
87         if ((peripheral_i2c_init(bus_num, &i2c)) != 0) {
88                 printf("Failed to initialize I2C device\n");
89                 return 0;
90         }
91
92         if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) {
93                 printf("Failed to set address\n");
94                 goto error;
95         }
96
97         buf[0] = GY30_CONT_HIGH_RES_MODE;
98         if (peripheral_i2c_write(i2c, buf, 1) != 0) {
99                 printf("Failed to write\n");
100                 goto error;
101         }
102
103         while (cnt++ < 15) {
104                 int result;
105                 sleep(1);
106                 peripheral_i2c_read(i2c, buf, 2);
107                 result = GY30_READ_INTENSITY(buf);
108                 printf("Result [%d]\n", result);
109         }
110
111         peripheral_i2c_stop(i2c);
112         return 1;
113
114 error:
115         peripheral_i2c_stop(i2c);
116         return 0;
117 }
118
119 int adc_test(void)
120 {
121 #if 0
122         int channel = 0;
123         int data = 0;
124         adc_context_h dev = NULL;
125
126         printf(">>channel :");
127         scanf("%d", &channel);
128
129         dev = peripheral_adc_open(channel);
130
131         if (!dev) {
132                 printf("open error!\n");
133                 return 1;
134         }
135
136         peripheral_adc_read(dev, &data);
137
138         peripheral_adc_close(dev);
139 #endif
140         return 1;
141 }
142
143 int pwm_test_led(void)
144 {
145         int device = 0, channel = 0;
146         int period = 1 * 1000;
147         int duty_cycle = 1 * 1000 / 100;
148         int cnt = 0;
149
150         int set_duty_cycle;
151         int get_period, get_duty_cycle;
152         peripheral_pwm_context_h dev;
153
154         printf("<<< pwm_test >>>\n");
155
156         dev = peripheral_pwm_open(device, channel);
157         peripheral_pwm_set_period(dev, period); /* period: nanosecond */
158         peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */
159         peripheral_pwm_set_enabled(dev, 1);     /* 0: disable, 1: enable */
160
161         while (cnt < 5) {
162                 for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) {
163                         /* set duty cycle */
164                         peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
165                         peripheral_pwm_get_period(dev, &get_period);
166                         peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
167                         printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
168                         usleep(500000);
169                 }
170                 for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) {
171                         /* set duty cycle */
172                         peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
173                         peripheral_pwm_get_period(dev, &get_period);
174                         peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
175                         printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
176                         usleep(500000);
177                 }
178                 cnt++;
179         }
180         peripheral_pwm_set_enabled(dev, 0);     /* 0: disable, 1: enable */
181         peripheral_pwm_close(dev);
182
183         return 0;
184 }
185
186 int pwm_test_motor(void)
187 {
188         int device = 0, channel = 0;
189         int period = 20000000;
190         int duty_cycle = 1500000;
191         int cnt = 0, idx = 0;
192         int degree[3] = {0, 45, 90};
193         peripheral_pwm_context_h dev;
194
195         printf("<<< pwm_test_motor >>>\n");
196
197         dev = peripheral_pwm_open(device, channel);
198         for (cnt = 0; cnt < 5; cnt++) {
199                 for (idx = 0; idx < 3; idx++) {
200                         switch (degree[idx]) {
201                         case 0:
202                                 duty_cycle = 1000000;
203                                 break;
204                         case 45:
205                                 duty_cycle = 1500000;
206                                 break;
207                         case 90:
208                                 duty_cycle = 2000000;
209                                 break;
210                         default:
211                                 duty_cycle = 2000000;
212                                 break;
213                         }
214                         printf("set degree: %d\n", degree[idx]);
215                         peripheral_pwm_set_period(dev, period);
216                         peripheral_pwm_set_duty_cycle(dev, duty_cycle);
217                         peripheral_pwm_set_enabled(dev, 1);             /* 0: disable, 1: enable */
218                         usleep(500000);
219                 }
220         }
221
222         peripheral_pwm_set_enabled(dev, 0);     /* 0: disable, 1: enable */
223         peripheral_pwm_close(dev);
224
225         return 0;
226 }
227
228 int main(int argc, char **argv)
229 {
230         int num = 1;
231         int ret;
232
233         printf("===================\n");
234         printf("  test Menu\n");
235         printf("===================\n");
236         printf(" 1. GPIO Test\n");
237         printf(" 2. I2C Test\n");
238         printf(" 3. pwm led test\n");
239         printf(" 4. pwm motor test\n");
240
241         printf(" 11. H/W IF GPIO Test\n");
242         printf(" 12. H/W IF I2C Test\n");
243         printf(" 13. H/W IF PWM Test\n");
244         printf(" 14. H/W IF SPI Test\n");
245
246         if (scanf("%d", &num) < 0)
247                 return 0;
248
249         switch (num) {
250         case 1:
251                 ret = gpio_test();
252                 break;
253         case 2:
254                 ret = i2c_test();
255                 break;
256         case 3:
257                 ret = pwm_test_led();
258                 break;
259         case 4:
260                 ret = pwm_test_motor();
261                 break;
262         case 11:
263                 ret = gpio_test();
264                 break;
265         case 12:
266                 ret = i2c_test();
267                 break;
268         case 14:
269                 ret = adc_test();
270                 break;
271         default:
272                 printf("Not support \n");
273         }
274         printf(" return : %d\n", ret);
275
276         return 1;
277 }