从Terminal直接控制IO口的方法如下:- root@beaglebone:~# cd /sys/class/gpio  
 - root@beaglebone:/sys/class/gpio# ls -l  
 - total 0  
 - --w------- 1 root root 4096 Jan  1 00:00 export  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96  
 - --w------- 1 root root 4096 Jan  1 00:00 unexport  
 - root@beaglebone:/sys/class/gpio# echo 44 > export  
 - root@beaglebone:/sys/class/gpio# ls -l  
 - total 0  
 - --w------- 1 root root 4096 Jan  1 00:03 export  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:03 gpio44 -> ../../devices/virtual/gpio/gpio44  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96  
 - --w------- 1 root root 4096 Jan  1 00:00 unexport  
 - root@beaglebone:/sys/class/gpio# cd gpio44  
 - root@beaglebone:/sys/class/gpio/gpio44# ls -l  
 - total 0  
 - -rw-r--r-- 1 root root 4096 Jan  1 00:03 active_low  
 - -rw-r--r-- 1 root root 4096 Jan  1 00:03 direction  
 - -rw-r--r-- 1 root root 4096 Jan  1 00:03 edge  
 - drwxr-xr-x 2 root root    0 Jan  1 00:03 power  
 - lrwxrwxrwx 1 root root    0 Jan  1 00:03 subsystem -> ../../../../class/gpio  
 - -rw-r--r-- 1 root root 4096 Jan  1 00:03 uevent  
 - -rw-r--r-- 1 root root 4096 Jan  1 00:03 value  
 - root@beaglebone:/sys/class/gpio/gpio44# cat direction  
 - in  
 - root@beaglebone:/sys/class/gpio/gpio44# echo out > direction  
 - root@beaglebone:/sys/class/gpio/gpio44# cat direction  
 - out  
 - root@beaglebone:/sys/class/gpio/gpio44# cat value  
 - 0  
 - root@beaglebone:/sys/class/gpio/gpio44# echo 1 > value  
 - root@beaglebone:/sys/class/gpio/gpio44# cat value  
 - 1  
 
 复制代码 解释一下,首先要把某个(本例中是第44个,即P8_12)gpio export一下,变成用户可用的状态,然后目录里就会多出来一个gpio44目录,进入它对相应文件进行读写就可以操作io口了。输入输出是一样的道理。读的话Linux会自动实时更新value文件里的数据,但更新速度有多快暂时还不清楚,高速io操作的话用这种方法感觉不靠谱。不过速度不敏感的话是没问题的。用c程序控制io口,我们当然可以完全照搬上面对文件操作的过程,只不过写成c语言的形式。具体如下:- #include <stdlib.h>  
 - #include <stdio.h>  
 - #include <string.h>  
 - #include <fcntl.h> //define O_WRONLY and O_RDONLY  
 - #define SYSFS_GPIO_DIR "/sys/class/gpio"  
 - #define MAX_BUF 64  
 -   
 - void main()  
 - {  
 -     int fd, len;  
 -     char buf[MAX_BUF];  
 -     char ch;  
 -     int i;  
 -   
 -     //export gpio44  
 -     fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);  
 -     len = snprintf(buf,sizeof(buf),"44");  
 -     write(fd,buf,len);  
 -     close(fd);  
 -   
 -     //set direction  
 -     snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/direction");  
 -     fd = open(buf, O_WRONLY);  
 -     write(fd, "in", 3);  
 -     close(fd);  
 -   
 -     //read and print value 10 times  
 -     for(i=0;i<10;i++)  
 -     {  
 -         snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/value");  
 -         fd = open(buf, O_RDONLY);  
 -         read(fd,&ch,1);  
 -         printf("%c\n",ch);  
 -         close(fd);  
 -         usleep(1000000);  
 -     }  
 - }  
 
 复制代码
这个例子实现了把gpio44引脚设置为输入引脚,然后每秒检测一次输入电平,并将电平(0或1)打印在终端上。
(转自我自己的博客http://blog.csdn.net/wyt2013)BBB GPIO代码
原文:https://www.cnblogs.com/smtx51/p/10819012.html