1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* C Implementation: gpio
*
* Description:
*
*
* Author: Paolo Zebelloni <p.zebelloni@c-labs-wt.com>, (C) 2014
*
* Copyright: See COPYING file that comes with this distribution
*
*/
#include "gpio.h"
#include "output.h"
#ifdef __Windows__
/* Added for building on Windows
*
* Daniel Beer <dlbeer@gmail.com>, 3 Mar 2015
*/
int gpio_is_exported ( unsigned int gpio )
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
int gpio_export ( unsigned int gpio )
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
int gpio_unexport ( unsigned int gpio )
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
int gpio_set_dir ( unsigned int gpio, unsigned int out_flag )
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
int gpio_set_value ( unsigned int gpio, unsigned int value )
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
int gpio_set_value_fd (int fd, int value)
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
int gpio_get_value ( unsigned int gpio )
{
return 0;
}
int gpio_get_value_fd (int fd, unsigned int gpio)
{
return 0;
}
int gpio_open_fd (unsigned int gpio)
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
/**
* @return 1 if the gpio is already exported, 0 otherwise, -1 on error
*/
int gpio_is_exported ( unsigned int gpio )
{
char dir_name[100] = {};
snprintf(dir_name, sizeof(dir_name) - 1, SYSFS_GPIO_DIR "/gpio%d", gpio);
struct stat s;
int err = stat(dir_name, &s);
if(-1 == err)
{
if(errno == ENOENT)
{
return 0;
} else {
return -1;
}
} else {
if(S_ISDIR(s.st_mode))
{
return 1;
} else {
return -1;
}
}
return -1;
}
/**
* Before a Linux application can configure and use a GPIO, the GPIO first has to be exported to user.
* Each GPIO is not accessible from user space until the GPIO has been exported.
* You can only export a GPIO that isn't owned by a Linux kernel driver
* @param gpio GPIO number
* @return 0 if OK, -1 if fails
*/
int gpio_export ( unsigned int gpio )
{
int fd, len;
char buf[MAX_BUF];
fd = open ( SYSFS_GPIO_DIR "/export", O_WRONLY );
if ( fd < 0 )
{
pr_error ( "gpio/export" );
return -1;
}
len = snprintf ( buf, sizeof ( buf ), "%d", gpio );
write ( fd, buf, len );
close ( fd );
return 0;
}
/**
* Complement of gpio_export().
* @param gpio GPIO number
* @return 0 if OK, -1 if fails
*/
int gpio_unexport ( unsigned int gpio )
{
int fd, len;
char buf[MAX_BUF];
fd = open ( SYSFS_GPIO_DIR "/unexport", O_WRONLY );
if ( fd < 0 )
{
pr_error ( "gpio/unexport" );
return -1;
}
len = snprintf ( buf, sizeof ( buf ), "%d", gpio );
write ( fd, buf, len );
close ( fd );
return 0;
}
/**
* To avoid hardware issues where two devices are driving the same signal, GPIOs default to be configured as an input.
* If you want to use the GPIO as an output, you need to change the configuration
* @param gpio GPIO number
* @param out_flag TRUE means OUT, FALSE means IN
* @return 0 if OK, -1 if fails
*/
int gpio_set_dir ( unsigned int gpio, unsigned int out_flag )
{
int fd;
char buf[MAX_BUF];
snprintf ( buf, MAX_BUF, SYSFS_GPIO_DIR "/gpio%d/direction", gpio );
fd = open ( buf, O_WRONLY );
if ( fd < 0 )
{
pr_error ( "gpio/direction" );
return -1;
}
if ( out_flag )
write ( fd, "out", 4 );
else
write ( fd, "in", 3 );
close ( fd );
return 0;
}
/**
* Set HI or LO state to GPIO (output) pin.
* @param gpio GPIO number
* @param value TRUE means HI, FALSE means LO
* @return 0 if OK, -1 if fails
*/
int gpio_set_value ( unsigned int gpio, unsigned int value )
{
int fd;
char buf[MAX_BUF];
snprintf ( buf, MAX_BUF, SYSFS_GPIO_DIR "/gpio%d/value", gpio );
fd = open ( buf, O_WRONLY );
if ( fd < 0 )
{
pr_error ( "gpio/set-value" );
return -1;
}
write ( fd, value ? "1" : "0", 2 );
close ( fd );
return 0;
}
/**
* Read state of GPIO (input) pin.
* @param gpio GPIO number
* @return 0 if LO, 1 if HI, -1 if fails
*/
int gpio_get_value ( unsigned int gpio )
{
int fd;
char buf[MAX_BUF];
char ch;
snprintf ( buf, MAX_BUF, SYSFS_GPIO_DIR "/gpio%d/value", gpio );
fd = open ( buf, O_RDONLY );
if ( fd < 0 )
{
pr_error ( "gpio/get-value" );
return -1;
}
read ( fd, &ch, 1 );
close ( fd );
return ( ch != '0' );
}
/**
* Opens GPIO (input/output) pin file descriptor
* @param gpio GPIO number
* @return file descriptor, -1 if fails
*/
int gpio_open_fd ( unsigned int gpio ) {
int fd;
char buf[MAX_BUF];
snprintf ( buf, MAX_BUF, SYSFS_GPIO_DIR "/gpio%d/value", gpio );
fd = open ( buf, O_RDWR );
if ( fd < 0 )
{
pr_error ( "gpio/get-value" );
return -1;
}
return fd;
}
/**
* Sets GPIO (output) value with given file descriptor
* @param file descriptor
* @param gpio GPIO number
* @return 0 if OK, -1 if fails
*/
int gpio_set_value_fd (int fd, int value)
{
ssize_t ret;
char gpio_value = value + '0';
ret = write (fd, &gpio_value, 1);
if (ret != 1)
{
printf("Error setting value gpio\n");
return -1;
}
return 0;
}
/**
* Read state of GPIO (input) pin, with file descriptor
* @param file descriptor
* @param gpio GPIO number
* @return 0 if LO, 1 if HI, -1 if fails
*/
int gpio_get_value_fd (int fd, unsigned int gpio)
{
ssize_t ret;
char value;
ret = pread (fd, &value, 1, 0);
if (ret != 1)
{
printf("Error getting value of gpio %u\n", gpio);
return -1;
}
return value == '1';
}
#endif
|