Improve I2C test by using new APIs
[platform/core/api/peripheral-io.git] / test / peripheral-io-test.c
1 /*
2  * Copyright (c) 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 <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <gio/gio.h>
23
24 #include "peripheral_io.h"
25
26 #define BUFFER_LEN 32
27
28 typedef struct {
29         const char *tc_name;
30         int tc_code;
31         int (*tc_func)(void);
32 } tc_table_t;
33
34 tc_table_t *tc_table;
35
36 GMainLoop *main_loop;
37 GList *gpio_list;
38 GList *i2c_list;
39 GList *pwm_list;
40 GList *adc_list;
41 GList *uart_list;
42 GList *spi_list;
43
44 int read_int_input(int *input)
45 {
46         char buf[BUFFER_LEN];
47         int rv;
48
49         rv = read(0, buf, BUFFER_LEN);
50
51         /* Ignore Enter without value */
52         if (*buf == '\n' || *buf == '\r') {
53                 printf("No input value\n");
54                 return -1;
55         }
56
57         if (rv < 0) return -1;
58         *input = atoi(buf);
59
60         return 0;
61 }
62
63 int gpio_led_test(void)
64 {
65         int num, ret;
66         int cnt = 0;
67         peripheral_gpio_h handle = NULL;
68
69         printf("    %s()\n", __func__);
70         printf("Enter GPIO pin number ");
71
72         if (scanf("%d", &num) < 0)
73                 return -1;
74         printf("num %d\n", num);
75
76         if ((ret = peripheral_gpio_open(num, &handle)) < PERIPHERAL_ERROR_NONE) {
77                 printf("Failed to open\n");
78                 return ret;
79         }
80
81         if ((ret = peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT)) < PERIPHERAL_ERROR_NONE) {
82                 printf("Failed to set direction!!\n");
83                 goto error;
84         }
85
86         while (cnt++ < 5) {
87                 printf("Writing..\n");
88                 peripheral_gpio_write(handle, 1);
89                 sleep(1);
90                 peripheral_gpio_write(handle, 0);
91                 sleep(1);
92         }
93         printf("Write finish\n");
94         if ((ret = peripheral_gpio_close(handle)) < PERIPHERAL_ERROR_NONE) {
95                 printf("Failed to close the pin\n");
96                 return ret;
97         }
98
99         return 0;
100
101 error:
102         peripheral_gpio_close(handle);
103         return ret;
104 }
105
106 void gpio_irq_test_isr(void *user_data)
107 {
108         int pin;
109         peripheral_gpio_h gpio = user_data;
110
111         peripheral_gpio_get_pin(gpio, &pin);
112
113         printf("%s: GPIO %d interrupt occurs.\n", __func__, pin);
114 }
115
116 int gpio_irq_register(void)
117 {
118         peripheral_gpio_h gpio = NULL;
119         int pin, ret;
120
121         printf("    %s()\n", __func__);
122         printf("Enter gpio pin number\n");
123
124         if (read_int_input(&pin) < 0)
125                 return -1;
126
127         if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) {
128                 printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret);
129                 return -1;
130         }
131         gpio_list = g_list_append(gpio_list, gpio);
132
133         if ((ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN)) < 0) {
134                 printf(">>>>> Failed to set direction, ret : %d\n", ret);
135                 goto error;
136         }
137         peripheral_gpio_set_edge_mode(gpio, PERIPHERAL_GPIO_EDGE_BOTH);
138         peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio);
139
140         return 0;
141
142 error:
143         gpio_list = g_list_remove(gpio_list, gpio);
144         peripheral_gpio_close(gpio);
145         return -1;
146 }
147
148 int gpio_irq_unregister(void)
149 {
150         peripheral_gpio_h gpio;
151         int pin, ret;
152         int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio);
153
154         printf("    %s()\n", __func__);
155         printf("Enter gpio pin number\n");
156
157         if (read_int_input(&pin) < 0)
158                 return -1;
159
160         if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
161                 printf(">>>>> cannot find handle. please open the gpio pin.\n");
162                 return -1;
163         }
164
165         if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) {
166                 printf(">>>>> failed to unregister callback function, ret : %d\n", ret);
167                 return -1;
168         }
169
170         gpio_list = g_list_remove(gpio_list, gpio);
171         peripheral_gpio_close(gpio);
172
173         return 0;
174 }
175
176 /* Address of GY30 light sensor */
177 #define GY30_ADDR 0x23
178
179 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
180 #define GY30_CONT_HIGH_RES_MODE 0x10
181
182 #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
183
184 int i2c_gy30_test(void)
185 {
186         int cnt = 0;
187         unsigned char buf[10];
188         peripheral_i2c_h i2c;
189         struct timeval tv_1, tv_2;
190         int bus_num, ret, result, interval;
191
192         printf("    %s()\n", __func__);
193         printf("Enter I2C bus number\n");
194
195         if (read_int_input(&bus_num) < 0)
196                 return -1;
197
198         if ((ret = peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) < 0) {
199                 printf("Failed to open I2C communication, ret : %d\n", ret);
200                 return -1;
201         }
202
203         if ((ret = peripheral_i2c_write_byte(i2c, GY30_CONT_HIGH_RES_MODE)) < 0) {
204                 printf("Failed to write, ret : %d\n", ret);
205                 goto error;
206         }
207
208         gettimeofday(&tv_1, NULL);
209         while (cnt++ < 1000) {
210                 ret = peripheral_i2c_read_byte(i2c, buf);
211                 if (ret < 0)
212                         printf("Failed to read, ret : %d\n", ret);
213                 result = GY30_READ_INTENSITY(buf);
214                 printf("Light intensity : %d\n", result);
215         }
216         gettimeofday(&tv_2, NULL);
217         interval = (tv_2.tv_sec - tv_1.tv_sec) * 1000 + (int)(tv_2.tv_usec - tv_1.tv_usec)/1000;
218         printf("1000 i2c read calls took %d ms\n", interval);
219
220         peripheral_i2c_close(i2c);
221         return 0;
222
223 error:
224         peripheral_i2c_close(i2c);
225         return -1;
226 }
227
228 #define MMA7455_ADDRESS 0x1D //I2C Address for the sensor
229
230 #define MMA7455_MCTL 0x16 // Mode Control
231
232 #define MMA7455_MCTL_STANDBY_MODE 0x0
233 #define MMA7455_MCTL_MEASUREMENT_MODE 0x01
234 #define MMA7455_MCTL_LEVEL_DETECTION_MODE 0x02
235 #define MMA7455_MCTL_PULSE_DETECTION_MODE 0x03
236 #define MMA7455_MCTL_2G 0x04 //Set Sensitivity to 2g
237 #define MMA7455_MCTL_4G 0x08 //Set Sensitivity to 4g
238 #define MMA7455_MCTL_8G 0x00 //Set Sensitivity to 8g
239
240 #define MMA7455_INTRST 0x17
241 #define MMA7455_INTRST_CLRINT 0x03
242 #define MMA7445_INTRST_DONOTCLR 0x00
243
244 #define MMA7455_CONTROL1 0x18
245 #define MMA7455_CONTROL1_INTREG 0x06
246 #define MMA7455_CONTROL1_DFBW 0x80
247
248 #define MMA7455_XOUT8 0x06 //Register for reading the X-Axis
249 #define MMA7455_YOUT8 0x07 //Register for reading the Y-Axis
250 #define MMA7455_ZOUT8 0x08 //Register for reading the Z-Axis
251
252 static void i2c_mma7455_isr(void *user_data)
253 {
254         peripheral_i2c_h i2c = user_data;
255         uint8_t x_pos, y_pos, z_pos;
256
257         peripheral_i2c_read_register_byte(i2c, MMA7455_XOUT8, &x_pos);
258         peripheral_i2c_read_register_byte(i2c, MMA7455_YOUT8, &y_pos);
259         peripheral_i2c_read_register_byte(i2c, MMA7455_ZOUT8, &z_pos);
260
261         printf("Result X : %d, Y : %d, Z : %d\n", x_pos, y_pos, z_pos);
262
263         /* Reset interrupt flags */
264         peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7455_INTRST_CLRINT);
265         peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7445_INTRST_DONOTCLR);
266
267         return;
268 }
269
270 int i2c_mma7455_test(void)
271 {
272         static peripheral_i2c_h i2c;
273         static peripheral_gpio_h isr_gpio;
274         int bus_num, gpio_num, ret;
275         int cnt = 0;
276
277         printf("    %s()\n", __func__);
278         if (i2c) {
279                 printf("Disabling the test\n");
280
281                 peripheral_i2c_close(i2c);
282                 i2c = NULL;
283                 printf("i2c handle is closed\n");
284
285                 if (isr_gpio) {
286                         peripheral_gpio_close(isr_gpio);
287                         isr_gpio = NULL;
288                         printf("isr_gpio handle is closed\n");
289                 }
290
291                 return 0;
292         }
293
294         printf("Enter I2C bus number\n");
295
296         if (read_int_input(&bus_num) < 0)
297                 return -1;
298
299         if ((ret = peripheral_i2c_open(bus_num, MMA7455_ADDRESS, &i2c)) < 0) {
300                 printf(">>>>> Failed to open I2C communication, ret : %d \n", ret);
301                 return -1;
302         }
303
304         ret = peripheral_i2c_write_register_byte(i2c,
305                 MMA7455_MCTL,
306                 MMA7455_MCTL_8G | MMA7455_MCTL_PULSE_DETECTION_MODE);
307         if (ret < PERIPHERAL_ERROR_NONE) {
308                 printf(">>>>> Failed to write, ret : %d\n", ret);
309                 goto error;
310         }
311
312         printf("Enter GPIO pin number for Interrupt\n");
313         if (read_int_input(&gpio_num) < 0)
314                 gpio_num = -1;
315
316         if ((gpio_num > 0) && (peripheral_gpio_open(gpio_num, &isr_gpio) == 0)) {
317                 ret = peripheral_gpio_set_direction(isr_gpio, PERIPHERAL_GPIO_DIRECTION_IN);
318                 if (ret < 0)
319                         printf(">>>> Failed to set direction of isr_gpio\n");
320
321                 ret = peripheral_gpio_set_edge_mode(isr_gpio, PERIPHERAL_GPIO_EDGE_RISING);
322                 if (ret < 0)
323                         printf(">>>> Failed to set edge mode of isr_gpio\n");
324
325                 ret = peripheral_gpio_register_cb(isr_gpio, i2c_mma7455_isr, (void*)i2c);
326                 if (ret < 0)
327                         printf(">>>> Failed to register gpio callback\n");
328
329                 /* Reset interrupt flags */
330                 peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7455_INTRST_CLRINT);
331                 peripheral_i2c_write_register_byte(i2c, MMA7455_INTRST, MMA7445_INTRST_DONOTCLR);
332
333                 printf("callback is registered on gpio pin %d\n", gpio_num);
334                 printf("i2c(bus = %d address = %d) handle is open\n", bus_num, MMA7455_ADDRESS);
335         } else {
336                 while (cnt++ < 10) {
337                         uint8_t x_pos, y_pos, z_pos;
338                         unsigned char buf[4];
339                         sleep(1);
340
341                         /* Get measurement data with different APIs */
342                         buf[0] = MMA7455_XOUT8;
343                         peripheral_i2c_write(i2c, buf, 0x1);
344                         peripheral_i2c_read(i2c, &x_pos, 0x1);
345                         buf[0] = MMA7455_YOUT8;
346                         peripheral_i2c_write(i2c, buf, 0x1);
347                         peripheral_i2c_read(i2c, &y_pos, 0x1);
348                         buf[0] = MMA7455_ZOUT8;
349                         peripheral_i2c_write(i2c, buf, 0x1);
350                         peripheral_i2c_read(i2c, &z_pos, 0x1);
351                         printf("Result X : %d, Y : %d, Z : %d  (peripheral_i2c_read)\n", x_pos, y_pos, z_pos);
352
353                         peripheral_i2c_write_byte(i2c, MMA7455_XOUT8);
354                         peripheral_i2c_read_byte(i2c, &x_pos);
355                         peripheral_i2c_write_byte(i2c, MMA7455_YOUT8);
356                         peripheral_i2c_read_byte(i2c, &y_pos);
357                         peripheral_i2c_write_byte(i2c, MMA7455_ZOUT8);
358                         peripheral_i2c_read_byte(i2c, &z_pos);
359                         printf("Result X : %d, Y : %d, Z : %d (peripheral_i2c_read_byte)\n", x_pos, y_pos, z_pos);
360
361                         peripheral_i2c_read_register_byte(i2c, MMA7455_XOUT8, &x_pos);
362                         peripheral_i2c_read_register_byte(i2c, MMA7455_YOUT8, &y_pos);
363                         peripheral_i2c_read_register_byte(i2c, MMA7455_ZOUT8, &z_pos);
364                         printf("Result X : %d, Y : %d, Z : %d  (peripheral_i2c_read_register_byte)\n", x_pos, y_pos, z_pos);
365
366                 }
367                 peripheral_i2c_close(i2c);
368                 i2c = NULL;
369                 printf("i2c(bus = %d address = %d) handle is closed\n", bus_num, MMA7455_ADDRESS);
370         }
371         return 0;
372
373 error:
374         peripheral_i2c_close(i2c);
375         i2c = NULL;
376         return -1;
377 }
378
379 int pwm_test_led(void)
380 {
381         int device = 0, channel = 0;
382         int period = 1 * 1000;
383         int duty_cycle = 1 * 1000 / 100;
384         int cnt = 0;
385
386         int set_duty_cycle;
387         int get_period, get_duty_cycle;
388         peripheral_pwm_h dev;
389
390         printf("    %s()\n", __func__);
391
392         peripheral_pwm_open(device, channel, &dev);
393         peripheral_pwm_set_period(dev, period); /* period: nanosecond */
394         peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */
395         peripheral_pwm_set_enable(dev, 1);      /* 0: disable, 1: enable */
396
397         while (cnt < 5) {
398                 for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) {
399                         /* set duty cycle */
400                         peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
401                         peripheral_pwm_get_period(dev, &get_period);
402                         peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
403                         printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
404                         usleep(500000);
405                 }
406                 for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) {
407                         /* set duty cycle */
408                         peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
409                         peripheral_pwm_get_period(dev, &get_period);
410                         peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
411                         printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
412                         usleep(500000);
413                 }
414                 cnt++;
415         }
416         peripheral_pwm_set_enable(dev, 0);      /* 0: disable, 1: enable */
417         peripheral_pwm_close(dev);
418
419         return 0;
420 }
421
422 int pwm_test_motor(void)
423 {
424         int device = 0, channel = 0;
425         int period = 20000000;
426         int duty_cycle = 1500000;
427         int cnt = 0, idx = 0;
428         int degree[3] = {0, 45, 90};
429         peripheral_pwm_h dev;
430
431         printf("    %s()\n", __func__);
432
433         peripheral_pwm_open(device, channel, &dev);
434         for (cnt = 0; cnt < 5; cnt++) {
435                 for (idx = 0; idx < 3; idx++) {
436                         switch (degree[idx]) {
437                         case 0:
438                                 duty_cycle = 1000000;
439                                 break;
440                         case 45:
441                                 duty_cycle = 1500000;
442                                 break;
443                         case 90:
444                                 duty_cycle = 2000000;
445                                 break;
446                         default:
447                                 duty_cycle = 2000000;
448                                 break;
449                         }
450                         printf("set degree: %d\n", degree[idx]);
451                         peripheral_pwm_set_period(dev, period);
452                         peripheral_pwm_set_duty_cycle(dev, duty_cycle);
453                         peripheral_pwm_set_enable(dev, 1);              /* 0: disable, 1: enable */
454                         usleep(500000);
455                 }
456         }
457
458         peripheral_pwm_set_enable(dev, 0);      /* 0: disable, 1: enable */
459         peripheral_pwm_close(dev);
460
461         return 0;
462 }
463
464
465 int uart_test_accelerometer(void)
466 {
467         peripheral_uart_h uart;
468         int ret;
469         int port;
470         int loop = 100;
471         unsigned char buf[1024];
472
473         printf("    %s()\n", __func__);
474         printf("Enter port number");
475
476         if (scanf("%d", &port) < 0)
477                 return -1;
478
479         ret = peripheral_uart_open(port, &uart);
480         if (ret < 0)
481                 goto err_open;
482
483         ret = peripheral_uart_set_baudrate(uart, PERIPHERAL_UART_BAUDRATE_4800);
484         if (ret < 0)
485                 goto out;
486
487         ret = peripheral_uart_set_mode(uart,
488                         PERIPHERAL_UART_BYTESIZE_8BIT,
489                         PERIPHERAL_UART_PARITY_NONE,
490                         PERIPHERAL_UART_STOPBITS_1BIT);
491         if (ret < 0)
492                 goto out;
493
494         ret = peripheral_uart_set_flowcontrol(uart, true, false);
495         if (ret < 0)
496                 goto out;
497
498         sleep(1);
499         ret = peripheral_uart_flush(uart);
500         if (ret < 0)
501                 goto out;
502
503         while (loop--) {
504                 ret = peripheral_uart_read(uart, buf, 13);
505                 if (ret < 0) {
506                         if (ret == PERIPHERAL_ERROR_NO_DATA)
507                                 printf("No data to read (%d)\n", ret);
508                         else
509                                 printf("Failed to read (%d)\n", ret);
510                         continue;
511                 }
512                 buf[ret] = 0;
513                 printf("%s", buf);
514                 usleep(100000);
515         }
516
517         peripheral_uart_close(uart);
518         return 0;
519
520 out:
521         peripheral_uart_close(uart);
522
523 err_open:
524         return -1;
525 }
526
527 #define MMA7455_MCTL_SPI3W 0x20 // SPI is 3 wire mode
528 #define MMA7455_MCTL_DRPD 0x40 // Data ready status is not output to INT1/DRDY PIN
529
530 #define MMA7455_SPI_REGISTER_WRITE 0x40
531
532 int spi_mma7455_module_test(void)
533 {
534         int cnt = 0;
535         int bus_num, cs_num, ret;
536         unsigned char tx_buf[10];
537         unsigned char rx_buf[10];
538         unsigned int num;
539         peripheral_spi_h spi;
540
541         printf("        %s()\n", __func__);
542         printf("Enter SPI bus number : ");
543         if (scanf("%d", &bus_num) < 0)
544                 return -1;
545
546         printf("Enter SPI cs number : ");
547         if (scanf("%d", &cs_num) < 0)
548                 return -1;
549
550         if ((ret = peripheral_spi_open(bus_num, cs_num, &spi)) < 0) {
551                 printf("Failed to open I2C communication, ret : %d\n", ret);
552                 return -1;
553         }
554         peripheral_spi_set_mode(spi, PERIPHERAL_SPI_MODE_0);
555         peripheral_spi_set_lsb_first(spi, false);
556         peripheral_spi_set_bits_per_word(spi, 8);
557         peripheral_spi_set_frequency(spi, 100000);
558
559         printf("bus : %d, cs : %d, ", bus_num, cs_num);
560         peripheral_spi_get_mode(spi, (peripheral_spi_mode_e*)&num);
561         printf("mode : %d, ", num);
562         peripheral_spi_get_lsb_first(spi, (bool*)&num);
563         printf("lsb first : %d, ", (bool)num);
564         peripheral_spi_get_bits_per_word(spi, (unsigned char*)&num);
565         printf("bits : %d, ", (unsigned char)num);
566         peripheral_spi_get_frequency(spi, &num);
567         printf("max frequency : %d\n", num);
568
569         tx_buf[0] = (MMA7455_MCTL | MMA7455_SPI_REGISTER_WRITE) << 1;
570         tx_buf[1] = MMA7455_MCTL_DRPD | MMA7455_MCTL_SPI3W | MMA7455_MCTL_2G | MMA7455_MCTL_MEASUREMENT_MODE;
571         if ((ret = peripheral_spi_write(spi, tx_buf, 2)) < 0) {
572                 printf("Failed to write, ret : %d\n", ret);
573                 goto error;
574         }
575
576         while (cnt++ < 15) {
577                 int i;
578                 unsigned char buf[5];
579
580                 sleep(1);
581                 for (i = 0; i < 3; i++) {
582                         tx_buf[0] = (MMA7455_XOUT8 + i) << 1;
583                         tx_buf[1] = 0;
584                         ret = peripheral_spi_read_write(spi, tx_buf, rx_buf, 2);
585                         if (ret < 0)
586                                 printf("Failed to read, ret : %d\n", ret);
587                         buf[i] = rx_buf[1];
588                 }
589
590                 printf("X = 0x%02X, Y = 0x%02X, Z = 0x%02X\n", buf[0], buf[1], buf[2]);
591         }
592
593         peripheral_spi_close(spi);
594         return 0;
595
596 error:
597         peripheral_spi_close(spi);
598         return -1;
599 }
600
601 int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio)
602 {
603         peripheral_gpio_h handle;
604         GList *cursor;
605         int cur_pin;
606
607         cursor = gpio_list;
608         while (cursor) {
609                 handle = (peripheral_gpio_h)cursor->data;
610                 peripheral_gpio_get_pin(handle, &cur_pin);
611                 if (pin == cur_pin)
612                         break;
613                 cursor = g_list_next(cursor);
614         }
615         if (!cursor) return -1;
616
617         *gpio = handle;
618
619         return 0;
620 }
621
622 int print_gpio_handle(void)
623 {
624         peripheral_gpio_h gpio;
625         GList *cursor;
626         peripheral_gpio_direction_e direction;
627         peripheral_gpio_edge_e edge;
628         int pin, value;
629         char *dir_str, *edge_str;
630
631         printf("--- GPIO handle info. -------------------------------------------\n");
632         printf("      No     Pin   Direction   Value   Edge mode\n");
633
634         cursor = gpio_list;
635         while (cursor) {
636                 gpio = (peripheral_gpio_h)cursor->data;
637
638                 if (peripheral_gpio_get_pin(gpio, &pin) < 0)
639                         continue;
640                 if (peripheral_gpio_get_direction(gpio, &direction) < 0)
641                         continue;
642                 if (peripheral_gpio_get_edge_mode(gpio, &edge) < 0)
643                         continue;
644
645                 if (direction == PERIPHERAL_GPIO_DIRECTION_IN)
646                         dir_str = "IN";
647                 else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_LOW)
648                         dir_str = "OUT_LOW";
649                 else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_HIGH)
650                         dir_str = "OUT_HIGH";
651                 else
652                         dir_str = "UNKNOWN";
653
654                 if (edge == PERIPHERAL_GPIO_EDGE_NONE)
655                         edge_str = "NONE";
656                 else if (edge == PERIPHERAL_GPIO_EDGE_RISING)
657                         edge_str = "RISING";
658                 else if (edge == PERIPHERAL_GPIO_EDGE_FALLING)
659                         edge_str = "FALLING";
660                 else if (edge == PERIPHERAL_GPIO_EDGE_BOTH)
661                         edge_str = "BOTH";
662                 else
663                         edge_str = "UNKNOWN";
664
665                 if (direction == PERIPHERAL_GPIO_DIRECTION_IN) {
666                         if (peripheral_gpio_read(gpio, &value) < 0)
667                                 continue;
668                         printf("%8d%8d%12s%8d%12s\n", g_list_position(gpio_list, cursor),
669                                         pin, dir_str, value, edge_str);
670                 } else
671                         printf("%8d%8d%12s%20s\n", g_list_position(gpio_list, cursor),
672                                         pin, dir_str, edge_str);
673
674                 cursor = g_list_next(cursor);
675         }
676         printf("-----------------------------------------------------------------\n");
677
678         return 0;
679 }
680
681 int gpio_test_open(void)
682 {
683         peripheral_gpio_h gpio;
684         int pin, ret;
685
686         printf("%s\n", __func__);
687         printf("Enter GPIO pin number\n");
688
689         if (read_int_input(&pin) < 0)
690                 return -1;
691
692         if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) {
693                 printf(">>>>> GPIO open failed, ret : %d\n", ret);
694                 return -1;
695         }
696         gpio_list = g_list_append(gpio_list, gpio);
697         print_gpio_handle();
698
699         return 0;
700 }
701
702 int gpio_test_close(void)
703 {
704         peripheral_gpio_h gpio;
705         int pin, ret;
706
707         printf("%s\n", __func__);
708         printf("Enter GPIO pin number\n");
709
710         if (read_int_input(&pin) < 0)
711                 return -1;
712
713         if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
714                 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
715                 return -1;
716         }
717         gpio_list = g_list_remove(gpio_list, gpio);
718
719         if ((ret = peripheral_gpio_close(gpio)) < 0) {
720                 printf(">>>>> GPIO close failed, ret : %d\n", ret);
721                 return -1;
722         }
723         print_gpio_handle();
724
725         return 0;
726 }
727
728 int gpio_test_set_direction(void)
729 {
730         peripheral_gpio_h gpio;
731         int pin, ret;
732         int direction;
733
734         printf("%s\n", __func__);
735         printf("Enter gpio pin number\n");
736
737         if (read_int_input(&pin) < 0)
738                 return -1;
739
740         if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
741                 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
742                 return -1;
743         }
744
745         printf("Enter direction (0:IN, 1:OUT_LOW, 2:OUT_HIGH)\n");
746         if (read_int_input(&direction) < 0)
747                 return -1;
748
749         if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH ||
750                 direction < PERIPHERAL_GPIO_DIRECTION_IN) {
751                 printf(">>>>> Wrong input value\n");
752                 return -1;
753         }
754
755         if ((ret = peripheral_gpio_set_direction(gpio, (peripheral_gpio_direction_e)direction)) < 0) {
756                 printf(">>>>> Failed to set direction, ret : %d\n", ret);
757                 return -1;
758         }
759
760         return 0;
761 }
762
763 int gpio_test_write(void)
764 {
765         peripheral_gpio_h gpio;
766         int pin, ret;
767         int value;
768
769         printf("%s\n", __func__);
770         printf("Enter gpio pin number\n");
771
772         if (read_int_input(&pin) < 0)
773                 return -1;
774
775         if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
776                 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
777                 return -1;
778         }
779
780         printf("Enter value (0:LOW, 1:HIGH)\n");
781         if (read_int_input(&value) < 0)
782                 return -1;
783
784         if (value < 0 || value > 1) {
785                 printf(">>>>> Wrong input value\n");
786                 return -1;
787         }
788
789         if ((ret = peripheral_gpio_write(gpio, value)) < 0) {
790                 printf(">>>>> Failed to write value, ret : %d\n", ret);
791                 return -1;
792         }
793
794         return 0;
795 }
796
797 int gpio_test_set_edge_mode(void)
798 {
799         peripheral_gpio_h gpio;
800         int pin, ret;
801         int edge;
802
803         printf("%s\n", __func__);
804         printf("Enter gpio pin number\n");
805
806         if (read_int_input(&pin) < 0)
807                 return -1;
808
809         if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
810                 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
811                 return -1;
812         }
813
814         printf("Enter edge mode (0:NONE, 1:RISING, 2:FALLING, 3:BOTH)\n");
815         if (read_int_input(&edge) < 0)
816                 return -1;
817
818         if (edge < PERIPHERAL_GPIO_EDGE_NONE || edge > PERIPHERAL_GPIO_EDGE_BOTH) {
819                 printf(">>>>> Wrong input value\n");
820                 return -1;
821         }
822
823         if ((ret = peripheral_gpio_set_edge_mode(gpio, (peripheral_gpio_edge_e)edge)) < 0) {
824                 printf(">>>>> Failed to set edge mode, ret : %d\n", ret);
825                 return -1;
826         }
827
828         return 0;
829 }
830
831 int gpio_test_set_register_cb(void)
832 {
833         peripheral_gpio_h gpio;
834         int pin, ret;
835
836         printf("%s\n", __func__);
837         printf("Enter gpio pin number\n");
838
839         if (read_int_input(&pin) < 0)
840                 return -1;
841
842         if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
843                 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
844                 return -1;
845         }
846
847         if ((ret = peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio)) < 0) {
848                 printf(">>>>> Failed to register callback function, ret : %d\n", ret);
849                 return -1;
850         }
851         return 0;
852 }
853
854 int gpio_test_set_unregister_cb(void)
855 {
856         peripheral_gpio_h gpio;
857         int pin, ret;
858
859         printf("%s\n", __func__);
860         printf("Enter gpio pin number\n");
861
862         if (read_int_input(&pin) < 0)
863                 return -1;
864
865         if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
866                 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
867                 return -1;
868         }
869
870         if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) {
871                 printf(">>>>> failed to unregister callback function, ret : %d\n", ret);
872                 return -1;
873         }
874
875         return 0;
876 }
877
878 int enter_main(void);
879
880 tc_table_t gpio_tc_table[] = {
881         {"Open GPIO pin",                               1, gpio_test_open},
882         {"Close GPIO pin",                              2, gpio_test_close},
883         {"Set direction GPIO pin",              3, gpio_test_set_direction},
884         {"Write value to GPIO pin",             4, gpio_test_write},
885         {"Set edge mode",                               5, gpio_test_set_edge_mode},
886         {"Register callback",                   6, gpio_test_set_register_cb},
887         {"Unregister callback",                 7, gpio_test_set_unregister_cb},
888         {"Print GPIO handle",                   9, print_gpio_handle},
889         {"Go back to main",                             0, enter_main},
890         {NULL,  0, NULL},
891 };
892
893 int enter_gpio_test(void)
894 {
895         tc_table = gpio_tc_table;
896         print_gpio_handle();
897
898         return 0;
899 }
900
901 int enter_i2c_test(void)
902 {
903         return 0;
904 }
905
906 int enter_pwm_test(void)
907 {
908         return 0;
909 }
910
911 int enter_adc_test(void)
912 {
913         return 0;
914 }
915
916 int enter_uart_test(void)
917 {
918         return 0;
919 }
920
921 int enter_spi_test(void)
922 {
923         return 0;
924 }
925
926 tc_table_t preset_tc_table[] = {
927         {"[Preset Test] GPIO LED",                                      1, gpio_led_test},
928         {"[Preset Test] I2C GY30 Light sensor",         2, i2c_gy30_test},
929         {"[Preset Test] I2C MMA7455 Accel. sensor",     3, i2c_mma7455_test},
930         {"[Preset Test] PWM LED",                                       4, pwm_test_led},
931         {"[Preset Test] PWM Motor",                                     5, pwm_test_motor},
932         {"[Preset Test] Uart Accelerometer",            6, uart_test_accelerometer},
933         {"[Preset Test] SPI MMA7455 Accel. sensor",     7, spi_mma7455_module_test},
934         {"[Preset Test] GPIO IRQ register",                     8, gpio_irq_register},
935         {"[Preset Test] GPIO IRQ unregister",           9, gpio_irq_unregister},
936         {"Go back to main",                                                     0, enter_main},
937         {NULL,  0, NULL},
938 };
939
940 int enter_preset_test(void)
941 {
942         tc_table = preset_tc_table;
943         return 0;
944 }
945
946 int terminate_test(void)
947 {
948         int ret = 0;
949
950         printf("Terminate test\n");
951         g_main_loop_quit(main_loop);
952
953         exit(1);
954
955         return ret;
956 }
957
958 tc_table_t main_tc_table[] = {
959         {"GPIO Test Menu",                                                      1, enter_gpio_test},
960         {"I2C Test Menu",                                                       2, enter_i2c_test},
961         {"PWM Test Menu",                                                       3, enter_pwm_test},
962         {"ADC Test Menu",                                                       4, enter_adc_test},
963         {"UART Test Menu",                                                      5, enter_uart_test},
964         {"SPI Test Menu",                                                       6, enter_spi_test},
965         {"Preset Test",                                                         10, enter_preset_test},
966         {"Exit Test",                                                           0, terminate_test},
967         {NULL,  0, NULL},
968 };
969
970 int enter_main(void)
971 {
972         tc_table = main_tc_table;
973
974         return 0;
975 }
976
977 static int test_input_callback(void *data)
978 {
979         tc_table_t *tc;
980         long test_id = (long)data;
981         int ret = PERIPHERAL_ERROR_NONE;
982         int i = 0;
983
984         tc = tc_table;
985
986         while (tc[i].tc_name) {
987                 if (tc[i].tc_code == test_id && tc[i].tc_func) {
988                         ret = tc[i].tc_func();
989                         if (ret != PERIPHERAL_ERROR_NONE)
990                                 printf(">>>>> Test Error Returned !!! : %d\n", ret);
991
992                         break;
993                 }
994                 i++;
995         }
996         if (!tc[i].tc_name) {
997                 printf(">>>>> Wrong input value!\n");
998                 return -1;
999         }
1000
1001         return 0;
1002 }
1003
1004 static void print_tc_usage(void)
1005 {
1006         int i = 0;
1007
1008         printf("===========================================\n");
1009         while (tc_table[i].tc_name) {
1010                 printf("    %2d : %s\n", tc_table[i].tc_code, tc_table[i].tc_name);
1011                 i++;
1012         }
1013         printf("===========================================\n");
1014         printf("Enter TC number\n");
1015 }
1016
1017 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
1018 {
1019         char buf[BUFFER_LEN] = {0,};
1020         long test_id;
1021         int rv = 0;
1022
1023         memset(buf, 0, sizeof(buf));
1024
1025         rv = read(0, buf, BUFFER_LEN);
1026
1027         if (*buf == '\n' || *buf == '\r') {
1028                 print_tc_usage();
1029                 return TRUE;
1030         }
1031
1032         if (rv < 0) return FALSE;
1033
1034         test_id = atoi(buf);
1035
1036         test_input_callback((void *)test_id);
1037         print_tc_usage();
1038
1039         return TRUE;
1040 }
1041
1042 int main(int argc, char **argv)
1043 {
1044         GIOChannel *key_io;
1045
1046         main_loop = g_main_loop_new(NULL, FALSE);
1047         key_io = g_io_channel_unix_new(0);
1048
1049         tc_table = main_tc_table;
1050
1051         print_tc_usage();
1052         g_io_add_watch(key_io, (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), key_event_cb, NULL);
1053
1054         g_main_loop_run(main_loop);
1055
1056         g_io_channel_unref(key_io);
1057         g_main_loop_unref(main_loop);
1058
1059         return 0;
1060 }