linux 中IO操作GPIO

通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括directionvalue等,direction控制GPIO方向(是控制输出还是输入),而value可控制GPIO输出或获得GPIO输入(通过它设置引脚的值是写入1或0)。文件IO方式操作GPIO,使用到了4个函数openclosereadwrite

 

首先,看看系统中有没有“/sys/class/gpio”这个文件夹。如果没有请在编译内核的时候加入   Device Drivers-> GPIO Support ->/sys/class/gpio/… (sysfs interface)

 

/sys/class/gpio 的使用说明:

编程步骤:

  控制GPIO的目录位于/sys/class/gpio

  /sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号

  /sys/class/gpio/unexport 用于通知系统取消导出

  /sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息,包括每个寄存器控制引脚的起始编号base,寄存器名称,引脚总数 导出一个引脚的操作步骤

  首先计算此引脚编号,引脚编号 = 控制引脚的寄存器基数 + 控制引脚寄存器位数(例如GPIO4_21的引脚编号计算:首先查你使用的开发板的用户手册我的是i.MX 6UltraLite Applications Processor Reference Manual。在GPIO那章节中找到GPIO1_0与/sys/class/gpio/gpiochipX的对应关系,我的是GPIO1_0对应/sys/class/gpio/gpiochip0,所以CPIO4_21对应/sys/class/gpio/gpiochip96,即GPIO4的基数是96,GPIO4_21=96+21=117引脚编号

  /sys/class/gpio/export写入此编号,比如GPIO4_21号引脚,在shell中可以通过以下命令实现,命令成功后生成/sys/class/gpio/gpio117目录,如果没有出现相应的目录,说明此引脚不可导出

  direction文件,定义输入输入方向,可以通过下面命令定义为输出。direction接受的参数:in, out, high, lowhigh/low同时设置方向为输出,并将value设置为相应的1/0

  value文件是端口的数值,为10

 

几个例子:

操作GPIO4_21引脚

1. 导出

/sys/class/gpio# echo 117 > export

2. 设置方向

/sys/class/gpio/gpio117# echo out > direction

3. 查看方向

/sys/class/gpio/gpio117# cat direction

out

4. 设置输出

/sys/class/gpio/gpio117# echo 1 > value

5. 查看输出值

/sys/class/gpio/gpio117# cat value

1

6. 取消导出

/sys/class/gpio# echo 117 > unexport

[objc]  view plain  copy
  1.  文件读写例程:  
  2.   
  3. #include stdlib.h    
  4.   
  5. #include stdio.h    
  6.   
  7. #include string.h  
  8.   
  9. #include unistd.h  
  10.   
  11. #include fcntl.h     
  12. //define O_WRONLY and O_RDONLY    
  13.   
  14.    
  15.   
  16. //芯片复位引脚: CPIO4_21  
  17.   
  18. #define SYSFS_GPIO_EXPORT           "/sys/class/gpio/export"    
  19.   
  20. #define SYSFS_GPIO_RST_PIN_VAL      "117"     
  21.   
  22. #define SYSFS_GPIO_RST_DIR          "/sys/class/gpio/gpio117<span><span style="font-size:16px;line-height:24px;"></span></span>/direction"  
  23.   
  24. #define SYSFS_GPIO_RST_DIR_VAL      "OUT"    
  25.   
  26. #define SYSFS_GPIO_RST_VAL          "/sys/class/gpio/gpio117/value"  
  27.   
  28. #define SYSFS_GPIO_RST_VAL_H        "1"  
  29.   
  30. #define SYSFS_GPIO_RST_VAL_L        "0"  
  31.   
  32.    
  33.   
  34. int main()   
  35.   
  36. {   
  37.   
  38.     int fd;   
  39.   
  40.           
  41.   
  42.          //打开端口/sys/class/gpio# echo 117 > export  
  43.   
  44.          fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);  
  45.   
  46.          if(fd == -1)  
  47.   
  48.          {  
  49.   
  50.                    printf("ERR: Radio hard reset pin open error.\n");  
  51.   
  52.                    return EXIT_FAILURE;  
  53.   
  54.          }  
  55.   
  56.          write(fd, SYSFS_GPIO_RST_PIN_VAL ,sizeof(SYSFS_GPIO_RST_PIN_VAL));   
  57.   
  58.          close(fd);   
  59.   
  60.    
  61.   
  62.          //设置端口方向/sys/class/gpio/gpio117# echo out > direction  
  63.   
  64.          fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);  
  65.   
  66.          if(fd == -1)  
  67.   
  68.          {  
  69.   
  70.                    printf("ERR: Radio hard reset pin direction open error.\n");  
  71.   
  72.                    return EXIT_FAILURE;  
  73.   
  74.          }  
  75.   
  76.          write(fd, SYSFS_GPIO_RST_DIR_VAL, sizeof(SYSFS_GPIO_RST_DIR_VAL));   
  77.   
  78.          close(fd);   
  79.   
  80.    
  81.   
  82.          //输出复位信号: 拉高>100ns  
  83.   
  84.          fd = open(SYSFS_GPIO_RST_VAL, O_RDWR);  
  85.   
  86.          if(fd == -1)  
  87.   
  88.          {  
  89.   
  90.                    printf("ERR: Radio hard reset pin value open error.\n");  
  91.   
  92.                    return EXIT_FAILURE;  
  93.   
  94.          }         
  95.   
  96.          while(1)  
  97.   
  98.          {  
  99.   
  100.                    write(fd, SYSFS_GPIO_RST_VAL_H, sizeof(SYSFS_GPIO_RST_VAL_H));  
  101.   
  102.                    usleep(1000000);  
  103.   
  104.                    write(fd, SYSFS_GPIO_RST_VAL_L, sizeof(SYSFS_GPIO_RST_VAL_L));  
  105.   
  106.                    usleep(1000000);  
  107.   
  108.          }  
  109.   
  110.          close(fd);  
  111.   
  112.    
  113.   
  114.          printf("INFO: Radio hard reset pin value open error.\n");  
  115.   
  116.          return 0;  
  117.   
  118.    
  119.   
  120. }  <span></span>  

猜你喜欢

转载自blog.csdn.net/guoke312/article/details/80803608