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

12 篇文章 0 订阅
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;
 
}  


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值