linux中通用GPIO接口的操作

来源:互联网 发布:linux用户默认目录 编辑:程序博客网 时间:2024/04/20 15:33


 转载地址:http://blog.csdn.net/cjyusha/article/details/50418862


在linux嵌入式设备开发中,对GPIO的操作是最常用的,在一般的情况下,一般都有对应的驱动,应用程序打开对应的驱动,然后通过ioctl函数就可以对其进行操作。在linux中,其实有一个通用的GPIO驱动,应用通过调用文件的形式来进行读写操作,这个文件是/sys/class/gpio,本文就简单的来讲解一下通用GPIO接口的读写。


一、 以echo的形式调用system函数进行操作,这种形式编程比较简单,结构比较清晰,如下:

[cpp] view plain copy print?
  1. void set_gpio64_low(void)  
  2. {     
  3.     system("echo 64 > /sys/class/gpio/export");  
  4.     system("echo out > /sys/class/gpio/gpio64/direction");  
  5.     system("echo 0 > /sys/class/gpio/gpio64/value");  
  6. }  

只要完成三个步骤,就可以了

[cpp] view plain copy print?
  1. void set_gpio64_high(void)  
  2. {     
  3.     system("echo 64 > /sys/class/gpio/export");  
  4.     system("echo out > /sys/class/gpio/gpio64/direction");  
  5.     system("echo 1 > /sys/class/gpio/gpio64/value");  
  6. }  

二、 通过文件的形式来调用

[cpp] view plain copy print?
  1. int set_io_value_high(int gpio)  
  2. {  
  3.     FILE *fp;  
  4.     char buffer[10];  
  5.     int value;  
  6.     char s[50]="";  
  7.     char s1[50]="";  
  8.     if ((fp = fopen("/sys/class/gpio/export""w")) == NULL)   
  9.     {  
  10.         printf("Cannot open export file.\n");  
  11.         return -1;  
  12.     }  
  13.     fprintf(fp, "%d", gpio);  
  14.     fclose(fp);  
  15.   
  16.     sprintf(s,"/sys/class/gpio/gpio%d/direction",gpio);  
  17.     if ((fp = fopen(s, "rb+")) == NULL)   
  18.     {  
  19.         printf("Cannot open %s.\n",s);  
  20.         return -1;  
  21.     }  
  22.     fprintf(fp, "out");  
  23.     fclose(fp);  
  24.           
  25.     sprintf(s1,"/sys/class/gpio/gpio%d/value",gpio);  
  26.   
  27.     if ((fp = fopen(s1, "rb+")) == NULL)   
  28.     {  
  29.         printf("Cannot open %s.\n",s1);  
  30.         return -1;  
  31.     }  
  32.     strcpy(buffer,"1");  
  33.     fwrite(buffer, sizeof(char), sizeof(buffer) - 1, fp);         
  34.     fclose(fp);  
  35.     return 1;  
  36. }  

[cpp] view plain copy print?
  1. int set_io_value_low(int gpio)  
  2. {  
  3.     FILE *fp;  
  4.     char buffer[10];  
  5.     int value;  
  6.     char s[50]="";  
  7.     char s1[50]="";  
  8.     if ((fp = fopen("/sys/class/gpio/export""w")) == NULL)   
  9.     {  
  10.         printf("Cannot open export file.\n");  
  11.         return -1;  
  12.     }  
  13.     fprintf(fp, "%d", gpio);  
  14.     fclose(fp);  
  15.   
  16.     sprintf(s,"/sys/class/gpio/gpio%d/direction",gpio);  
  17.     if ((fp = fopen(s, "rb+")) == NULL)   
  18.     {  
  19.         printf("Cannot open %s.\n",s);  
  20.         return -1;  
  21.     }  
  22.     fprintf(fp, "out");  
  23.     fclose(fp);  
  24.           
  25.     sprintf(s1,"/sys/class/gpio/gpio%d/value",gpio);  
  26.   
  27.     if ((fp = fopen(s1, "rb+")) == NULL)   
  28.     {  
  29.         printf("Cannot open %s.\n",s1);  
  30.         return -1;  
  31.     }  
  32.     strcpy(buffer,"0");  
  33.     fwrite(buffer, sizeof(char), sizeof(buffer) - 1, fp);         
  34.     fclose(fp);  
  35.     return 1;  
  36.   
  37. }  
0 0
原创粉丝点击