ubuntu GPIO口操作

这是一个使用C++在Linux系统下通过文件I/O方式控制NVIDIA Jetson Xavier NX GPIO的示例代码。代码展示了如何打开、关闭GPIO,设置GPIO方向以及读写GPIO值,具体应用为通过GPIO-09控制蜂鸣器的开关。
摘要由CSDN通过智能技术生成
h文件
/*
    NVIDIA Jetson Xavier NX 控制GPIO
    在linux系统中以文件io的方式控制GPIO示例,开发板为NVIDIA Jetson Xavier NX,其它公司的开发板也可使用。

    此代码功能:GPIO-09接入蜂鸣器,高电平开启,低电平关闭。
*/

#ifndef NVIDIAGPIO_H
#define NVIDIAGPIO_H

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>

using namespace std;

#define ON "1"              //开启
#define OFF "0"             //关闭
#define BUZZER "436"        //控制蜂鸣器引脚
#define DIRECTION_OUT "out"     //方向为输出
#define DIRECTION_IN "in"     //方向为输出

class NvidiaGpio
{
public:
    NvidiaGpio();
    ~NvidiaGpio();

    int openGpio(const char *port); //导入GPIO
    int closeGpio(const char *port); //导出GPIO

    int setGpioDirection(const char *port, const char *direction); //设置GPIO输入/输出模式

    int writeGpioValue(const char *port, const char *value); //设置GPIO值
    int readGpioValue(const char *port); //读取GPIO值
};

#endif // NVIDIAGPIO_H

cpp文件
#include "nvidiagpio.h"

NvidiaGpio::NvidiaGpio()
{

}

NvidiaGpio::~NvidiaGpio()
{

}

int NvidiaGpio::openGpio(const char *port)
{
    int fd = -1;
    const char *path = "/sys/class/gpio/export";

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to open gpio! ");
        return -1;
    }

    write(fd, port, sizeof(port));
    close(fd);

    return 0;
}

int NvidiaGpio::closeGpio(const char *port)
{
    int fd = -1;
    const char *path = "/sys/class/gpio/unexport";

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to close gpio! ");
        return -1;
    }

    write(fd, port, sizeof(port));
    close(fd);

    return 0;
}

int NvidiaGpio::setGpioDirection(const char *port, const char *direction)
{
    int fd = -1;
    char path[40] = {0};
    sprintf(path, "/sys/class/gpio/gpio%s/direction", port);

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to set GPIO dirention! ");
        return -1;
    }

    write(fd, direction, sizeof(direction));
    close(fd);

    return 0;
}

int NvidiaGpio::writeGpioValue(const char *port, const char *value)
{
    int fd = -1;
    char path[40] = {0};
    sprintf(path, "/sys/class/gpio/gpio%s/value", port);

    if((fd = open(path, O_WRONLY)) == -1) {
        perror("Failed to set GPIO value! ");
        return -1;
    }

    write(fd, value, sizeof(value));
    close(fd);

    return 0;
}

int NvidiaGpio::readGpioValue(const char *port)
{
    int fd = -1;
    int value = 0;
    char path[40] = {0};
    sprintf(path, "/sys/class/gpio/gpio%s/value", port);

    if((fd = open(path, O_RDONLY)) == -1) {
        perror("Failed to set GPIO value! ");
        return -1;
    }

    char tmp[2] = {0};
    read(fd, tmp, 1);
    close(fd);

    value = atoi(tmp);

    return value;
}

main文件
#include <iostream>
#include "nvidiagpio.h"

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    NvidiaGpio *gpio = new NvidiaGpio();

    if(gpio->openGpio(BUZZER) != 0) { //打开gpio
        exit(1);
    }

    usleep(10*1000);

    if(gpio->setGpioDirection(BUZZER, DIRECTION_OUT) != 0) { // 设置GPIO方向
        gpio->closeGpio(BUZZER);
        exit(1);
    }

    usleep(10*1000);

    if(gpio->writeGpioValue(BUZZER, ON) != 0) { //设置高电平为输出,开启蜂鸣器
        gpio->closeGpio(BUZZER);
        exit(1);
    }

    int ret = 0;
    ret = gpio->readGpioValue(BUZZER);
    cout<<"get gpio value: "<<ret<<endl;

    sleep(1);

    if(gpio->writeGpioValue(BUZZER, OFF) != 0) { //设置低电平为输出,关闭蜂鸣器
        gpio->closeGpio(BUZZER);
        exit(1);
    }

    ret = gpio->readGpioValue(BUZZER);
    cout<<"get gpio value: "<<ret<<endl;

    gpio->closeGpio(BUZZER);

    return 0;
}

注意GPIO文件位置

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值