Mercurial > hg > beaglert
comparison core/GPIOcontrol.cpp @ 0:8a575ba3ab52
Initial commit.
author | andrewm |
---|---|
date | Fri, 31 Oct 2014 19:10:17 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a575ba3ab52 |
---|---|
1 /* | |
2 * SimpleGPIO.cpp | |
3 * | |
4 * Modifications by Derek Molloy, School of Electronic Engineering, DCU | |
5 * www.derekmolloy.ie | |
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 "../include/GPIOcontrol.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 /**************************************************************** | |
48 * gpio_setup | |
49 ****************************************************************/ | |
50 int gpio_setup(unsigned int gpio, int out_flag) | |
51 { | |
52 /* Export the GPIO pins and set their direction */ | |
53 if(gpio_export(gpio)) { | |
54 printf("Unable to export GPIO input pin\n"); | |
55 return -1; | |
56 } | |
57 if(gpio_set_dir(gpio, out_flag)) { | |
58 printf("Unable to set GPIO input direction\n"); | |
59 return -1; | |
60 } | |
61 | |
62 return gpio_fd_open(gpio, O_RDWR); | |
63 } | |
64 | |
65 /**************************************************************** | |
66 * gpio_export | |
67 ****************************************************************/ | |
68 int gpio_export(unsigned int gpio) | |
69 { | |
70 int fd, len, result = 0; | |
71 char buf[MAX_BUF]; | |
72 | |
73 fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY); | |
74 if (fd < 0) { | |
75 perror("gpio/export"); | |
76 return fd; | |
77 } | |
78 | |
79 len = snprintf(buf, sizeof(buf), "%d", gpio); | |
80 if(write(fd, buf, len) < 0) | |
81 result = -1; | |
82 close(fd); | |
83 | |
84 return result; | |
85 } | |
86 | |
87 /**************************************************************** | |
88 * gpio_unexport | |
89 ****************************************************************/ | |
90 int gpio_unexport(unsigned int gpio) | |
91 { | |
92 int fd, len, result = 0; | |
93 char buf[MAX_BUF]; | |
94 | |
95 fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY); | |
96 if (fd < 0) { | |
97 perror("gpio/export"); | |
98 return fd; | |
99 } | |
100 | |
101 len = snprintf(buf, sizeof(buf), "%d", gpio); | |
102 if(write(fd, buf, len) < 0) | |
103 result = -1; | |
104 close(fd); | |
105 return result; | |
106 } | |
107 | |
108 /**************************************************************** | |
109 * gpio_set_dir | |
110 ****************************************************************/ | |
111 int gpio_set_dir(unsigned int gpio, int out_flag) | |
112 { | |
113 int fd, result = 0; | |
114 char buf[MAX_BUF]; | |
115 | |
116 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio); | |
117 | |
118 fd = open(buf, O_WRONLY); | |
119 if (fd < 0) { | |
120 perror("gpio/direction"); | |
121 return fd; | |
122 } | |
123 | |
124 if (out_flag == OUTPUT_PIN) { | |
125 if(write(fd, "out", 4) < 0) | |
126 result = -1; | |
127 } | |
128 else { | |
129 if(write(fd, "in", 3) < 0) | |
130 result = -1; | |
131 } | |
132 | |
133 close(fd); | |
134 return result; | |
135 } | |
136 | |
137 /**************************************************************** | |
138 * gpio_set_value | |
139 ****************************************************************/ | |
140 int gpio_set_value(unsigned int gpio, int value) | |
141 { | |
142 int fd, result = 0; | |
143 char buf[MAX_BUF]; | |
144 | |
145 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); | |
146 | |
147 fd = open(buf, O_WRONLY); | |
148 if (fd < 0) { | |
149 perror("gpio/set-value"); | |
150 return fd; | |
151 } | |
152 | |
153 if (value==LOW) { | |
154 if(write(fd, "0", 2) < 0) | |
155 result = -1; | |
156 } | |
157 else { | |
158 if(write(fd, "1", 2) < 0) | |
159 result = -1; | |
160 } | |
161 | |
162 close(fd); | |
163 return result; | |
164 } | |
165 | |
166 /**************************************************************** | |
167 * gpio_get_value | |
168 ****************************************************************/ | |
169 int gpio_get_value(unsigned int gpio, unsigned int *value) | |
170 { | |
171 int fd, result = 0; | |
172 char buf[MAX_BUF]; | |
173 char ch; | |
174 | |
175 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); | |
176 | |
177 fd = open(buf, O_RDONLY); | |
178 if (fd < 0) { | |
179 perror("gpio/get-value"); | |
180 return fd; | |
181 } | |
182 | |
183 if(read(fd, &ch, 1) <= 0) { | |
184 result = -1; | |
185 } | |
186 else { | |
187 if (ch != '0') { | |
188 *value = 1; | |
189 } else { | |
190 *value = 0; | |
191 } | |
192 } | |
193 | |
194 close(fd); | |
195 return result; | |
196 } | |
197 | |
198 | |
199 /**************************************************************** | |
200 * gpio_set_edge | |
201 ****************************************************************/ | |
202 | |
203 int gpio_set_edge(unsigned int gpio, char *edge) | |
204 { | |
205 int fd, result = 0; | |
206 char buf[MAX_BUF]; | |
207 | |
208 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio); | |
209 | |
210 fd = open(buf, O_WRONLY); | |
211 if (fd < 0) { | |
212 perror("gpio/set-edge"); | |
213 return fd; | |
214 } | |
215 | |
216 if(write(fd, edge, strlen(edge) + 1) < 0) | |
217 result = -1; | |
218 close(fd); | |
219 return result; | |
220 } | |
221 | |
222 /**************************************************************** | |
223 * gpio_fd_open | |
224 ****************************************************************/ | |
225 | |
226 int gpio_fd_open(unsigned int gpio, int writeFlag) | |
227 { | |
228 int fd; | |
229 char buf[MAX_BUF]; | |
230 | |
231 snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); | |
232 | |
233 fd = open(buf, writeFlag | O_NONBLOCK ); | |
234 if (fd < 0) { | |
235 perror("gpio/fd_open"); | |
236 } | |
237 return fd; | |
238 } | |
239 | |
240 /**************************************************************** | |
241 * gpio_fd_close | |
242 ****************************************************************/ | |
243 | |
244 int gpio_fd_close(int fd) | |
245 { | |
246 return close(fd); | |
247 } | |
248 | |
249 /**************************************************************** | |
250 * gpio_read | |
251 ****************************************************************/ | |
252 int gpio_read(int fd, unsigned int *value) | |
253 { | |
254 int result = 0; | |
255 char ch; | |
256 | |
257 if(read(fd, &ch, 1) <= 0) { | |
258 result = -1; | |
259 } | |
260 else { | |
261 if (ch != '0') { | |
262 *value = 1; | |
263 } else { | |
264 *value = 0; | |
265 } | |
266 } | |
267 | |
268 return result; | |
269 } | |
270 | |
271 /**************************************************************** | |
272 * gpio_write | |
273 ****************************************************************/ | |
274 int gpio_write(int fd, int value) | |
275 { | |
276 int result = 0; | |
277 //char buf[MAX_BUF]; | |
278 | |
279 if (value==LOW) { | |
280 if(write(fd, "0", 2) < 0) | |
281 result = -1; | |
282 } | |
283 else { | |
284 if(write(fd, "1", 2) < 0) | |
285 result = -1; | |
286 } | |
287 | |
288 return result; | |
289 } | |
290 | |
291 | |
292 /**************************************************************** | |
293 * gpio_dismiss | |
294 ****************************************************************/ | |
295 int gpio_dismiss(int fd, unsigned int gpio) | |
296 { | |
297 close(fd); | |
298 gpio_unexport(gpio); | |
299 return 0; | |
300 } | |
301 | |
302 /**************************************************************** | |
303 * led_set_trigger | |
304 ****************************************************************/ | |
305 int led_set_trigger(unsigned int lednum, const char *trigger) | |
306 { | |
307 // Set the trigger source for an onboard user LED | |
308 int fd, result = 0; | |
309 char buf[MAX_BUF]; | |
310 | |
311 snprintf(buf, sizeof(buf), SYSFS_LED_DIR "/beaglebone:green:usr%d/trigger", lednum); | |
312 | |
313 fd = open(buf, O_WRONLY); | |
314 if (fd < 0) { | |
315 perror("gpio/led-set-trigger"); | |
316 return fd; | |
317 } | |
318 | |
319 if(write(fd, trigger, strlen(trigger) + 1) < 0) | |
320 result = -1; | |
321 | |
322 close(fd); | |
323 return result; | |
324 } | |
325 |