首页 > 编程语言 >全志v851s使用GPIO应用程序编写

全志v851s使用GPIO应用程序编写

时间:2023-04-21 14:02:04浏览次数:31  
标签:int 全志 len fd v851s gpio GPIO include buf

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 节点

通过终端操作手动导出:

echo 238 > /sys/class/gpio/export

查看导出的gpio节点

cd /sys/class/gpio

可以看到gpio238

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

#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 如下:

#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);
}

标签:int,全志,len,fd,v851s,gpio,GPIO,include,buf
From: https://blog.51cto.com/u_15380233/6212647

相关文章

  • 全志SDK - 3. 系统配置(1)
    1.应用程序放置自己的应用程序,一般有两种放置方式:源码&编译过后的二进制文件(推荐)1.1源码放置源码放置位置一般为:package/allwinner/[app-name]源码是以工程的形式进行放置,所以必须配上makefile和Kconfig等,具体的可以参考该目录下的其他工程,修改一下即可,很简单的!一般......
  • GPIO初始化,及中断代码演示
    1#defineLED2572#defineLED1563#defineKEY2554#defineKEY15456#defineGPIO_DEVICE_IDXPAR_XGPIOPS_0_DEVICE_ID7XGpioPsGpio;89voidGpio_Init(void){10XGpioPs_Config*ConfigPtr;1112......
  • 全志SDK - 2. PC与开发板连接方式
    为了方便对程序调试,我们需要将开发板与PC进行连接,连接方式主要有两种:有线连接:串口、ADB等无线连接:wifi、蓝牙1.有线连接有线连接最常用的是串口和adb,都是非常的方便,唯一的不足是开发板移动距离较小,且需要额外连线!1.1串口使用串口可非常方便的对板子进行各种操作,但是需......
  • 全志v85x 在 eyesee-mpp 中添加一个hello_world sample 的流程
    1.为什么要在eyesee-mpp中添加sample?1)保持整个openwrt应用程序编写的完成性;2)eyesee-mpp中包含了几乎所有全志视频音频模块的sample以及头文件,参考以及头文件调用起来非常方便,而且可以学习各种模块的使用流程;3)可以直接在makemenuconfig中管理应用程序,是否编译;4)不需要将......
  • Raspberry Pi GPIO 图解教程 All In One
    RaspberryPiGPIO图解教程AllInOneRaspberryPi&GPIOGPIO图解GPIOhttps://www.raspberrypi.com/documentation/computers/os.html#gpio-and-the-40-pin-header$pinouthttps://pinout.xyzGPIO(GeneralPurposeIO)SPI(SerialPeripheralInterface)I......
  • 全志SDK - 1. 系统编译
    目录1.准备工作1.1下载SDK1.2SDK解压2.SDK编译2.1系统编译2.2编译boot2.3编译内核2.4编译应用程序2.4.1方法12.4.2方法23.系统烧录1).下载(提取码:708u)并安装PhoenixSuit软件2).选择驱动:(第一次使用时)3).将需要烧录的板子通过串口线(带adb),将电脑和板子进行连接,连接......
  • 学习STM32的第一个外设GPIO(2)——GPIO的输出
    【1】GPIO位结构  【1-1】输入部分为了保护IO引脚,上下各接一个保护二极管,用于限幅输入电压。上面二极管接VDD(3.3V),下面的二极管接VSS(0V)。如果输入电压比3.3V还要高,上面二极管导通,输入电压产生的电流会直接流入VDD而不是内部电流。如果输入电压比0V还要低,相对于VSS电......
  • 关于GPIO部分重映射以及完全重映射的理解
    关于重映射功能中ETR后面的数字编号,可参考下图理解,表示不同的引脚以及是部分重映射还是完全重映射。 以TIM2为例,可以看到默认映射对应的引脚为PA0、PA1、PA2、PA3;部分重映射的引脚为PA15、PB3、PA2、PA3,只改变了部分引脚,所以叫部分重映射;设置为完全重映射时为PA15,PB3,PB10和PB1......
  • STM32]GPIO工作原理详解
     GPIO是通用输入/输出端口的简称,是STM32可控制的引脚。GPIO的引脚与外部硬件设备连接,可实现与外部通讯、控制外部硬件或者采集外部硬件数据的功能。 STM32F103ZET6芯片为144脚芯片,包括7个通用目的的输入/输出口(GPIO)组,分别为GPIOA、GPIOB、GPIOC、GPIOD、GPIOE、GPIOF、GPIOG,同......
  • STM32中,对GPIO_Init(GPIOB, &GPIO_InitStructure)的理解
    //笔者使用的硬件平台为STM32F103ZET6战舰版GPIO_InitTypeDefGPIO_InitStructure;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//指定GPIO-端口配置PB5->LED0GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//指定模式-推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_......