4 * Modifications by Derek Molloy, School of Electronic Engineering, DCU
5 * www.eeng.dcu.ie/~molloyd/
6 * Almost entirely based on Software by RidgeRun:
8 * Copyright (c) 2011, RidgeRun
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the RidgeRun.
21 * 4. Neither the name of the RidgeRun nor the
22 * names of its contributors may be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
26 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
29 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "SimpleGPIO.h"
46 /****************************************************************
48 ****************************************************************/
49 int gpio_export(unsigned int gpio)
54 fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
56 perror("gpio/export");
60 len = snprintf(buf, sizeof(buf), "%d", gpio);
67 /****************************************************************
69 ****************************************************************/
70 int gpio_unexport(unsigned int gpio)
75 fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
77 perror("gpio/export");
81 len = snprintf(buf, sizeof(buf), "%d", gpio);
87 /****************************************************************
89 ****************************************************************/
90 int gpio_set_dir(unsigned int gpio, PIN_DIRECTION out_flag)
95 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
97 fd = open(buf, O_WRONLY);
99 perror("gpio/direction");
103 if (out_flag == OUTPUT_PIN)
112 /****************************************************************
114 ****************************************************************/
115 int gpio_set_value(unsigned int gpio, PIN_VALUE value)
120 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
122 fd = open(buf, O_WRONLY);
124 perror("gpio/set-value");
137 /****************************************************************
139 ****************************************************************/
140 int gpio_get_value(unsigned int gpio, unsigned int *value)
146 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
148 fd = open(buf, O_RDONLY);
150 perror("gpio/get-value");
167 /****************************************************************
169 ****************************************************************/
171 int gpio_set_edge(unsigned int gpio, char *edge)
176 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
178 fd = open(buf, O_WRONLY);
180 perror("gpio/set-edge");
184 write(fd, edge, strlen(edge) + 1);
189 /****************************************************************
191 ****************************************************************/
193 int gpio_fd_open(unsigned int gpio)
198 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
200 fd = open(buf, O_RDONLY | O_NONBLOCK );
202 perror("gpio/fd_open");
207 /****************************************************************
209 ****************************************************************/
211 int gpio_fd_close(int fd)
217 /****************************************************************
218 * gpio_omap_mux_setup - Allow us to setup the omap mux mode for a pin
219 ****************************************************************/
220 int gpio_omap_mux_setup(const char *omap_pin0_name, const char *mode)
224 snprintf(buf, sizeof(buf), SYSFS_OMAP_MUX_DIR "%s", omap_pin0_name);
225 fd = open(buf, O_WRONLY);
227 perror("failed to open OMAP_MUX");
230 write(fd, mode, strlen(mode) + 1);