1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <poll.h>
7 #include <sys/ioctl.h>
8 
9 #if AOS_COMP_CLI
10 #include "aos/cli.h"
11 #endif
12 #include "aos/vfs.h"
13 
14 #include <vfsdev/gpio_dev.h>
15 #include <drivers/char/u_device.h>
16 #include <drivers/u_ld.h>
17 
vfs_gpio_test(int id)18 int vfs_gpio_test(int id)
19 {
20     int ret = 0;
21     int fd = 0;
22     char buffer[16];
23     struct gpio_io_config config;
24 
25     fd = open("/dev/gpio", 0);
26     ddkc_info("open gpio %s, fd:%d\r\n", fd >= 0 ? "success" : "fail", fd);
27 
28     if (fd >= 0) {
29         config.id = id;
30         config.config = GPIO_IO_INPUT | GPIO_IO_INPUT_PU;
31         config.data = 0;
32         ret = ioctl(fd, IOC_GPIO_GET,  (unsigned long)&config);
33         ddkc_info("gpio read %d return %d\r\n", id, ret);
34 
35         config.id = id;
36         config.config = GPIO_IO_OUTPUT | GPIO_IO_OUTPUT_PP;
37         config.data = id%2;
38         ret = ioctl(fd, IOC_GPIO_SET, (unsigned long)&config);
39         ddkc_info("gpio write %d return %d\r\n", config.data, ret);
40         usleep(1000000);
41         config.config = GPIO_IO_OUTPUT_TOGGLE;
42         config.data = 0;
43         ret = ioctl(fd, IOC_GPIO_SET, (unsigned long)&config);
44         ddkc_info("gpio toggle return %d\r\n", ret);
45 
46         close(fd);
47     }
48     return 0;
49 }
50 
51 #if AOS_COMP_CLI
vfs_gpio_cli_cmd(char * buf,int len,int argc,char ** argv)52 static void vfs_gpio_cli_cmd(char *buf, int len, int argc, char **argv)
53 {
54     int ret = 0;
55     int id = argc > 1 ? atoi(argv[1]) : 1;
56 
57     ddkc_info("argc:%d, gpio[%d]\n", argc, id);
58     ret = vfs_gpio_test(id);
59 
60     ddkc_info("vfs gpio test %s, ret:%d\r\n", ret ? "failed" : "success", ret);
61 
62     return;
63 }
64 
65 struct cli_command vfs_gpio_cli_cmds[] = {
66     { "gpiot", "gpio test", vfs_gpio_cli_cmd, },
67 };
68 
vfs_gpio_test_cmd_init(void)69 int vfs_gpio_test_cmd_init(void)
70 {
71     return aos_cli_register_commands(&vfs_gpio_cli_cmds[0], sizeof(vfs_gpio_cli_cmds) / sizeof(vfs_gpio_cli_cmds[0]));
72 }
73 
74 POST_DRIVER_ENTRY(vfs_gpio_test_cmd_init)
75 #endif /* AOS_COMP_CLI */
76