linux下,应用程序如何操作GPIO(BBB板子中的GPIO为例)
cd /sys/class/gpio
使用P8.12引脚
echo 44 > export 
cd gpio44
cat direction


1. 导出
/sys/class/gpio# echo 44 > export
2. 设置方向
/sys/class/gpio/gpio44# echo out > direction
3. 查看方向
/sys/class/gpio/gpio44# cat direction
4. 设置输出
/sys/class/gpio/gpio44# echo 1 > value
5. 查看输出值
/sys/class/gpio/gpio44# cat value
6. 取消导出

/sys/class/gpio# echo 44 > unexport


#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>


#define SYSFS_GPIO_EXPORT           "/sys/class/gpio/export"  
#define SYSFS_GPIO_RST_PIN8_11_VAL   "45"  
#define SYSFS_GPIO_RST_PIN8_12_VAL   "44"  


#define SYSFS_GPIO_RST_DIR          "/sys/class/gpio/gpio44/direction"
#define SYSFS_GPIO_RST_DIR_PIN8_11  "/sys/class/gpio/gpio45/direction"
#define SYSFS_GPIO_RST_DIR_PIN8_12  "/sys/class/gpio/gpio44/direction"

#define SYSFS_GPIO_RST_DIR_VAL_OUT  "out"
#define SYSFS_GPIO_RST_DIR_VAL_IN   "in"


#define SYSFS_GPIO_RST_VAL_OUT      "/sys/class/gpio/gpio44/value"
#define SYSFS_GPIO_RST_VAL_IN       "/sys/class/gpio/gpio45/value"


#define SYSFS_GPIO_RST_VAL_H        "1"
#define SYSFS_GPIO_RST_VAL_L        "0"
 
int main() 
{ 
		char value_str[3];	

    	int fd; 
    	int fd_in;
    	int gpio_in_val=0;
    	static const char dir_str[] = "in\0out";  
    	int i=0;
    	//for(i=0;i<2;i++)
    	//{
    		printf(
"%s\n",&dir_str[0]);
    		printf(
"%s\n",&dir_str[3]);
    	//}
        
         //打开端口/sys/class/gpio# echo 44 > export
         fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
         if(fd == -1)
         {
                   printf("ERR: Radio hard reset pin open error.\n");
                   return EXIT_FAILURE;
         }
          printf("ddd,%d\n",sizeof(SYSFS_GPIO_RST_PIN8_12_VAL));
          if( write(fd, SYSFS_GPIO_RST_PIN8_12_VAL ,sizeof(SYSFS_GPIO_RST_PIN8_12_VAL))<0)
          printf("write error!\n");
         
         close(fd); 


         //打开端口/sys/class/gpio# echo 45 > export
         fd_in = open(SYSFS_GPIO_EXPORT, O_WRONLY);
         if(fd_in == -1)
         {
                   printf("ERR: Radio hard reset pin open error.\n");
                   return EXIT_FAILURE;
         }
         write(fd_in, SYSFS_GPIO_RST_PIN8_11_VAL ,sizeof(SYSFS_GPIO_RST_PIN8_11_VAL)); 
         close(fd_in); 

 
         //设置端口方向/sys/class/gpio/gpio44# echo out > direction
         fd = open(SYSFS_GPIO_RST_DIR_PIN8_12, O_WRONLY);
         if(fd == -1)
         {
                   printf("ERR: Radio hard reset pin direction open error.\n");
                   return EXIT_FAILURE;
         }
        
         write(fd, SYSFS_GPIO_RST_DIR_VAL_OUT, sizeof(SYSFS_GPIO_RST_DIR_VAL_OUT)); 
         close(fd); 


         
         //设置端口方向/sys/class/gpio/gpio45# echo in > direction
         fd_in = open(SYSFS_GPIO_RST_DIR_PIN8_11, O_WRONLY);
         if(fd_in == -1)
         {
                   printf("ERR: Radio hard reset pin direction open error.\n");
                   return EXIT_FAILURE;
         }
         write(fd_in, SYSFS_GPIO_RST_DIR_VAL_IN, sizeof(SYSFS_GPIO_RST_DIR_VAL_IN)); 
         close(fd_in); 

 
         //输出复位信号: 拉高>100ns
         fd = open(SYSFS_GPIO_RST_VAL_OUT, O_RDWR);
         if(fd == -1)
         {
                   printf("ERR: Radio hard reset pin value open error.\n");
                   return EXIT_FAILURE;
         } 

         
         //输出复位信号: 拉高>100ns
         fd_in= open(SYSFS_GPIO_RST_VAL_IN, O_RDWR);
         if(fd_in == -1)
         {
                   printf("ERR: Radio hard reset pin value open error.\n");
                   return EXIT_FAILURE;
         }       

		 

         while(1)
         {
                  write(fd, SYSFS_GPIO_RST_VAL_H, sizeof(SYSFS_GPIO_RST_VAL_H));
                  lseek(fd_in, 0, SEEK_SET);//读完之后,要把文件指针移动到文件开始位置,否则读不到数据
                  if(read(fd_in,value_str,sizeof(value_str))>0)
                  printf("H1,%d\n",atoi(value_str));
                  memset(value_str,0,sizeof(value_str));
                  usleep(1000000);
                  write(fd, SYSFS_GPIO_RST_VAL_L, sizeof(SYSFS_GPIO_RST_VAL_L));
                
               	  printf("*****\n");
               	  lseek(fd_in, 0, SEEK_SET);
			      if(read(fd_in,value_str,sizeof(value_str))>0)
			      printf("H2,%d\n",atoi(value_str));
			      usleep(1000000);
               
         }
         close(fd);
         close(fd_in);
 
         printf("INFO: Radio hard reset pin value open error.\n");
         return 0;
 
}  


阅读更多
个人分类: c linux
想对作者说点什么? 我来说一句

有关访问GPIO的简单描述

2011年08月29日 65KB 下载

没有更多推荐了,返回首页

不良信息举报

linux下,应用程序如何操作GPIO(BBB板子中的GPIO为例)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭