GPIO应用编程

开发平台:正点原子阿尔法开发板

PS:可以用cat /sys/kernel/debug/gpio命令查看引脚被占用情况

GPIO应用编程

/sys/class/gpio/export:是一个只写文件,用于导出需要使用的GPIO引脚

/sys/class/gpio/gpiox/:是一个文件夹,在引脚导出后自动在/sys/class/gpio/目录下生成的

/sys/class/gpio/gpiox/active_low:是一个文件,用来控制电平的极性(写1是高电平还是写0是高电平),默认写1是高电平,这个文件不用去管它

/sys/class/gpio/gpiox/direction:是一个文件,用来控制GPIO是输入还是输出,往directionout就是输出引脚,往directionin就是输入引脚

/sys/class/gpio/gpiox/edge:是一个文件,在输入模式下,写edge文件,配置gpio为外部中断引脚

非中断引脚: none
上升沿触发: rising
下降沿触发: falling
边沿触发: both

/sys/class/gpio/gpiox/value:是一个文件,在输出模式下,写该文件表示gpio输出;在输入模式下读该文件表示输入

编程步骤

  1. 把需要使用的GPIO导出,往**/sys/class/gpio/export**文件中写入数字表示导出的引脚
  2. GPIO导出后会在**/sys/class/gpio/目录下生成gpiox(x是引脚号)**的文件夹,对GPIO的各种操作都是在这个文件夹下发生的
  3. 进入gpiox文件夹,主要关注四个文件(active_low、direction、edge、value
    • 配置成输出模式:往directionout,然后往value中写1或0就行
    • 配置成输入模式:往directionin,然后读value文件就行
    • 配置成外部中断模式:在输入模式的基础上,写edge文件

应用编程

程序只进行简单的测试,没有特别规范

./文件名 引脚号 out/in 若是out(写1或0),in没用

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

char gpio_path[100];
int gpio_config(const char * direction,const char * val)
{
    int fd,len;
    char file_path[200];
    char buf[10];
    sprintf(file_path,"%s/direction",gpio_path);
    if(-1==(fd=open(file_path,O_WRONLY))){
        perror("direction file open err\n");
        exit(1);
    }
    len=strlen(direction);
    if(len!=write(fd,direction,len)){
        perror("direction file write err\n");
        exit(1);
    }
    close(fd);
    if(0==strcmp(direction,"out"))
    {
        sprintf(file_path,"%s/value",gpio_path);
        if(-1==(fd=open(file_path,O_WRONLY))){
            perror("value file open err\n");
            exit(1);
        }
        len=strlen(val);
        if(len!=write(fd,val,len)){
            perror("value file write err\n");
            exit(1);
        }
        close(fd);
    }
    else
        if(0==strcmp(direction,"in"))
        {
            sprintf(file_path,"%s/value",gpio_path);
            if(-1==(fd=open(file_path,O_RDONLY))){
                perror("value file open err\n");
                exit(1);
            }
            if(1!=read(fd,buf,1))
            {
                perror("value file read err\n");
                exit(1);
            }
            printf("read %s \n",buf);
            close(fd);
        }
    
    exit(0);
}
int main(int argc,char * argv[])
{
    int fd,len;
    if(argc != 4){
        perror("argc err\n");
        exit(1);
    }
    sprintf(gpio_path,"/sys/class/gpio/gpio%s",argv[1]);
    if(0 != access(gpio_path,F_OK)){
        if((fd=open("/sys/class/gpio/export",O_WRONLY))==-1){
            perror("open err\n");
            exit(1);
        }
        len=strlen(argv[1]);
        if(len != write(fd,argv[1],len)){
            perror("write err\n");
            exit(1);
        }
        close(fd);
    }
    if(0!=gpio_config(argv[2],argv[3])){
        printf("gpio_config err\n");
        exit(1);
    }

    exit(0);
}

遇到的问题

  1. 把gpio0配置成输出模式,然后往value文件中写1后,用cat value发现value中的值没有发生变化,还是0,但是把该引脚接一个led发现led能亮,==不知哪里出现了问题?==输入模式同理

    echo "out" > direction 	#配置成输出模式
    echo "1" > value		#输出1
    cat value				#看value文件中值是否发生变化
    

能亮,==不知哪里出现了问题?==输入模式同理

echo "out" > direction 	#配置成输出模式
echo "1" > value		#输出1
cat value				#看value文件中值是否发生变化
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值