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