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/>.
22 #include <glib/gstdio.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "gpio"
33 static int open_and_write(const gchar *path, const gchar *buf)
38 fd = g_fopen(path, "w");
40 sr_err("Error opening %s: %s", path, g_strerror(errno));
44 wr = g_fprintf(fd, "%s", buf);
47 sr_err("Error writing to %s: %s", path, g_strerror(errno));
54 SR_PRIV int sr_gpio_export(unsigned gpio)
60 path = g_string_sized_new(128);
61 g_string_printf(path, "/sys/class/gpio/gpio%d", gpio);
62 exported = g_file_test(path->str, G_FILE_TEST_IS_DIR);
63 g_string_free(path, TRUE);
65 return 0; /* Already exported. */
67 status = sr_gpio_set_direction(gpio, GPIO_DIR_OUT);
71 buf = g_string_sized_new(16);
72 g_string_printf(buf, "%u\n", gpio);
73 status = open_and_write("/sys/class/gpio/export", buf->str);
74 g_string_free(buf, TRUE);
79 SR_PRIV int sr_gpio_set_direction(unsigned gpio, unsigned direction)
84 path = g_string_sized_new(128);
85 buf = g_string_sized_new(16);
86 g_string_printf(path, "/sys/class/gpio/gpio%d/direction", gpio);
87 g_string_printf(buf, "%s\n", direction == GPIO_DIR_IN ? "in" : "out");
89 status = open_and_write(path->str, buf->str);
91 g_string_free(path, TRUE);
92 g_string_free(buf, TRUE);
97 SR_PRIV int sr_gpio_set_value(unsigned gpio, unsigned value)
102 path = g_string_sized_new(128);
103 buf = g_string_sized_new(16);
104 g_string_printf(path, "/sys/class/gpio/gpio%d/value", gpio);
105 g_string_printf(buf, "%d\n", value);
107 status = open_and_write(path->str, buf->str);
109 g_string_free(path, TRUE);
110 g_string_free(buf, TRUE);
115 SR_PRIV int sr_gpio_get_value(int gpio)
121 path = g_string_sized_new(128);
122 g_string_printf(path, "/sys/class/gpio/gpio%d/value", gpio);
123 fd = g_fopen(path->str, "r");
125 sr_err("Error opening %s: %s", path->str, g_strerror(errno));
126 g_string_free(path, TRUE);
130 status = fscanf(fd, "%d", &ret);
133 sr_err("Error reading from %s: %s", path->str, g_strerror(errno));
134 g_string_free(path, TRUE);
138 g_string_free(path, TRUE);
142 SR_PRIV int sr_gpio_setval_export(int gpio, int value)
146 status = sr_gpio_export(gpio);
150 status = sr_gpio_set_value(gpio, value);
157 SR_PRIV int sr_gpio_getval_export(int gpio)
161 status = sr_gpio_export(gpio);
165 return sr_gpio_get_value(gpio);