使用Beaglebone Black的IO口

来源:互联网 发布:linux查看dns配置 编辑:程序博客网 时间:2024/05/16 00:31

从Terminal直接控制IO口的方法如下:

  1. root@beaglebone:~# cd /sys/class/gpio  
  2. root@beaglebone:/sys/class/gpio# ls -l  
  3. total 0  
  4. --w------- 1 root root 4096 Jan  1 00:00 export  
  5. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0  
  6. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32  
  7. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64  
  8. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96  
  9. --w------- 1 root root 4096 Jan  1 00:00 unexport  
  10. root@beaglebone:/sys/class/gpio# echo 44 > export  
  11. root@beaglebone:/sys/class/gpio# ls -l  
  12. total 0  
  13. --w------- 1 root root 4096 Jan  1 00:03 export  
  14. lrwxrwxrwx 1 root root    0 Jan  1 00:03 gpio44 -> ../../devices/virtual/gpio/gpio44  
  15. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0  
  16. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32  
  17. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64  
  18. lrwxrwxrwx 1 root root    0 Jan  1 00:00 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96  
  19. --w------- 1 root root 4096 Jan  1 00:00 unexport  
  20. root@beaglebone:/sys/class/gpio# cd gpio44  
  21. root@beaglebone:/sys/class/gpio/gpio44# ls -l  
  22. total 0  
  23. -rw-r--r-- 1 root root 4096 Jan  1 00:03 active_low  
  24. -rw-r--r-- 1 root root 4096 Jan  1 00:03 direction  
  25. -rw-r--r-- 1 root root 4096 Jan  1 00:03 edge  
  26. drwxr-xr-x 2 root root    0 Jan  1 00:03 power  
  27. lrwxrwxrwx 1 root root    0 Jan  1 00:03 subsystem -> ../../../../class/gpio  
  28. -rw-r--r-- 1 root root 4096 Jan  1 00:03 uevent  
  29. -rw-r--r-- 1 root root 4096 Jan  1 00:03 value  
  30. root@beaglebone:/sys/class/gpio/gpio44# cat direction  
  31. in  
  32. root@beaglebone:/sys/class/gpio/gpio44# echo out > direction  
  33. root@beaglebone:/sys/class/gpio/gpio44# cat direction  
  34. out  
  35. root@beaglebone:/sys/class/gpio/gpio44# cat value  
  36. 0  
  37. root@beaglebone:/sys/class/gpio/gpio44# echo 1 > value  
  38. root@beaglebone:/sys/class/gpio/gpio44# cat value  
  39. 1  

解释一下,首先要把某个(本例中是第44个)gpio export一下,变成用户可用的状态,然后目录里就会多出来一个gpio44目录,进入它对相应文件进行读写就可以操作io口了。输入输出是一样的道理。读的话Linux会自动实时更新value文件里的数据,但更新速度有多快暂时还不清楚,高速io操作的话用这种方法感觉不靠谱。不过速度不敏感的话是没问题的。用c程序控制io口,我们当然可以完全照搬上面对文件操作的过程,只不过写成c语言的形式。具体如下:

  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <fcntl.h> //define O_WRONLY and O_RDONLY  
  5. #define SYSFS_GPIO_DIR "/sys/class/gpio"  
  6. #define MAX_BUF 64  
  7.   
  8. void main()  
  9. {  
  10.     int fd, len;  
  11.     char buf[MAX_BUF];  
  12.     char ch;  
  13.     int i;  
  14.   
  15.     //export gpio44  
  16.     fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);  
  17.     len = snprintf(buf,sizeof(buf),"44");  
  18.     write(fd,buf,len);  
  19.     close(fd);  
  20.   
  21.     //set direction  
  22.     snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/direction");  
  23.     fd = open(buf, O_WRONLY);  
  24.     write(fd, "in", 3);  
  25.     close(fd);  
  26.   
  27.     //read and print value 10 times  
  28.     for(i=0;i<10;i++)  
  29.     {  
  30.         snprintf(buf,sizeof(buf),SYSFS_GPIO_DIR"/gpio44/value");  
  31.         fd = open(buf, O_RDONLY);  
  32.         read(fd,&ch,1);  
  33.         printf("%c\n",ch);  
  34.         close(fd);  
  35.         usleep(1000000);  
  36.     }  
  37. }  
这个例子实现了把gpio44引脚设置为输入引脚,然后每秒检测一次输入电平,并将电平(0或1)打印在终端上。
0 0
原创粉丝点击