Advertisement
Guest User

z2-gpio.c

a guest
Aug 30th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. /* This seems to work if run with "picoc mainprog.c z2-gpio.c"               */
  2. /* It does not run if mainprog.c is listed after z2-gpio.c.          */
  3. /* And the main function must be defined as below (or with no args). */
  4. /* int main(int argc, char ** argv)                                  */
  5.  
  6. /*
  7.  * GPIO user space helpers
  8.  *
  9.  * Copyright 2009 Analog Devices Inc.
  10.  * Michael Hennerich (hennerich@blackfin.uclinux.org)
  11.  *
  12.  * Licensed under the GPL-2 or later
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19.  
  20. #define GPIO_DIR_IN 0
  21. #define GPIO_DIR_OUT    1
  22.  
  23. int gpio_export(unsigned gpio)
  24. {
  25.     FILE *fp = fopen("/sys/class/gpio/export", "w");
  26.     if (fp == NULL) {
  27.         perror("gpio/export");
  28.         return -1;
  29.     }
  30.     fprintf(fp, "%i", gpio);
  31.     fclose(fp);
  32.     return 0;
  33. }
  34.  
  35. int gpio_unexport(unsigned gpio)
  36. {
  37.     FILE *fp = fopen("/sys/class/gpio/export", "w");
  38.     if (fp == NULL) {
  39.         perror("gpio/unexport");
  40.         return -1;
  41.     }
  42.     fprintf(fp, "%i", gpio);
  43.     fclose(fp);
  44.     return 0;
  45. }
  46.  
  47. int gpio_dir(unsigned gpio, unsigned dir)
  48. {
  49.     FILE *fp;
  50.     char buf[60];
  51.  
  52.     snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
  53.     fp = fopen(buf, "w");
  54.     if (fp == NULL) {
  55.         perror("gpio/direction");
  56.         return -1;
  57.     }
  58.  
  59.     if (dir == GPIO_DIR_OUT)
  60.         fprintf(fp, "out");
  61.     else
  62.         fprintf(fp, "in");
  63.  
  64.     fclose(fp);
  65.     return 0;
  66. }
  67.  
  68. int gpio_dir_out(unsigned gpio)
  69. {
  70.     return gpio_dir(gpio, GPIO_DIR_OUT);
  71. }
  72.  
  73. int gpio_dir_in(unsigned gpio)
  74. {
  75.     return gpio_dir(gpio, GPIO_DIR_IN);
  76. }
  77.  
  78. int set_gpio_value(unsigned gpio, unsigned value)
  79. {
  80.     FILE *fp;
  81.     char buf[60];
  82.  
  83.     snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
  84.  
  85.     fp = fopen(buf, "w");
  86.     if (fp == NULL) {
  87.         perror("gpio/value");
  88.         return -1;
  89.     }
  90.  
  91.     if (value)
  92.         fprintf(fp, "1");
  93.     else
  94.         fprintf(fp, "0");
  95.  
  96.     fclose(fp);
  97.     return 0;
  98. }
  99.  
  100. int get_gpio_value(unsigned gpio)
  101. {
  102.     FILE *fp;
  103.     int value = 0;
  104.     char buf[60];
  105.  
  106.     snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
  107.  
  108.     fp = fopen(buf, "r");
  109.     if (fp == NULL) {
  110.         perror("gpio/value");
  111.         return -1;
  112.     }
  113.  
  114.     fscanf(fp, "%d", &value);
  115.  
  116.     fclose(fp);
  117.     return value;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement