Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30103339
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: LINUX

2017-05-18 15:57:29


点击(此处)折叠或打开

  1. /* Copyright (c) 2011, RidgeRun
  2.  * All rights reserved.
  3.  *
  4.  * Contributors include:
  5.  * Todd Fischer
  6.  * Brad Lu
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  * notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  * notice, this list of conditions and the following disclaimer in the
  14.  * documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  * must display the following acknowledgement:
  17.  * This product includes software developed by the RidgeRun.
  18.  * 4. Neither the name of the RidgeRun nor the
  19.  * names of its contributors may be used to endorse or promote products
  20.  * derived from this software without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25.  * DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
  26.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  */

  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <fcntl.h>
  39. #include <poll.h>

  40.  /****************************************************************
  41.  * Constants
  42.  ****************************************************************/
  43.  
  44. #define SYSFS_GPIO_DIR "/sys/class/gpio"
  45. #define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
  46. #define MAX_BUF 64

  47. /****************************************************************
  48.  * gpio_export
  49.  ****************************************************************/
  50. int gpio_export(unsigned int gpio)
  51. {
  52.     int fd, len;
  53.     char buf[MAX_BUF];
  54.  
  55.     fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
  56.     if (fd < 0) {
  57.         perror("gpio/export");
  58.         return fd;
  59.     }
  60.  
  61.     len = snprintf(buf, sizeof(buf), "%d", gpio);
  62.     write(fd, buf, len);
  63.     close(fd);
  64.  
  65.     return 0;
  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.  * gpio_set_dir
  88.  ****************************************************************/
  89. int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
  90. {
  91.     int fd, len;
  92.     char buf[MAX_BUF];
  93.  
  94.     len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
  95.  
  96.     fd = open(buf, O_WRONLY);
  97.     if (fd < 0) {
  98.         perror("gpio/direction");
  99.         return fd;
  100.     }
  101.  
  102.     if (out_flag)
  103.         write(fd, "out", 4);
  104.     else
  105.         write(fd, "in", 3);
  106.  
  107.     close(fd);
  108.     return 0;
  109. }

  110. /****************************************************************
  111.  * gpio_set_value
  112.  ****************************************************************/
  113. int gpio_set_value(unsigned int gpio, unsigned int value)
  114. {
  115.     int fd, len;
  116.     char buf[MAX_BUF];
  117.  
  118.     len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  119.  
  120.     fd = open(buf, O_WRONLY);
  121.     if (fd < 0) {
  122.         perror("gpio/set-value");
  123.         return fd;
  124.     }
  125.  
  126.     if (value)
  127.         write(fd, "1", 2);
  128.     else
  129.         write(fd, "0", 2);
  130.  
  131.     close(fd);
  132.     return 0;
  133. }

  134. /****************************************************************
  135.  * gpio_get_value
  136.  ****************************************************************/
  137. int gpio_get_value(unsigned int gpio, unsigned int *value)
  138. {
  139.     int fd, len;
  140.     char buf[MAX_BUF];
  141.     char ch;

  142.     len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  143.  
  144.     fd = open(buf, O_RDONLY);
  145.     if (fd < 0) {
  146.         perror("gpio/get-value");
  147.         return fd;
  148.     }
  149.  
  150.     read(fd, &ch, 1);

  151.     if (ch != '0') {
  152.         *value = 1;
  153.     } else {
  154.         *value = 0;
  155.     }
  156.  
  157.     close(fd);
  158.     return 0;
  159. }


  160. /****************************************************************
  161.  * gpio_set_edge
  162.  ****************************************************************/

  163. int gpio_set_edge(unsigned int gpio, char *edge)
  164. {
  165.     int fd, len;
  166.     char buf[MAX_BUF];

  167.     len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
  168.  
  169.     fd = open(buf, O_WRONLY);
  170.     if (fd < 0) {
  171.         perror("gpio/set-edge");
  172.         return fd;
  173.     }
  174.  
  175.     write(fd, edge, strlen(edge) + 1);
  176.     close(fd);
  177.     return 0;
  178. }

  179. /****************************************************************
  180.  * gpio_fd_open
  181.  ****************************************************************/

  182. int gpio_fd_open(unsigned int gpio)
  183. {
  184.     int fd, len;
  185.     char buf[MAX_BUF];

  186.     len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
  187.  
  188.     fd = open(buf, O_RDONLY | O_NONBLOCK );
  189.     if (fd < 0) {
  190.         perror("gpio/fd_open");
  191.     }
  192.     return fd;
  193. }

  194. /****************************************************************
  195.  * gpio_fd_close
  196.  ****************************************************************/

  197. int gpio_fd_close(int fd)
  198. {
  199.     return close(fd);
  200. }

  201. /****************************************************************
  202.  * Main
  203.  ****************************************************************/
  204. int main(int argc, char **argv, char **envp)
  205. {
  206.     struct pollfd fdset[2];
  207.     int nfds = 2;
  208.     int gpio_fd, timeout, rc;
  209.     char *buf[MAX_BUF];
  210.     unsigned int gpio;
  211.     int len;



  212.     if (argc < 2) {
  213.         printf("Usage: gpio-int \n\n");
  214.         printf("Waits for a change in the GPIO pin voltage level or input on stdin\n");
  215.         exit(-1);
  216.     }

  217.     gpio = atoi(argv[1]);

  218.     gpio_export(gpio);
  219.     gpio_set_dir(gpio, 0);
  220.     gpio_set_edge(gpio, "rising");
  221.     gpio_fd = gpio_fd_open(gpio);

  222.     timeout = POLL_TIMEOUT;
  223.  
  224.     while (1) {
  225.         memset((void*)fdset, 0, sizeof(fdset));

  226.         fdset[0].fd = STDIN_FILENO;
  227.         fdset[0].events = POLLIN;
  228.       
  229.         fdset[1].fd = gpio_fd;
  230.         fdset[1].events = POLLPRI;

  231.         rc = poll(fdset, nfds, timeout);

  232.         if (rc < 0) {
  233.             printf("\npoll() failed!\n");
  234.             return -1;
  235.         }
  236.       
  237.         if (rc == 0) {
  238.             printf(".");
  239.         }
  240.             
  241.         if (fdset[1].revents & POLLPRI) {
  242.             lseek(fdset[1].fd, 0, SEEK_SET);
  243.             len = read(fdset[1].fd, buf, MAX_BUF);
  244.             printf("\npoll() GPIO %d interrupt occurred\n", gpio);
  245.         }

  246.         if (fdset[0].revents & POLLIN) {
  247.             (void)read(fdset[0].fd, buf, 1);
  248.             printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]);
  249.         }

  250.         fflush(stdout);
  251.     }

  252.     gpio_fd_close(gpio_fd);
  253.     return 0;
  254. }

阅读(962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~