gpio  1.4.2
General Purpose I/O Package
gpio.h File Reference

GPIO interface declarations and defines. More...

#include <sys/types.h>
#include "rnr/rnrconfig.h"

Go to the source code of this file.

Classes

struct  gpio_info_t
 GPIO info structure. More...
 

Macros

#define GPIO_DIR_IN   0
 input
 
#define GPIO_DIR_OUT   1
 output
 
#define GPIO_DIR_IN_STR   "in"
 input string
 
#define GPIO_DIR_OUT_STR   "out"
 output string
 
#define GPIO_EDGE_NONE   0
 no edge
 
#define GPIO_EDGE_RISING   1
 rising edge
 
#define GPIO_EDGE_FALLING   2
 falling edge
 
#define GPIO_EDGE_BOTH   3
 both edges
 
#define GPIO_EDGE_NONE_STR   "none"
 no edge string
 
#define GPIO_EDGE_RISING_STR   "rising"
 rising edge string
 
#define GPIO_EDGE_FALLING_STR   "falling"
 falling edge string
 
#define GPIO_EDGE_BOTH_STR   "both"
 both edges string
 
#define GPIO_PULL_DS   0
 disable pullup/down
 
#define GPIO_PULL_UP   1
 enable pullup
 
#define GPIO_PULL_DN   2
 enable pulldown
 
#define GPIO_PULL_DS_STR   "disabled"
 disable pullup/down string
 
#define GPIO_PULL_UP_STR   "up"
 enable pullup string
 
#define GPIO_PULL_DN_STR   "down"
 enable pulldown string
 

Functions

void gpioMakeDirname (int gpio, char buf[], size_t size)
 Make GPIO directory name. More...
 
int gpioExport (int gpio)
 Export (create) a GPIO interface. More...
 
int gpioUnexport (int gpio)
 Unexport (delete) a GPIO interface. More...
 
int gpioSetDirection (int gpio, int dir)
 Set GPIO signal direction. More...
 
int gpioSetEdge (int gpio, int edge)
 Set GPIO edge trigger type. More...
 
int gpioSetPull (int gpio, int pull)
 Set GPIO pull. More...
 
int gpioProbe (int gpio, gpio_info_t *p)
 Safely probe GPIO parameters. More...
 
int gpioRead (int gpio)
 Read GPIO pin's current value. More...
 
int gpioWrite (int gpio, int value)
 Write GPIO value. More...
 
int gpioNotify (int fd, double timeout)
 Notify on GPIO input value change. More...
 
int gpioOpen (int gpio)
 Open GPIO pin. More...
 
int gpioClose (int fd)
 Close GPIO pin. More...
 
int gpioQuickRead (int fd)
 Quick read GPIO pin's current value. More...
 
int gpioQuickWrite (int fd, int value)
 Quick write GPIO pin value. More...
 
int gpioBitBang (int fd, byte_t pattern[], size_t bitCount, unsigned int usecIbd)
 Bit-bang bits out a GPIO pin. More...
 

Detailed Description

GPIO interface declarations and defines.

LastChangedDate
2015-04-08 17:22:10 -0600 (Wed, 08 Apr 2015)
Rev
3913

The memory mapped interface is base on:

Author
Markham Thomas (https://github.com/mlinuxguy/odpygpio)
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file gpio.h.

Function Documentation

int gpioBitBang ( int  fd,
byte_t  pattern[],
size_t  bitCount,
unsigned int  usecIbd 
)

Bit-bang bits out a GPIO pin.

Bits from pattern[0] ... pattern[N] sequentially drive the GPIO output pin. If bit == 0 then the pin is driven low, if bit == 1 then the pin is driven high.

The pattern buffer must be at least (bitCount + 7) / 8 bytes long. The bits in pattern[i] are read out in big-endian order b7 b6 ...

Between bit output, an inter-bit delay of usecIbd is enforced.

Method: sysfs

Parameters
fdOpen GPIO pin file descriptor.
patternBit pattern buffer to bang. The bytes are big-endian.
bitCountCount of bits to bang.
usecIbdInter-bit delay in microseconds. A 0 value means a fast as the architecture can bang.
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 558 of file gpio.c.

References gpioQuickWrite().

Referenced by sysfsBitBang().

562 {
563  size_t byteCount = (bitCount + 7) / 8;
564  size_t i, j, k;
565  int mask, value;
566 
567  for(i=0, k=0; i<byteCount; ++i)
568  {
569  for(j=0, mask=0x80; j<8 && k<bitCount; ++j, ++k)
570  {
571  if( usecIbd > 0 )
572  {
573  usleep(usecIbd);
574  }
575  value = pattern[i] & mask? 1: 0;
576  mask >>= 1;
577  if( gpioQuickWrite(fd, value) < 0 )
578  {
579  return RC_ERROR;
580  }
581  }
582  }
583 
584  return OK;
585 }
int gpioQuickWrite(int fd, int value)
Quick write GPIO pin value.
Definition: gpio.c:538
int gpioClose ( int  fd)

Close GPIO pin.

Method: sysfs

Parameters
fdOpen GPIO pin file descriptor.
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 510 of file gpio.c.

Referenced by sysfsBitBang(), and sysfsRead().

511 {
512  if( fd >= 0 )
513  {
514  close(fd);
515  }
516 
517  return OK;
518 }
int gpioExport ( int  gpio)

Export (create) a GPIO interface.

The call requires root privaleges.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 192 of file gpio.c.

Referenced by main().

193 {
194  int fd;
195  char buf[MAX_PATH];
196 
197  sprintf(buf, "%s/export", GpioRoot);
198 
199  if( (fd = open(buf, O_WRONLY)) < 0 )
200  {
201  LOGSYSERROR("open(\"%s\", O_WRONLY)", buf);
202  return RC_ERROR;
203  }
204 
205  sprintf(buf, "%d", gpio);
206 
207  if( write(fd, buf, strlen(buf)) < 0 )
208  {
209  LOGSYSERROR("write(%d, \"%s\", %zu)", fd, buf, strlen(buf));
210  close(fd);
211  return RC_ERROR;
212  }
213 
214  close(fd);
215 
216  LOGDIAG3("Exported GPIO %d interface.", gpio);
217 
218  return OK;
219 }
void gpioMakeDirname ( int  gpio,
char  buf[],
size_t  size 
)

Make GPIO directory name.

Director name: /sys/class/gpio/gpio<gpio>

The base directory holds key special files such as value, direction, and edge.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
[out]bufBuffer to hold directory name.
sizeSize of buffer in bytes.

Definition at line 587 of file gpio.c.

Referenced by main().

588 {
589  snprintf(buf, size, "%s/gpio%d", GpioRoot, gpio);
590  buf[size-1] = 0;
591 }
int gpioNotify ( int  fd,
double  timeout 
)

Notify on GPIO input value change.

This function blocks until 1) value has changed, 2) a timeout has occurred, or 3) an error was encoutered.

Method: sysfs

Parameters
fdOpen GPIO pin file descriptor.
timeoutWait timeout. A value of 0.0 is no timeout.
Returns
On success the pin value 0 or 1 is returned. Otherwise RC_ERROR(-1) is returned. Use errno to determine error.

Definition at line 442 of file gpio.c.

References gpioQuickRead().

Referenced by mainInit().

443 {
444  fd_set efds;
445  struct timeval tv;
446  int rc;
447 
448  FD_ZERO(&efds);
449  FD_SET(fd, &efds);
450 
451  // no timeout
452  if( timeout <= 0.0 )
453  {
454  rc = select(fd+1, NULL, NULL, &efds, NULL);
455  }
456  else
457  {
458  tv.tv_sec = (long)timeout;
459  tv.tv_usec = (long)((timeout - (double)tv.tv_sec) * 1000000.0);
460  if( tv.tv_usec > 1000000 )
461  {
462  ++tv.tv_sec;
463  tv.tv_usec = 0;
464  }
465  //fprintf(stderr, "%lf --> %ld %ld\n", timeout, tv.tv_sec, tv.tv_usec);
466  rc = select(fd+1, NULL, NULL, &efds, &tv);
467  }
468 
469  // change
470  if( rc > 0 )
471  {
472  LOGDIAG3("GPIO value changed.");
473  rc = gpioQuickRead(fd);
474  }
475 
476  // timeout
477  else if( rc == 0 )
478  {
479  LOGDIAG3("GPIO watch timedout.");
480  rc = gpioQuickRead(fd);
481  }
482 
483  // error
484  else
485  {
486  LOGSYSERROR("select(%d, ...)", fd);
487  }
488 
489  return rc;
490 }
int gpioQuickRead(int fd)
Quick read GPIO pin&#39;s current value.
Definition: gpio.c:520
int gpioOpen ( int  gpio)

Open GPIO pin.

Leaving the file descriptor open allows for quick reads or writes. It is the responsibility of the user to close the file descriptor.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
Returns
On success the open file descriptor is returned. Otherwise RC_ERROR(-1) is returned.

Definition at line 492 of file gpio.c.

Referenced by mainInit(), sysfsBitBang(), and sysfsRead().

493 {
494  int fd;
495  char buf[MAX_PATH];
496 
497  sprintf(buf, "%s/gpio%d/value", GpioRoot, gpio);
498 
499  if( (fd = open(buf, O_RDWR)) < 0 )
500  {
501  LOGSYSERROR("open(\"%s\", O_RDWR)", buf);
502  return RC_ERROR;
503  }
504 
505  LOGDIAG3("Opened GPIO %d.", gpio);
506 
507  return fd;
508 }
int gpioProbe ( int  gpio,
gpio_info_t p 
)

Safely probe GPIO parameters.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
[out]pGPIO info.
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 346 of file gpio.c.

References gpio_info_t::dir, gpio_info_t::edge, gpio_info_t::gpio, GPIO_DIR_IN, GPIO_EDGE_NONE, GPIO_PULL_DS, gpioRead(), gpioReadDirection(), gpioReadEdge(), gpio_info_t::pin, gpio_info_t::pull, and gpio_info_t::value.

Referenced by sysfsProbe().

347 {
348  int v;
349  int rc = OK;
350 
351  p->gpio = gpio;
352  p->pin = -1; // gpioExportedToPin(gpio);
353  p->dir = GPIO_DIR_IN;
354  p->edge = GPIO_EDGE_NONE;
355  p->pull = GPIO_PULL_DS;
356  p->value = 0;
357 
358  if( (v = gpioReadDirection(gpio)) < 0 )
359  {
360  rc = RC_ERROR;
361  }
362  else
363  {
364  p->dir = v;
365  }
366 
367  if( (v = gpioReadEdge(gpio)) < 0 )
368  {
369  rc = RC_ERROR;
370  }
371  else
372  {
373  p->edge = v;
374  }
375 
376  if( (v = gpioRead(gpio)) < 0 )
377  {
378  rc = RC_ERROR;
379  }
380  else
381  {
382  p->value = v;
383  }
384 
385  return rc;
386 }
int pull
pull state
Definition: gpio.h:104
#define GPIO_PULL_DS
disable pullup/down
Definition: gpio.h:88
int gpio
sysfs exported gpio number
Definition: gpio.h:100
#define GPIO_DIR_IN
input
Definition: gpio.h:68
static int gpioReadDirection(int gpio)
Read GPIO current direction.
Definition: gpio.c:87
static int gpioReadEdge(int gpio)
Read GPIO current edge trigger.
Definition: gpio.c:139
int edge
gpio edge type trigger
Definition: gpio.h:103
int gpioRead(int gpio)
Read GPIO pin&#39;s current value.
Definition: gpio.c:388
int dir
gpio direction
Definition: gpio.h:102
#define GPIO_EDGE_NONE
no edge
Definition: gpio.h:76
int pin
external header pin number
Definition: gpio.h:101
int value
current value
Definition: gpio.h:105
int gpioQuickRead ( int  fd)

Quick read GPIO pin's current value.

Method: sysfs

Parameters
fdOpen GPIO pin file descriptor.
Returns
On success the pin value 0 or 1 is returned. Otherwise RC_ERROR(-1) is returned.

Definition at line 520 of file gpio.c.

Referenced by gpioNotify(), mainInit(), and sysfsRead().

521 {
522  char c;
523 
524  // go to top of 'file'
525  lseek(fd, 0, SEEK_SET);
526 
527  if( read(fd, &c, 1) < 0 )
528  {
529  LOGSYSERROR("read(%s, ...)", fd);
530  return RC_ERROR;
531  }
532 
533  LOGDIAG3("Read GPIO value %c.", c);
534 
535  return c == '0'? 0: 1;
536 }
int gpioQuickWrite ( int  fd,
int  value 
)

Quick write GPIO pin value.

Method: sysfs

Parameters
fdOpen GPIO pin file descriptor.
valueGPIO pin value. One of: 0 (low) or 1 (high).
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 538 of file gpio.c.

Referenced by gpioBitBang().

539 {
540  char c;
541 
542  // go to top of 'file'
543  lseek(fd, 0, SEEK_SET);
544 
545  c = value == 0? '0': '1';
546 
547  if( write(fd, &c, 1) < 0 )
548  {
549  LOGSYSERROR("write(%s, \"%c\", 1)", fd, c);
550  return RC_ERROR;
551  }
552 
553  LOGDIAG3("Wrote GPIO value %c.", c);
554 
555  return OK;
556 }
int gpioRead ( int  gpio)

Read GPIO pin's current value.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
Returns
On success the pin value 0 or 1 is returned. Otherwise RC_ERROR(-1) is returned.

Definition at line 388 of file gpio.c.

Referenced by gpioProbe().

389 {
390  int fd;
391  char buf[MAX_PATH];
392  char c;
393 
394  sprintf(buf, "%s/gpio%d/value", GpioRoot, gpio);
395 
396  if( (fd = open(buf, O_RDONLY)) < 0 )
397  {
398  LOGSYSERROR("open(\"%s\", O_RDONLY)", buf);
399  return RC_ERROR;
400  }
401 
402  if( read(fd, &c, 1) < 0 )
403  {
404  LOGSYSERROR("read(%s, ...)", fd);
405  close(fd);
406  return RC_ERROR;
407  }
408 
409  LOGDIAG3("Read GPIO %d value %c.", gpio, c);
410 
411  return c == '0'? 0: 1;
412 }
int gpioSetDirection ( int  gpio,
int  dir 
)

Set GPIO signal direction.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
dirDirection. One of: GPIO_DIR_IN(0) GPIO_DIR_OUT(1)
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 250 of file gpio.c.

References GPIO_DIR_IN, GPIO_DIR_IN_STR, GPIO_DIR_OUT, and GPIO_DIR_OUT_STR.

Referenced by main(), and sysfsSetDirection().

251 {
252  int fd;
253  char buf[MAX_PATH];
254  const char *sDir;
255 
256  switch( dir )
257  {
258  case GPIO_DIR_IN:
259  sDir = GPIO_DIR_IN_STR;
260  break;
261  case GPIO_DIR_OUT:
262  sDir = GPIO_DIR_OUT_STR;
263  break;
264  default:
265  LOGERROR("GPIO %d: \"%s\": Invalid direction. Must be one of: %d %d.",
266  gpio, dir, GPIO_DIR_IN, GPIO_DIR_OUT);
267  return RC_ERROR;
268  }
269 
270  sprintf(buf, "%s/gpio%d/direction", GpioRoot, gpio);
271 
272  if( (fd = open(buf, O_WRONLY)) < 0 )
273  {
274  LOGSYSERROR("open(\"%s\", O_WRONLY)", buf);
275  return RC_ERROR;
276  }
277 
278  if( write(fd, sDir, strlen(sDir)) < 0 )
279  {
280  LOGSYSERROR("write(%d, \"%s\", %zu)", fd, sDir, strlen(sDir));
281  close(fd);
282  return RC_ERROR;
283  }
284 
285  close(fd);
286 
287  LOGDIAG3("GPIO %d direction set to %s.", gpio, sDir);
288 
289  return OK;
290 }
#define GPIO_DIR_IN
input
Definition: gpio.h:68
#define GPIO_DIR_OUT_STR
output string
Definition: gpio.h:71
#define GPIO_DIR_OUT
output
Definition: gpio.h:69
#define GPIO_DIR_IN_STR
input string
Definition: gpio.h:70
int gpioSetEdge ( int  gpio,
int  edge 
)

Set GPIO edge trigger type.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
edgeEdge. One of: GPIO_EDGE_NONE(0) GPIO_EDGE_RISING(1) GPIO_EDGE_FALLING(2) GPIO_EDGE_BOTH(3)
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 292 of file gpio.c.

References GPIO_EDGE_BOTH, GPIO_EDGE_BOTH_STR, GPIO_EDGE_FALLING, GPIO_EDGE_FALLING_STR, GPIO_EDGE_NONE, GPIO_EDGE_NONE_STR, GPIO_EDGE_RISING, and GPIO_EDGE_RISING_STR.

Referenced by main().

293 {
294  int fd;
295  char buf[MAX_PATH];
296  const char *sEdge;
297 
298  switch( edge )
299  {
300  case GPIO_EDGE_NONE:
301  sEdge = GPIO_EDGE_NONE_STR;
302  break;
303  case GPIO_EDGE_RISING:
304  sEdge = GPIO_EDGE_RISING_STR;
305  break;
306  case GPIO_EDGE_FALLING:
307  sEdge = GPIO_EDGE_FALLING_STR;
308  break;
309  case GPIO_EDGE_BOTH:
310  sEdge = GPIO_EDGE_BOTH_STR;
311  break;
312  default:
313  LOGERROR("GPIO %d: \"%s\": Invalid edge. Must be in range [%d-%d].",
314  gpio, edge, GPIO_EDGE_NONE, GPIO_EDGE_BOTH);
315  return RC_ERROR;
316  }
317 
318  sprintf(buf, "%s/gpio%d/edge", GpioRoot, gpio);
319 
320  if( (fd = open(buf, O_WRONLY)) < 0 )
321  {
322  LOGSYSERROR("open(\"%s\", O_WRONLY)", buf);
323  return RC_ERROR;
324  }
325 
326  if( write(fd, sEdge, strlen(sEdge)) < 0 )
327  {
328  LOGSYSERROR("write(%d, \"%s\", %zu)", fd, sEdge, strlen(sEdge));
329  close(fd);
330  return RC_ERROR;
331  }
332 
333  close(fd);
334 
335  LOGDIAG3("GPIO %d edge set to %s.", gpio, sEdge);
336 
337  return OK;
338 }
#define GPIO_EDGE_FALLING
falling edge
Definition: gpio.h:78
#define GPIO_EDGE_BOTH
both edges
Definition: gpio.h:79
#define GPIO_EDGE_FALLING_STR
falling edge string
Definition: gpio.h:82
#define GPIO_EDGE_BOTH_STR
both edges string
Definition: gpio.h:83
#define GPIO_EDGE_NONE_STR
no edge string
Definition: gpio.h:80
#define GPIO_EDGE_NONE
no edge
Definition: gpio.h:76
#define GPIO_EDGE_RISING_STR
rising edge string
Definition: gpio.h:81
#define GPIO_EDGE_RISING
rising edge
Definition: gpio.h:77
int gpioSetPull ( int  gpio,
int  pull 
)

Set GPIO pull.

Methodsys mmap

Parameters
gpioThe sysfs exported GPIO number.
pullPull type. One of: GPIO_PULL_DS(0) to disabled GPIO_PULL_UP(1) to pull-up GPIO_PULL_DN(2) to pull-down
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 340 of file gpio.c.

341 {
342  LOGERROR("Sysfs GPIO pull operation not supported.");
343  return RC_ERROR;
344 }
int gpioUnexport ( int  gpio)

Unexport (delete) a GPIO interface.

The call requires root privaleges.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 221 of file gpio.c.

Referenced by main().

222 {
223  int fd;
224  char buf[MAX_PATH];
225 
226  sprintf(buf, "%s/unexport", GpioRoot);
227 
228  if( (fd = open(buf, O_WRONLY)) < 0 )
229  {
230  LOGSYSERROR("open(\"%s\", O_WRONLY)", buf);
231  return RC_ERROR;
232  }
233 
234  sprintf(buf, "%d", gpio);
235 
236  if( write(fd, buf, strlen(buf)) < 0 )
237  {
238  LOGSYSERROR("write(%d, \"%s\", %zu)", fd, buf, strlen(buf));
239  close(fd);
240  return RC_ERROR;
241  }
242 
243  close(fd);
244 
245  LOGDIAG3("Unexported GPIO %d interface.", gpio);
246 
247  return OK;
248 }
int gpioWrite ( int  gpio,
int  value 
)

Write GPIO value.

Method: sysfs

Parameters
gpioThe sysfs exported GPIO number.
valueGPIO pin value. One of: 0 (low) or 1 (high).
Returns
On success OK(0) is returned, otherwise RC_ERROR(-1) is returned.

Definition at line 414 of file gpio.c.

Referenced by sysfsWrite().

415 {
416  int fd;
417  char buf[MAX_PATH];
418  char c;
419 
420  sprintf(buf, "%s/gpio%d/value", GpioRoot, gpio);
421 
422  if( (fd = open(buf, O_WRONLY)) < 0 )
423  {
424  LOGSYSERROR("open(\"%s\", O_WRONLY)", buf);
425  return RC_ERROR;
426  }
427 
428  c = value == 0? '0': '1';
429 
430  if( write(fd, &c, 1) < 0 )
431  {
432  LOGSYSERROR("write(%s, \"%c\", 1)", fd, c);
433  close(fd);
434  return RC_ERROR;
435  }
436 
437  LOGDIAG3("Wrote GPIO %d value %c.", gpio, c);
438 
439  return OK;
440 }