Nvidia Jetson Nano学习笔记--使用C语言实现GPIO 输入输出

系列文章目录

一.实现PC和Nvidia远程连接
二.Nvidia 交叉编译程序
三.Nvidia 使用命令行实现GPIO控制



前言

在上一文中采用命令行实现了GPIO控制,本次将使用C语言来实现GPIO输入输出功能,编辑器为VScode。

一、思路

回顾一下上文中实现GPIO口控制的流程。

1.进入到Nano的 /sys/class/gpio 目录下。

cd /sys/classs/gpio

2.导出目标IO口的 GPIO文件(假设 目标IO口编号为216,实际对应Nano上的第7号管脚)

echo 216 > expoet

3.进入所导出的gpio216文件夹。

cd /sys/class/gpio/gpio216

4.如果需要该引脚输出高低电平则

//设置direction为输出模式,默认为输入模式
echo "out" > direction

//输出高电平,
echo 1 > value

//输出低电平
echo 0 > value

//active_low 级性为0,表示 1--高电平,0--低电平, 极性为1则相反

其实使用C语言来实现上述功能,就是将上述的步骤写成代码来实现。

二、具体实现代码

1.main函数–GPIO输出

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

static char gpio_path[100];
static int gpio_config(const char *attr, const char *val);

int main(int argc, char *argv[]) //传入参数格式: ./gpio_out 216 1 ;216号输出高电平
{
    /* 校验传参  传入的参数必须为3个*/
    if (3 != argc) {
        fprintf(stderr, "usage: %s <gpio> <value>\n", argv[0]); //stderr标准错误
        exit(-1);
    }

    /* 判断指定编号的GPIO是否导出 */
    //这一步是生成目标IO口的文件路径,并存入gpio__path中
    sprintf(gpio_path, "/sys/class/gpio/gpio%s", argv[1]); 

    if (access(gpio_path, F_OK))  //判断标IO口的文件路径是否存在 不存在则需要生成文件路径
    { 

        int fd;
        int len;
				//等同于echo io口编号 > expoet 
        if (0 > (fd = open("/sys/class/gpio/export", O_WRONLY))) {
            perror("open error");
            exit(-1);
        }

        len = strlen(argv[1]);
        if (len != write(fd, argv[1], len)) {//导出gpio
            perror("write error");
            close(fd);
            exit(-1);
        }

        close(fd);  //关闭文件
    }

		//下面三条语句是配置IO口状态
    /* 配置为输出模式 */
    if (gpio_config("direction", "out")) //echo "int" > direction
        exit(-1);

    /* 极性设置 */
    if (gpio_config("active_low", "0")) //echo "0" > active_low
        exit(-1);

    /* 控制GPIO输出高低电平 */
    if (gpio_config("value", argv[2])) //echo argc[2] > value
        exit(-1);

    /* 退出程序 */
    exit(0);
}

2.main函数–GPIO输入

输入与输出的区别在于 设置direction为in(默认),在配置完状态后需要阅读一次value的值,在将值打印出来

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

static char gpio_path[100];
static int gpio_config(const char *attr, const char *val);

int main(int argc, char *argv[])
{
    char file_path[100];
    char val;
    int fd;

    /* 校验传参 */
    if (2 != argc) {
        fprintf(stderr, "usage: %s <gpio>\n", argv[0]);
        exit(-1);
    }

    /* 判断指定编号的GPIO是否导出 */
    sprintf(gpio_path, "/sys/class/gpio/gpio%s", argv[1]);

    if (access(gpio_path, F_OK)) {//如果目录不存在 则需要导出

        int len;

        if (0 > (fd = open("/sys/class/gpio/export", O_WRONLY))) {
            perror("open error");
            exit(-1);
        }

        len = strlen(argv[1]);
        if (len != write(fd, argv[1], len)) {//导出gpio
            perror("write error");
            close(fd);
            exit(-1);
        }

        close(fd);  //关闭文件
    }

    /* 配置为输入模式 */
    if (gpio_config("direction", "in"))//echo "in" > direction
        exit(-1);

    /* 极性设置 */
    if (gpio_config("active_low", "0"))//echo "0" > active_low
        exit(-1);

    /* 配置为非中断方式 */
    if (gpio_config("edge", "none")) //echo "none" > edge
        exit(-1);

    /* 读取GPIO电平状态 */
    sprintf(file_path, "%s/%s", gpio_path, "value");

    if (0 > (fd = open(file_path, O_RDONLY))) {
        perror("open error");
        exit(-1);
    }

    if (0 > read(fd, &val, 1)) {
        perror("read error");
        close(fd);
        exit(-1);
    }

    printf("value: %c\n", val);

    /* 退出程序 */
    close(fd);
    exit(0);
}

3.配置函数

static int gpio_config(const char *attr, const char *val)
{
    char file_path[100];
    int len;
    int fd;
    
		//得到文件路径(direction/dege/active_low...)
    sprintf(file_path, "%s/%s", gpio_path, attr); 
    
    //打开文件(direction/dege/active_low...)
    if (0 > (fd = open(file_path, O_WRONLY))) 
    {
        perror("open error");
        return fd;
    }
		
		//写文件(direction/dege/active_low...)
    len = strlen(val);
    if (len != write(fd, val, len)) //write file
    {
        perror("write error");
        close(fd);
        return -1;
    }

    close(fd);  //关闭文件
    return 0;
}

总结

通过如上的步骤便可以通过C语言来实现Nano的GPIO控制。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会武功不懂江湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值