Voss Connection Detection  1
Detect a connector click using realtime code on Bela hardware
SimpleGPIO.cpp
Go to the documentation of this file.
1 /*
2  * SimpleGPIO.cpp
3  *
4  * Modifications by Derek Molloy, School of Electronic Engineering, DCU
5  * www.eeng.dcu.ie/~molloyd/
6  * Almost entirely based on Software by RidgeRun:
7  *
8  * Copyright (c) 2011, RidgeRun
9  * All rights reserved.
10  *
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.
24  *
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.
35  */
36 
37 #include "SimpleGPIO.h"
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <poll.h>
45 
46 /****************************************************************
47  * gpio_export
48  ****************************************************************/
49 int gpio_export(unsigned int gpio)
50 {
51  int fd, len;
52  char buf[MAX_BUF];
53 
54  fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
55  if (fd < 0) {
56  perror("gpio/export");
57  return fd;
58  }
59 
60  len = snprintf(buf, sizeof(buf), "%d", gpio);
61  write(fd, buf, len);
62  close(fd);
63 
64  return 0;
65 }
66 
67 /****************************************************************
68  * gpio_unexport
69  ****************************************************************/
70 int gpio_unexport(unsigned int gpio)
71 {
72  int fd, len;
73  char buf[MAX_BUF];
74 
75  fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
76  if (fd < 0) {
77  perror("gpio/export");
78  return fd;
79  }
80 
81  len = snprintf(buf, sizeof(buf), "%d", gpio);
82  write(fd, buf, len);
83  close(fd);
84  return 0;
85 }
86 
87 /****************************************************************
88  * gpio_set_dir
89  ****************************************************************/
90 int gpio_set_dir(unsigned int gpio, PIN_DIRECTION out_flag)
91 {
92  int fd;
93  char buf[MAX_BUF];
94 
95  snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
96 
97  fd = open(buf, O_WRONLY);
98  if (fd < 0) {
99  perror("gpio/direction");
100  return fd;
101  }
102 
103  if (out_flag == OUTPUT_PIN)
104  write(fd, "out", 4);
105  else
106  write(fd, "in", 3);
107 
108  close(fd);
109  return 0;
110 }
111 
112 /****************************************************************
113  * gpio_set_value
114  ****************************************************************/
115 int gpio_set_value(unsigned int gpio, PIN_VALUE value)
116 {
117  int fd;
118  char buf[MAX_BUF];
119 
120  snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
121 
122  fd = open(buf, O_WRONLY);
123  if (fd < 0) {
124  perror("gpio/set-value");
125  return fd;
126  }
127 
128  if (value==LOW)
129  write(fd, "0", 2);
130  else
131  write(fd, "1", 2);
132 
133  close(fd);
134  return 0;
135 }
136 
137 /****************************************************************
138  * gpio_get_value
139  ****************************************************************/
140 int gpio_get_value(unsigned int gpio, unsigned int *value)
141 {
142  int fd;
143  char buf[MAX_BUF];
144  char ch;
145 
146  snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
147 
148  fd = open(buf, O_RDONLY);
149  if (fd < 0) {
150  perror("gpio/get-value");
151  return fd;
152  }
153 
154  read(fd, &ch, 1);
155 
156  if (ch != '0') {
157  *value = 1;
158  } else {
159  *value = 0;
160  }
161 
162  close(fd);
163  return 0;
164 }
165 
166 
167 /****************************************************************
168  * gpio_set_edge
169  ****************************************************************/
170 
171 int gpio_set_edge(unsigned int gpio, char *edge)
172 {
173  int fd;
174  char buf[MAX_BUF];
175 
176  snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
177 
178  fd = open(buf, O_WRONLY);
179  if (fd < 0) {
180  perror("gpio/set-edge");
181  return fd;
182  }
183 
184  write(fd, edge, strlen(edge) + 1);
185  close(fd);
186  return 0;
187 }
188 
189 /****************************************************************
190  * gpio_fd_open
191  ****************************************************************/
192 
193 int gpio_fd_open(unsigned int gpio)
194 {
195  int fd;
196  char buf[MAX_BUF];
197 
198  snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
199 
200  fd = open(buf, O_RDONLY | O_NONBLOCK );
201  if (fd < 0) {
202  perror("gpio/fd_open");
203  }
204  return fd;
205 }
206 
207 /****************************************************************
208  * gpio_fd_close
209  ****************************************************************/
210 
211 int gpio_fd_close(int fd)
212 {
213  return close(fd);
214 }
215 
216 
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)
221 {
222  int fd;
223  char buf[MAX_BUF];
224  snprintf(buf, sizeof(buf), SYSFS_OMAP_MUX_DIR "%s", omap_pin0_name);
225  fd = open(buf, O_WRONLY);
226  if (fd < 0) {
227  perror("failed to open OMAP_MUX");
228  return fd;
229  }
230  write(fd, mode, strlen(mode) + 1);
231  close(fd);
232  return 0;
233 }
gpio_set_edge
int gpio_set_edge(unsigned int gpio, char *edge)
Definition: SimpleGPIO.cpp:171
PIN_DIRECTION
PIN_DIRECTION
Definition: SimpleGPIO.h:49
MAX_BUF
#define MAX_BUF
Definition: SimpleGPIO.h:46
gpio_unexport
int gpio_unexport(unsigned int gpio)
Definition: SimpleGPIO.cpp:70
PIN_VALUE
PIN_VALUE
Definition: SimpleGPIO.h:54
SYSFS_OMAP_MUX_DIR
#define SYSFS_OMAP_MUX_DIR
Definition: SimpleGPIO.h:47
gpio_fd_close
int gpio_fd_close(int fd)
Definition: SimpleGPIO.cpp:211
LOW
@ LOW
Definition: SimpleGPIO.h:55
SimpleGPIO.h
gpio_fd_open
int gpio_fd_open(unsigned int gpio)
Definition: SimpleGPIO.cpp:193
gpio_get_value
int gpio_get_value(unsigned int gpio, unsigned int *value)
Definition: SimpleGPIO.cpp:140
gpio_set_value
int gpio_set_value(unsigned int gpio, PIN_VALUE value)
Definition: SimpleGPIO.cpp:115
gpio_export
int gpio_export(unsigned int gpio)
Definition: SimpleGPIO.cpp:49
gpio_set_dir
int gpio_set_dir(unsigned int gpio, PIN_DIRECTION out_flag)
Definition: SimpleGPIO.cpp:90
OUTPUT_PIN
@ OUTPUT_PIN
Definition: SimpleGPIO.h:51
SYSFS_GPIO_DIR
#define SYSFS_GPIO_DIR
Definition: SimpleGPIO.h:44
gpio_omap_mux_setup
int gpio_omap_mux_setup(const char *omap_pin0_name, const char *mode)
Definition: SimpleGPIO.cpp:220