1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "mraa/gpio.h"
31 
32 struct gpio_source {
33     int pin;
34     mraa_gpio_context context;
35 };
36 
37 void
print_version()38 print_version()
39 {
40     fprintf(stdout, "Version %s on %s\n", mraa_get_version(), mraa_get_platform_name());
41 }
42 
43 void
print_help()44 print_help()
45 {
46     fprintf(stdout, "list              List pins\n");
47     fprintf(stdout, "set pin level     Set pin to level (0/1)\n");
48     fprintf(stdout, "setraw pin level  Set pin to level (0/1) via mmap (if available)\n");
49     fprintf(stdout, "get pin           Get pin level\n");
50     fprintf(stdout, "getraw pin        Get pin level via mmap (if available)\n");
51     fprintf(stdout, "monitor pin       Monitor pin level changes\n");
52     fprintf(stdout, "version           Get mraa version and board name\n");
53 }
54 
55 void
print_command_error()56 print_command_error()
57 {
58     fprintf(stdout, "Invalid command, options are:\n");
59     print_help();
60 }
61 
62 int
list_platform_pins(uint8_t platform_offset)63 list_platform_pins(uint8_t platform_offset)
64 {
65     int pin_count = mraa_get_platform_pin_count(platform_offset);
66     int i;
67     for (i = 0; i < pin_count; ++i) {
68         int pin_id = platform_offset > 0 ? mraa_get_sub_platform_id(i) : i;
69         char* pin_name = mraa_get_pin_name(pin_id);
70         if (strcmp(pin_name, "INVALID") != 0) {
71             fprintf(stdout, "%02d ", pin_id);
72             fprintf(stdout, "%*s: ", (MRAA_PIN_NAME_SIZE - 1), pin_name);
73             if (mraa_pin_mode_test(pin_id, MRAA_PIN_GPIO))
74                 fprintf(stdout, "GPIO ");
75             if (mraa_pin_mode_test(pin_id, MRAA_PIN_I2C))
76                 fprintf(stdout, "I2C  ");
77             if (mraa_pin_mode_test(pin_id, MRAA_PIN_SPI))
78                 fprintf(stdout, "SPI  ");
79             if (mraa_pin_mode_test(pin_id, MRAA_PIN_PWM))
80                 fprintf(stdout, "PWM  ");
81             if (mraa_pin_mode_test(pin_id, MRAA_PIN_AIO))
82                 fprintf(stdout, "AIO  ");
83             if (mraa_pin_mode_test(pin_id, MRAA_PIN_UART))
84                 fprintf(stdout, "UART ");
85             fprintf(stdout, "\n");
86         }
87     }
88     return pin_count;
89 }
90 
91 int
list_pins()92 list_pins()
93 {
94     int pin_count = 0;
95     pin_count += list_platform_pins(MRAA_MAIN_PLATFORM_OFFSET);
96     pin_count += list_platform_pins(MRAA_SUB_PLATFORM_OFFSET);
97     if (pin_count == 0) {
98         fprintf(stdout, "No Pins\n");
99     }
100 }
101 
102 
103 mraa_result_t
gpio_set(int pin,int level,mraa_boolean_t raw)104 gpio_set(int pin, int level, mraa_boolean_t raw)
105 {
106     mraa_gpio_context gpio = mraa_gpio_init(pin);
107     if (gpio != NULL) {
108         mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
109         if (raw != 0) {
110             if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) {
111                 fprintf(stdout,
112                         "mmapped access to gpio %d not supported, falling back to normal mode\n", pin);
113             }
114         }
115         mraa_gpio_write(gpio, level);
116         return MRAA_SUCCESS;
117     }
118     return MRAA_ERROR_INVALID_RESOURCE;
119 }
120 
121 mraa_result_t
gpio_get(int pin,int * level,mraa_boolean_t raw)122 gpio_get(int pin, int* level, mraa_boolean_t raw)
123 {
124     mraa_gpio_context gpio = mraa_gpio_init(pin);
125     if (gpio != NULL) {
126         mraa_gpio_dir(gpio, MRAA_GPIO_IN);
127         if (raw != 0) {
128             if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) {
129                 fprintf(stdout,
130                         "mmapped access to gpio %d not supported, falling back to normal mode\n", pin);
131             }
132         }
133         *level = mraa_gpio_read(gpio);
134         return MRAA_SUCCESS;
135     }
136     return MRAA_ERROR_INVALID_RESOURCE;
137 }
138 
139 
140 void
gpio_isr_handler(void * args)141 gpio_isr_handler(void* args)
142 {
143     struct gpio_source* gpio_info = (struct gpio_source*) args;
144     int level = mraa_gpio_read(gpio_info->context);
145     fprintf(stdout, "Pin %d = %d\n", gpio_info->pin, level);
146 }
147 
148 mraa_result_t
gpio_isr_start(struct gpio_source * gpio_info)149 gpio_isr_start(struct gpio_source* gpio_info)
150 {
151     gpio_info->context = mraa_gpio_init(gpio_info->pin);
152     if (gpio_info->context != NULL) {
153         mraa_result_t status = mraa_gpio_dir(gpio_info->context, MRAA_GPIO_IN);
154         if (status == MRAA_SUCCESS) {
155             status = mraa_gpio_isr(gpio_info->context, MRAA_GPIO_EDGE_BOTH, &gpio_isr_handler, gpio_info);
156         }
157         return status;
158     } else {
159         return MRAA_ERROR_INVALID_RESOURCE;
160     }
161 }
162 
163 
164 mraa_result_t
gpio_isr_stop(struct gpio_source * gpio_info)165 gpio_isr_stop(struct gpio_source* gpio_info)
166 {
167     mraa_gpio_isr_exit(gpio_info->context);
168     mraa_gpio_close(gpio_info->context);
169     return MRAA_SUCCESS;
170 }
171 
172 
173 int
main(int argc,char ** argv)174 main(int argc, char** argv)
175 {
176     if (argc == 1) {
177         print_command_error();
178     }
179 
180     if (argc > 1) {
181         if (strcmp(argv[1], "list") == 0) {
182             list_pins();
183         } else if (strcmp(argv[1], "help") == 0) {
184             print_help();
185         } else if (strcmp(argv[1], "version") == 0) {
186             print_version();
187         } else if ((strcmp(argv[1], "set") == 0) || (strcmp(argv[1], "setraw") == 0)) {
188             if (argc == 4) {
189                 int pin = atoi(argv[2]);
190                 mraa_boolean_t rawmode = strcmp(argv[1], "setraw") == 0;
191                 if (gpio_set(pin, atoi(argv[3]), rawmode) != MRAA_SUCCESS)
192                     fprintf(stdout, "Could not initialize gpio %d\n", pin);
193             } else {
194                 print_command_error();
195             }
196         } else if ((strcmp(argv[1], "get") == 0) || (strcmp(argv[1], "getraw") == 0)) {
197             if (argc == 3) {
198                 int pin = atoi(argv[2]);
199                 int level;
200                 mraa_boolean_t rawmode = strcmp(argv[1], "getraw") == 0;
201                 if (gpio_get(pin, &level, rawmode) == MRAA_SUCCESS) {
202                     fprintf(stdout, "Pin %d = %d\n", pin, level);
203                 } else {
204                     fprintf(stdout, "Could not initialize gpio %d\n", pin);
205                 }
206             } else {
207                 print_command_error();
208             }
209         } else if (strcmp(argv[1], "monitor") == 0) {
210             if (argc == 3) {
211                 int pin = atoi(argv[2]);
212                 struct gpio_source gpio_info;
213                 gpio_info.pin = pin;
214                 if (gpio_isr_start(&gpio_info) == MRAA_SUCCESS) {
215                     fprintf(stdout, "Monitoring level changes to pin %d. Press RETURN to exit.\n", pin);
216                     gpio_isr_handler(&gpio_info);
217                     char aux = 0;
218                     do {
219                         fflush(stdin);
220                         fscanf(stdin, "%c", &aux);
221                     } while (aux != '\n');
222                     gpio_isr_stop(&gpio_info);
223                 } else {
224                     fprintf(stdout, "Failed to register ISR for pin %d\n", pin);
225                 }
226             } else {
227                 print_command_error();
228             }
229         } else {
230             print_command_error();
231         }
232     }
233     return 0;
234 }
235