Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1336626
  • 博文数量: 198
  • 博客积分: 1629
  • 博客等级: 上尉
  • 技术积分: 2743
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 15:41
文章分类
文章存档

2023年(6)

2022年(20)

2021年(8)

2020年(3)

2018年(17)

2017年(3)

2016年(3)

2015年(9)

2014年(13)

2013年(17)

2012年(77)

2011年(22)

分类: 嵌入式

2022-03-03 20:45:43


点击(此处)折叠或打开

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<unistd.h>
  5. #include<fcntl.h>
  6. #include<poll.h>
  7.  
  8. #define MSG(args...) printf(args)
  9.  
  10. //函数声明
  11. static int gpio_export(int pin);
  12. static int gpio_unexport(int pin);
  13. static int gpio_direction(int pin, int dir);
  14. static int gpio_write(int pin, int value);
  15. static int gpio_read(int pin);
  16. static int gpio_edge(int pin, int edge);
  17.  
  18.  
  19. static int gpio_export(int pin)
  20. {
  21.     char buffer[64];
  22.     int len;
  23.     int fd;
  24.   
  25.     fd = open("/sys/class/gpio/export", O_WRONLY);
  26.     if (fd < 0)
  27.     {
  28.         MSG("Failed to open export for writing!\n");
  29.         return(-1);
  30.     }
  31.   
  32.     len = snprintf(buffer, sizeof(buffer), "%d", pin);
  33.     printf("%s,%d,%d\n",buffer,sizeof(buffer),len);
  34.     if (write(fd, buffer, len) < 0)
  35.     {
  36.         MSG("Failed to export gpio!");
  37.         return -1;
  38.     }
  39.      
  40.     close(fd);
  41.     return 0;
  42. }
  43. static int gpio_unexport(int pin)
  44. {
  45.     char buffer[64];
  46.     int len;
  47.     int fd;
  48.   
  49.     fd = open("/sys/class/gpio/unexport", O_WRONLY);
  50.     if (fd < 0)
  51.     {
  52.         MSG("Failed to open unexport for writing!\n");
  53.         return -1;
  54.     }
  55.   
  56.     len = snprintf(buffer, sizeof(buffer), "%d", pin);
  57.     if (write(fd, buffer, len) < 0)
  58.     {
  59.         MSG("Failed to unexport gpio!");
  60.         return -1;
  61.     }
  62.      
  63.     close(fd);
  64.     return 0;
  65. }
  66. //dir: 0-->IN, 1-->OUT
  67. static int gpio_direction(int pin, int dir)
  68. {
  69.     static const char dir_str[] = "in\0out";
  70.     char path[64];
  71.     int fd;
  72.   
  73.     snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
  74.     fd = open(path, O_WRONLY);
  75.     if (fd < 0)
  76.     {
  77.         MSG("Failed to open gpio direction for writing!\n");
  78.         return -1;
  79.     }
  80.   
  81.     if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0)
  82.     {
  83.         MSG("Failed to set direction!\n");
  84.         return -1;
  85.     }
  86.   
  87.     close(fd);
  88.     return 0;
  89. }
  90. //value: 0-->LOW, 1-->HIGH
  91. static int gpio_write(int pin, int value)
  92. {
  93.     static const char values_str[] = "01";
  94.     char path[64];
  95.     int fd;
  96.   
  97.     snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
  98.     fd = open(path, O_WRONLY);
  99.     if (fd < 0)
  100.     {
  101.         MSG("Failed to open gpio value for writing!\n");
  102.         return -1;
  103.     }
  104.   
  105.     if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0)
  106.     {
  107.         MSG("Failed to write value!\n");
  108.         return -1;
  109.     }
  110.   
  111.     close(fd);
  112.     return 0;
  113. }
  114. static int gpio_read(int pin)
  115. {
  116.     char path[64];
  117.     char value_str[3];
  118.     int fd;
  119.   
  120.     snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
  121.     fd = open(path, O_RDONLY);
  122.     if (fd < 0)
  123.     {
  124.         MSG("Failed to open gpio value for reading!\n");
  125.         return -1;
  126.     }
  127.   
  128.     if (read(fd, value_str, 3) < 0)
  129.     {
  130.         MSG("Failed to read value!\n");
  131.         return -1;
  132.     }
  133.   
  134.     close(fd);
  135.     return (atoi(value_str));
  136. }
  137. // none表示引脚为输入,不是中断引脚
  138. // rising表示引脚为中断输入,上升沿触发
  139. // falling表示引脚为中断输入,下降沿触发
  140. // both表示引脚为中断输入,边沿触发
  141. // 0-->none, 1-->rising, 2-->falling, 3-->both
  142. static int gpio_edge(int pin, int edge)
  143. {
  144. const char dir_str[] = "none\0rising\0falling\0both";
  145. char ptr;
  146. char path[64];
  147.     int fd;
  148. switch(edge)
  149. {
  150.     case 0:
  151.         ptr = 0;
  152.         break;
  153.     case 1:
  154.         ptr = 5;
  155.         break;
  156.     case 2:
  157.         ptr = 12;
  158.         break;
  159.     case 3:
  160.         ptr = 20;
  161.         break;
  162.     default:
  163.         ptr = 0;
  164. }
  165.   
  166.     snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
  167.     fd = open(path, O_WRONLY);
  168.     if (fd < 0)
  169.     {
  170.         MSG("Failed to open gpio edge for writing!\n");
  171.         return -1;
  172.     }
  173.   
  174.     if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0)
  175.     {
  176.         MSG("Failed to set edge!\n");
  177.         return -1;
  178.     }
  179.   
  180.     close(fd);
  181.     return 0;
  182. }
  183. //GPIO1_17
  184. int main()
  185. {
  186.     int gpio_fd, ret;
  187.     struct pollfd fds[1];
  188.     char buff[10];
  189.     unsigned char cnt = 0;
  190.  
  191.     gpio_unexport(44);
  192.     gpio_unexport(45);
  193.  
  194.     
  195.     //p8_12 init
  196.     gpio_export(44);
  197.     gpio_direction(44, 1);//output out
  198.     gpio_write(44, 1);
  199.     
  200.     //p8_11 init
  201.     gpio_export(45);
  202.     gpio_direction(45, 0);//input in
  203.     gpio_edge(45,2);
  204.     gpio_fd = open("/sys/class/gpio/gpio45/value",O_RDONLY);
  205.     if(gpio_fd < 0)
  206.     {
  207.         MSG("Failed to open value!\n");
  208.         return -1;
  209.     }
  210.     fds[0].fd = gpio_fd;
  211.     fds[0].events = POLLPRI;
  212.     
  213.     while(1)
  214.     {
  215.         ret = read(gpio_fd,buff,10);
  216.         if( ret == -1 )
  217.         MSG("read\n");
  218.     
  219.         ret = poll(fds,1,0);
  220.         if( ret == -1 )
  221.         MSG("poll\n");
  222.         if( fds[0].revents & POLLPRI)
  223.         {
  224.             ret = lseek(gpio_fd,0,SEEK_SET);
  225.             if( ret == -1 )
  226.             MSG("lseek\n");
  227.         
  228.             //gpio_write(44, cnt++%2);
  229.             printf("**********************************\n");
  230.         }
  231.         usleep(5);
  232.     }
  233.     return 0;
  234. }

阅读(661) | 评论(0) | 转发(0) |
0

上一篇:ubuntu16.04安装文件服务

下一篇:DBus 移植

给主人留下些什么吧!~~