2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "peripheral_io.h"
44 int read_int_input(int *input)
49 rv = read(0, buf, BUFFER_LEN);
51 /* Ignore Enter without value */
52 if (*buf == '\n' || *buf == '\r') {
53 printf("No input value\n");
57 if (rv < 0) return -1;
63 int gpio_led_test(void)
67 peripheral_gpio_h handle = NULL;
69 printf(" %s()\n", __func__);
70 printf("Enter GPIO pin number ");
72 if (scanf("%d", &num) < 0)
74 printf("num %d\n", num);
76 if ((ret = peripheral_gpio_open(num, &handle)) < PERIPHERAL_ERROR_NONE) {
77 printf("Failed to open\n");
81 if ((ret = peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT)) < PERIPHERAL_ERROR_NONE) {
82 printf("Failed to set direction!!\n");
87 printf("Writing..\n");
88 peripheral_gpio_write(handle, 1);
90 peripheral_gpio_write(handle, 0);
93 printf("Write finish\n");
94 if ((ret = peripheral_gpio_close(handle)) < PERIPHERAL_ERROR_NONE) {
95 printf("Failed to close the pin\n");
102 peripheral_gpio_close(handle);
106 void gpio_irq_test_isr(void *user_data)
109 peripheral_gpio_h gpio = user_data;
111 peripheral_gpio_get_pin(gpio, &pin);
113 printf("%s: GPIO %d interrupt occurs.\n", __func__, pin);
116 int gpio_irq_register(void)
118 peripheral_gpio_h gpio = NULL;
121 printf(" %s()\n", __func__);
122 printf("Enter gpio pin number\n");
124 if (read_int_input(&pin) < 0)
127 if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) {
128 printf(">>>>> Failed to open GPIO pin, ret : %d\n", ret);
131 gpio_list = g_list_append(gpio_list, gpio);
133 if ((ret = peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN)) < 0) {
134 printf(">>>>> Failed to set direction, ret : %d\n", ret);
137 peripheral_gpio_set_edge_mode(gpio, PERIPHERAL_GPIO_EDGE_BOTH);
138 peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio);
143 gpio_list = g_list_remove(gpio_list, gpio);
144 peripheral_gpio_close(gpio);
148 int gpio_irq_unregister(void)
150 peripheral_gpio_h gpio;
152 int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio);
154 printf(" %s()\n", __func__);
155 printf("Enter gpio pin number\n");
157 if (read_int_input(&pin) < 0)
160 if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
161 printf(">>>>> cannot find handle. please open the gpio pin.\n");
165 if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) {
166 printf(">>>>> failed to unregister callback function, ret : %d\n", ret);
170 gpio_list = g_list_remove(gpio_list, gpio);
171 peripheral_gpio_close(gpio);
176 /* Address of GY30 light sensor */
177 #define GY30_ADDR 0x23
179 /* Start measurement at 11x resolution. Measurement time is approx 120ms. */
180 #define GY30_CONT_HIGH_RES_MODE 0x10
182 #define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
184 int i2c_gy30_test(void)
187 unsigned char buf[10];
188 peripheral_i2c_h i2c;
189 struct timeval tv_1, tv_2;
190 int bus_num, ret, result, interval;
192 printf(" %s()\n", __func__);
193 printf("Enter I2C bus number\n");
195 if (read_int_input(&bus_num) < 0)
198 if ((ret = peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) < 0) {
199 printf("Failed to open I2C communication, ret : %d\n", ret);
203 if ((ret = peripheral_i2c_write_byte(i2c, GY30_CONT_HIGH_RES_MODE)) < 0) {
204 printf("Failed to write, ret : %d\n", ret);
208 gettimeofday(&tv_1, NULL);
209 while (cnt++ < 1000) {
210 ret = peripheral_i2c_read_byte(i2c, buf);
212 printf("Failed to read, ret : %d\n", ret);
213 result = GY30_READ_INTENSITY(buf);
214 printf("Light intensity : %d\n", result);
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);
220 peripheral_i2c_close(i2c);
224 peripheral_i2c_close(i2c);
228 #define MMA7455_ADDRESS 0x1D //I2C Address for the sensor
230 #define MMA7455_MCTL 0x16 // Mode Control
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
240 #define MMA7455_INTRST 0x17
241 #define MMA7455_INTRST_CLRINT 0x03
242 #define MMA7445_INTRST_DONOTCLR 0x00
244 #define MMA7455_CONTROL1 0x18
245 #define MMA7455_CONTROL1_INTREG 0x06
246 #define MMA7455_CONTROL1_DFBW 0x80
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
252 static void i2c_mma7455_isr(void *user_data)
254 peripheral_i2c_h i2c = user_data;
255 uint8_t x_pos, y_pos, z_pos;
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);
261 printf("Result X : %d, Y : %d, Z : %d\n", x_pos, y_pos, z_pos);
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);
270 int i2c_mma7455_test(void)
272 static peripheral_i2c_h i2c;
273 static peripheral_gpio_h isr_gpio;
274 int bus_num, gpio_num, ret;
277 printf(" %s()\n", __func__);
279 printf("Disabling the test\n");
281 peripheral_i2c_close(i2c);
283 printf("i2c handle is closed\n");
286 peripheral_gpio_close(isr_gpio);
288 printf("isr_gpio handle is closed\n");
294 printf("Enter I2C bus number\n");
296 if (read_int_input(&bus_num) < 0)
299 if ((ret = peripheral_i2c_open(bus_num, MMA7455_ADDRESS, &i2c)) < 0) {
300 printf(">>>>> Failed to open I2C communication, ret : %d \n", ret);
304 ret = peripheral_i2c_write_register_byte(i2c,
306 MMA7455_MCTL_8G | MMA7455_MCTL_PULSE_DETECTION_MODE);
307 if (ret < PERIPHERAL_ERROR_NONE) {
308 printf(">>>>> Failed to write, ret : %d\n", ret);
312 printf("Enter GPIO pin number for Interrupt\n");
313 if (read_int_input(&gpio_num) < 0)
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);
319 printf(">>>> Failed to set direction of isr_gpio\n");
321 ret = peripheral_gpio_set_edge_mode(isr_gpio, PERIPHERAL_GPIO_EDGE_RISING);
323 printf(">>>> Failed to set edge mode of isr_gpio\n");
325 ret = peripheral_gpio_register_cb(isr_gpio, i2c_mma7455_isr, (void*)i2c);
327 printf(">>>> Failed to register gpio callback\n");
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);
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);
337 uint8_t x_pos, y_pos, z_pos;
338 unsigned char buf[4];
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);
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);
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);
367 peripheral_i2c_close(i2c);
369 printf("i2c(bus = %d address = %d) handle is closed\n", bus_num, MMA7455_ADDRESS);
374 peripheral_i2c_close(i2c);
379 int pwm_test_led(void)
381 int device = 0, channel = 0;
382 int period = 1 * 1000;
383 int duty_cycle = 1 * 1000 / 100;
387 int get_period, get_duty_cycle;
388 peripheral_pwm_h dev;
390 printf(" %s()\n", __func__);
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 */
398 for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) {
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);
406 for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) {
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);
416 peripheral_pwm_set_enable(dev, 0); /* 0: disable, 1: enable */
417 peripheral_pwm_close(dev);
422 int pwm_test_motor(void)
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;
431 printf(" %s()\n", __func__);
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]) {
438 duty_cycle = 1000000;
441 duty_cycle = 1500000;
444 duty_cycle = 2000000;
447 duty_cycle = 2000000;
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 */
458 peripheral_pwm_set_enable(dev, 0); /* 0: disable, 1: enable */
459 peripheral_pwm_close(dev);
465 int uart_test_accelerometer(void)
467 peripheral_uart_h uart;
471 unsigned char buf[1024];
473 printf(" %s()\n", __func__);
474 printf("Enter port number");
476 if (scanf("%d", &port) < 0)
479 ret = peripheral_uart_open(port, &uart);
483 ret = peripheral_uart_set_baudrate(uart, PERIPHERAL_UART_BAUDRATE_4800);
487 ret = peripheral_uart_set_mode(uart,
488 PERIPHERAL_UART_BYTESIZE_8BIT,
489 PERIPHERAL_UART_PARITY_NONE,
490 PERIPHERAL_UART_STOPBITS_1BIT);
494 ret = peripheral_uart_set_flowcontrol(uart, true, false);
499 ret = peripheral_uart_flush(uart);
504 ret = peripheral_uart_read(uart, buf, 13);
506 if (ret == PERIPHERAL_ERROR_NO_DATA)
507 printf("No data to read (%d)\n", ret);
509 printf("Failed to read (%d)\n", ret);
517 peripheral_uart_close(uart);
521 peripheral_uart_close(uart);
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
530 #define MMA7455_SPI_REGISTER_WRITE 0x40
532 int spi_mma7455_module_test(void)
535 int bus_num, cs_num, ret;
536 unsigned char tx_buf[10];
537 unsigned char rx_buf[10];
539 peripheral_spi_h spi;
541 printf(" %s()\n", __func__);
542 printf("Enter SPI bus number : ");
543 if (scanf("%d", &bus_num) < 0)
546 printf("Enter SPI cs number : ");
547 if (scanf("%d", &cs_num) < 0)
550 if ((ret = peripheral_spi_open(bus_num, cs_num, &spi)) < 0) {
551 printf("Failed to open I2C communication, ret : %d\n", ret);
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);
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);
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);
578 unsigned char buf[5];
581 for (i = 0; i < 3; i++) {
582 tx_buf[0] = (MMA7455_XOUT8 + i) << 1;
584 ret = peripheral_spi_read_write(spi, tx_buf, rx_buf, 2);
586 printf("Failed to read, ret : %d\n", ret);
590 printf("X = 0x%02X, Y = 0x%02X, Z = 0x%02X\n", buf[0], buf[1], buf[2]);
593 peripheral_spi_close(spi);
597 peripheral_spi_close(spi);
601 int gpio_test_get_handle_by_pin(int pin, peripheral_gpio_h *gpio)
603 peripheral_gpio_h handle;
609 handle = (peripheral_gpio_h)cursor->data;
610 peripheral_gpio_get_pin(handle, &cur_pin);
613 cursor = g_list_next(cursor);
615 if (!cursor) return -1;
622 int print_gpio_handle(void)
624 peripheral_gpio_h gpio;
626 peripheral_gpio_direction_e direction;
627 peripheral_gpio_edge_e edge;
629 char *dir_str, *edge_str;
631 printf("--- GPIO handle info. -------------------------------------------\n");
632 printf(" No Pin Direction Value Edge mode\n");
636 gpio = (peripheral_gpio_h)cursor->data;
638 if (peripheral_gpio_get_pin(gpio, &pin) < 0)
640 if (peripheral_gpio_get_direction(gpio, &direction) < 0)
642 if (peripheral_gpio_get_edge_mode(gpio, &edge) < 0)
645 if (direction == PERIPHERAL_GPIO_DIRECTION_IN)
647 else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_LOW)
649 else if (direction == PERIPHERAL_GPIO_DIRECTION_OUT_HIGH)
650 dir_str = "OUT_HIGH";
654 if (edge == PERIPHERAL_GPIO_EDGE_NONE)
656 else if (edge == PERIPHERAL_GPIO_EDGE_RISING)
658 else if (edge == PERIPHERAL_GPIO_EDGE_FALLING)
659 edge_str = "FALLING";
660 else if (edge == PERIPHERAL_GPIO_EDGE_BOTH)
663 edge_str = "UNKNOWN";
665 if (direction == PERIPHERAL_GPIO_DIRECTION_IN) {
666 if (peripheral_gpio_read(gpio, &value) < 0)
668 printf("%8d%8d%12s%8d%12s\n", g_list_position(gpio_list, cursor),
669 pin, dir_str, value, edge_str);
671 printf("%8d%8d%12s%20s\n", g_list_position(gpio_list, cursor),
672 pin, dir_str, edge_str);
674 cursor = g_list_next(cursor);
676 printf("-----------------------------------------------------------------\n");
681 int gpio_test_open(void)
683 peripheral_gpio_h gpio;
686 printf("%s\n", __func__);
687 printf("Enter GPIO pin number\n");
689 if (read_int_input(&pin) < 0)
692 if ((ret = peripheral_gpio_open(pin, &gpio)) < 0) {
693 printf(">>>>> GPIO open failed, ret : %d\n", ret);
696 gpio_list = g_list_append(gpio_list, gpio);
702 int gpio_test_close(void)
704 peripheral_gpio_h gpio;
707 printf("%s\n", __func__);
708 printf("Enter GPIO pin number\n");
710 if (read_int_input(&pin) < 0)
713 if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
714 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
717 gpio_list = g_list_remove(gpio_list, gpio);
719 if ((ret = peripheral_gpio_close(gpio)) < 0) {
720 printf(">>>>> GPIO close failed, ret : %d\n", ret);
728 int gpio_test_set_direction(void)
730 peripheral_gpio_h gpio;
734 printf("%s\n", __func__);
735 printf("Enter gpio pin number\n");
737 if (read_int_input(&pin) < 0)
740 if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
741 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
745 printf("Enter direction (0:IN, 1:OUT_LOW, 2:OUT_HIGH)\n");
746 if (read_int_input(&direction) < 0)
749 if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH ||
750 direction < PERIPHERAL_GPIO_DIRECTION_IN) {
751 printf(">>>>> Wrong input value\n");
755 if ((ret = peripheral_gpio_set_direction(gpio, (peripheral_gpio_direction_e)direction)) < 0) {
756 printf(">>>>> Failed to set direction, ret : %d\n", ret);
763 int gpio_test_write(void)
765 peripheral_gpio_h gpio;
769 printf("%s\n", __func__);
770 printf("Enter gpio pin number\n");
772 if (read_int_input(&pin) < 0)
775 if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
776 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
780 printf("Enter value (0:LOW, 1:HIGH)\n");
781 if (read_int_input(&value) < 0)
784 if (value < 0 || value > 1) {
785 printf(">>>>> Wrong input value\n");
789 if ((ret = peripheral_gpio_write(gpio, value)) < 0) {
790 printf(">>>>> Failed to write value, ret : %d\n", ret);
797 int gpio_test_set_edge_mode(void)
799 peripheral_gpio_h gpio;
803 printf("%s\n", __func__);
804 printf("Enter gpio pin number\n");
806 if (read_int_input(&pin) < 0)
809 if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
810 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
814 printf("Enter edge mode (0:NONE, 1:RISING, 2:FALLING, 3:BOTH)\n");
815 if (read_int_input(&edge) < 0)
818 if (edge < PERIPHERAL_GPIO_EDGE_NONE || edge > PERIPHERAL_GPIO_EDGE_BOTH) {
819 printf(">>>>> Wrong input value\n");
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);
831 int gpio_test_set_register_cb(void)
833 peripheral_gpio_h gpio;
836 printf("%s\n", __func__);
837 printf("Enter gpio pin number\n");
839 if (read_int_input(&pin) < 0)
842 if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
843 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
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);
854 int gpio_test_set_unregister_cb(void)
856 peripheral_gpio_h gpio;
859 printf("%s\n", __func__);
860 printf("Enter gpio pin number\n");
862 if (read_int_input(&pin) < 0)
865 if (gpio_test_get_handle_by_pin(pin, &gpio) < 0) {
866 printf(">>>>> Cannot find handle. Please open the GPIO pin\n");
870 if ((ret = peripheral_gpio_unregister_cb(gpio)) < 0) {
871 printf(">>>>> failed to unregister callback function, ret : %d\n", ret);
878 int enter_main(void);
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},
893 int enter_gpio_test(void)
895 tc_table = gpio_tc_table;
901 int enter_i2c_test(void)
906 int enter_pwm_test(void)
911 int enter_adc_test(void)
916 int enter_uart_test(void)
921 int enter_spi_test(void)
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},
940 int enter_preset_test(void)
942 tc_table = preset_tc_table;
946 int terminate_test(void)
950 printf("Terminate test\n");
951 g_main_loop_quit(main_loop);
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},
972 tc_table = main_tc_table;
977 static int test_input_callback(void *data)
980 long test_id = (long)data;
981 int ret = PERIPHERAL_ERROR_NONE;
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);
996 if (!tc[i].tc_name) {
997 printf(">>>>> Wrong input value!\n");
1004 static void print_tc_usage(void)
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);
1013 printf("===========================================\n");
1014 printf("Enter TC number\n");
1017 static gboolean key_event_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
1019 char buf[BUFFER_LEN] = {0,};
1023 memset(buf, 0, sizeof(buf));
1025 rv = read(0, buf, BUFFER_LEN);
1027 if (*buf == '\n' || *buf == '\r') {
1032 if (rv < 0) return FALSE;
1034 test_id = atoi(buf);
1036 test_input_callback((void *)test_id);
1042 int main(int argc, char **argv)
1046 main_loop = g_main_loop_new(NULL, FALSE);
1047 key_io = g_io_channel_unix_new(0);
1049 tc_table = main_tc_table;
1052 g_io_add_watch(key_io, (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), key_event_cb, NULL);
1054 g_main_loop_run(main_loop);
1056 g_io_channel_unref(key_io);
1057 g_main_loop_unref(main_loop);