前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >全志v851s GPIO 应用程序编写

全志v851s GPIO 应用程序编写

作者头像
阿志小管家
发布2024-02-02 18:19:56
1070
发布2024-02-02 18:19:56
举报

1. 查看硬件电路图SCH_Schematic1_2022-11-23 ,查找合适的gpio 作为使用pin

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

电路图pdf路径:Yuzukilizard/Hardware/Schematic/SCH_Schematic1_2022-11-23.pdf

2. 计算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

3. 通过sysfs操作gpio 导出gpio 节点

通过终端操作手动导出:

代码语言:javascript
复制
echo 238 > /sys/class/gpio/export

查看导出的gpio节点

代码语言:javascript
复制
cd /sys/class/gpio

可以看到gpio238

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

代码语言:javascript
复制
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>

#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\n", fd);
    if (fd < 0) {
        printf("gpio/export\n");
        return fd;
    }

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

    close(fd);

    return 0;
}

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

4 .接下来设置gpio 的输出状态,对其设置高低电平

完整code 如下:

代码语言:javascript
复制
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>

#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\n", fd);
    if (fd < 0) {
        printf("gpio/export\n");
        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\n");
        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\n");
        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\n");
        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\n");
        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\n");
        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\n");
    }
    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);
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-02-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档