2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
24 #include <glib/gstdio.h>
30 #define LOG_PREFIX "gpio"
32 static int open_and_write(const gchar *path, const gchar *buf)
37 fd = g_fopen(path, "w");
39 sr_err("Error opening %s: %s", path, strerror(errno));
43 wr = g_fprintf(fd, "%s", buf);
46 sr_err("Error writing to %s: %s", path, strerror(errno));
53 SR_PRIV int sr_gpio_export(unsigned gpio)
59 path = g_string_sized_new(128);
60 g_string_printf(path, "/sys/class/gpio/gpio%d", gpio);
61 exported = g_file_test(path->str, G_FILE_TEST_IS_DIR);
62 g_string_free(path, TRUE);
64 return 0; /* Already exported. */
66 buf = g_string_sized_new(16);
67 g_string_printf(buf, "%u\n", gpio);
68 status = open_and_write("/sys/class/gpio/export", buf->str);
69 g_string_free(buf, TRUE);
74 SR_PRIV int sr_gpio_set_direction(unsigned gpio, unsigned direction)
79 path = g_string_sized_new(128);
80 buf = g_string_sized_new(16);
81 g_string_printf(path, "/sys/class/gpio/gpio%d/direction", gpio);
82 g_string_printf(buf, "%s\n", direction == GPIO_DIR_IN ? "in" : "out");
84 status = open_and_write(path->str, buf->str);
86 g_string_free(path, TRUE);
87 g_string_free(buf, TRUE);
92 SR_PRIV int sr_gpio_set_value(unsigned gpio, unsigned value)
97 path = g_string_sized_new(128);
98 buf = g_string_sized_new(16);
99 g_string_printf(path, "/sys/class/gpio/gpio%d/value", gpio);
100 g_string_printf(buf, "%d\n", value);
102 status = open_and_write(path->str, buf->str);
104 g_string_free(path, TRUE);
105 g_string_free(buf, TRUE);
110 SR_PRIV int sr_gpio_get_value(int gpio)
116 path = g_string_sized_new(128);
117 g_string_printf(path, "/sys/class/gpio/gpio%d/value", gpio);
118 fd = g_fopen(path->str, "r");
120 sr_err("Error opening %s: %s", path->str, strerror(errno));
121 g_string_free(path, TRUE);
125 status = fscanf(fd, "%d", &ret);
128 sr_err("Error reading from %s: %s", path, strerror(errno));
129 g_string_free(path, TRUE);
133 g_string_free(path, TRUE);
137 SR_PRIV int sr_gpio_setval_export(int gpio, int value)
141 status = sr_gpio_export(gpio);
145 status = sr_gpio_set_direction(gpio, GPIO_DIR_OUT);
149 status = sr_gpio_set_value(gpio, value);
156 SR_PRIV int sr_gpio_getval_export(int gpio)
160 status = sr_gpio_export(gpio);
164 status = sr_gpio_set_direction(gpio, GPIO_DIR_IN);
168 return sr_gpio_get_value(gpio);