Navigation

    全志在线开发者论坛

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • 在线文档
    • 社区主页

    v851s gpio 应用程序编写

    V Series
    1
    1
    1300
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      chhjnavy LV 5 last edited by

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

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

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

      46c2f3f5-aaf3-4a11-b282-6d1045d58b0c-image.png

      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);
      }
      
      1 Reply Last reply Reply Quote Share 7
      • 1 / 1
      • First post
        Last post

      Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

      行为准则 | 用户协议 | 隐私权政策