简易实现GPIO应用程序在V85x上编写

描述

GPIO应用程序编写

作者@chhjnavy

查找合适的GPIO

在这里我们选取 GPIOH14(注意目前开发使用这个pin 作为触摸屏的pin脚,需要将触摸屏connect断开) ,因为可以通过排插使用杜邦线将其引出,用于连接别的设备。

触摸屏

 

计算GPIO IO号

在 Linux 系统中,GPIO 通常由 Pinctrl 系统进行管理。Linux 定义了 Pinctrl 框架,统一了各大 SoC 厂商的 Pin 管理方式,避免了各大厂商自行实现自己的 Pin 管理系统,是一个非常有用的功能。

每个gpio 都对应一个IO 号:
- PH14: 7 * 32 + 14 = 238
- PH13: 7 * 32 + 13 = 237
- PH12: 7 * 32 + 12 = 236
- PH11: 7 * 32 + 11 = 235

 

导出GPIO节点

通过终端操作手动导出:

 

echo 238 > /sys/class/gpio/export

 

查看导出的gpio节点

 

cd /sys/class/gpio

 

可以看到gpio238

如果通过应用程序导出,code 如下:

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#define GPIOH14  238


#define  XGPIO_HIGH  1
#define  XGPIO_LOW   0


/****************************************************************
* Constants
****************************************************************/


#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64


/****************************************************************
 * gpio_export
 ****************************************************************/
int gpio_export(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];


    fd = open(SYSFS_GPIO_DIR"/export", O_WRONLY);
    printf("open device ==================fd = %d
", fd);
    if (fd < 0) {
        printf("gpio/export
");
        return fd;
    }


    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);


    close(fd);


    return 0;
}

 

根据IO 号导出gpio 节点是很重要的一个环节,接下来就可以通过gpio 节点,对gpio 进行操作。

 

设置高低电平

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include "gpioAPIs.h"


/****************************************************************
* Constants
****************************************************************/


#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64


/****************************************************************
 * gpio_export
 ****************************************************************/
int gpio_export(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];


    fd = open(SYSFS_GPIO_DIR"/export", O_WRONLY);
    printf("open device ==================fd = %d
", fd);
    if (fd < 0) {
        printf("gpio/export
");
        return fd;
    }


    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);


    close(fd);


    return 0;
}


/****************************************************************
 * gpio_unexport
 ****************************************************************/
int gpio_unexport(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];


    fd = open(SYSFS_GPIO_DIR"/unexport", O_WRONLY);
    if (fd < 0) {
        printf("gpio/export
");
        return fd;
    }


    len = snprintf(buf, sizeof(buf), "%d", gpio);
    write(fd, buf, len);
    close(fd);
    return 0;
}


/****************************************************************
 * gpio_set_dir
 ****************************************************************/
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/direction", gpio);


    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        printf("gpio/direction
");
        return fd;
    }


    if (out_flag)
        write(fd, "out", 4);
    else
        write(fd, "in", 3);


    close(fd);
    return 0;
}


/****************************************************************
 * gpio_set_value
 ****************************************************************/
int gpio_set_value(unsigned int gpio, unsigned int value)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/value", gpio);


    fd = open(buf, O_RDWR );
    if (fd < 0) {
        printf("gpio/set-value
");
        return fd;
    }


    if (value)
        write(fd, "1", 2);
    else
        write(fd, "0", 2);


    close(fd);
    return 0;
}


/****************************************************************
 * gpio_get_value
 ****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
    int fd, len;
    char buf[MAX_BUF];
    char ch;


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/value", gpio);


    fd = open(buf, O_RDWR );
    if (fd < 0) {
        printf("gpio/get-value
");
        return fd;
    }


    read(fd, &ch, 1);


    if (ch != '0') {
        *value = 1;
    } else {
        *value = 0;
    }


    close(fd);
    return 0;
}




/****************************************************************
 * gpio_set_edge
 ****************************************************************/


int gpio_set_edge(unsigned int gpio, char *edge)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/edge", gpio);


    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        printf("gpio/set-edge
");
        return fd;
    }


    write(fd, edge, strlen(edge) + 1);
    close(fd);
    return 0;
}


/****************************************************************
 * gpio_fd_open
 ****************************************************************/


int gpio_fd_open(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];


    len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR"/gpio%d/value", gpio);


    fd = open(buf, O_RDONLY | O_NONBLOCK );
    if (fd < 0) {
        printf("gpio/fd_open
");
    }
    return fd;
}


/****************************************************************
 * gpio_fd_close
 ****************************************************************/


int gpio_fd_close(int fd)
{
    return close(fd);
}


void gpio_init() {


    gpio_export(GPIOH14);
    gpio_set_dir(GPIOH14, 0);
    //gpio_set_edge(GPIOH14, "rising");
}


void gpio_uninit() {
    gpio_unexport(GPIOH14);


}


void mian(void) {
    gpio_init();
    //将gpio238 设定为高电平输出
    gpio_set_value(GPIOH14, XGPIO_HIGH );
    //将gpio238 设定为低电平输出
    gpio_set_value(GPIOH14, XGPIO_LOW);
}

 

  审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分