1 /*
2 * Copyright (c) 2015 Christopher Anderson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <dev/gpio.h>
25 #include <lib/console.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #if WITH_LIB_CONSOLE
30
31 struct flag_labels {
32 unsigned id;
33 const char *label;
34 };
35
36 struct flag_labels gpio_flag_labels[] = {
37 { GPIO_INPUT, "input" },
38 { GPIO_OUTPUT, "output" },
39 { GPIO_LEVEL, "level" },
40 { GPIO_EDGE, "edge" },
41 { GPIO_RISING, "rising" },
42 { GPIO_FALLING, "falling" },
43 { GPIO_HIGH, "high" },
44 { GPIO_LOW, "low" },
45 { GPIO_PULLUP, "pullup" },
46 { GPIO_PULLDOWN, "pulldown" },
47 };
48
get_flag_value(const char * str)49 static unsigned int get_flag_value(const char *str)
50 {
51 for (unsigned i = 0; i < countof(gpio_flag_labels); i++) {
52 if (!strcmp(str, gpio_flag_labels[i].label)) {
53 return gpio_flag_labels[i].id;
54 }
55 }
56
57 return 0;
58 }
59
60 /* Ideally this would be generic, but different platforms have varying manners of handling gpio
61 * numbers / banks etc. Nvidia uses A-F, ST uses # and ports, Xilinx uses #s and banks. Etc.
62 */
cmd_gpio(int argc,const cmd_args * argv)63 static int cmd_gpio(int argc, const cmd_args *argv)
64 {
65 if (argc == 4 && !strcmp(argv[1].str,"set")) {
66 unsigned int gpio = argv[2].u;
67 unsigned int value = argv[3].u;
68
69 gpio_set(gpio, value);
70 } else if (argc == 3 && !strcmp(argv[1].str,"get")) {
71 unsigned int gpio = argv[2].u;
72
73 printf("gpio %u: %d\n", gpio, gpio_get(gpio));
74 } else if (argc >= 3 && !strcmp(argv[1].str,"cfg")) {
75 unsigned int gpio = argv[2].u;
76 unsigned int flags = 0;
77
78 for (int i = 3; i < argc; i++) {
79 flags |= get_flag_value(argv[i].str);
80 }
81
82 gpio_config(gpio, flags);
83 } else {
84 printf("gpio set <gpio #> <value>\n");
85 printf("gpio get <gpio #>\n");
86 printf("gpio cfg <gpio #> [flags: ");
87 for (unsigned int i = 0; i < countof(gpio_flag_labels); i++) {
88 printf("%s ", gpio_flag_labels[i].label);
89 }
90 putchar(']');
91 putchar('\n');
92 }
93
94 return 0;
95 }
96 STATIC_COMMAND_START
97 STATIC_COMMAND("gpio", "commands for manipulating system gpios", &cmd_gpio)
98 STATIC_COMMAND_END(gpio);
99
100 #endif
101