Navigation

    全志在线开发者论坛

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

    用用D1上的GPIO(封装好的函数)

    D1系列-RISC-V
    1
    1
    369
    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.
    • BedRock
      BedRock LV 6 last edited by

      由于最近要使用GPIO,所以今天就把玩了一下D1的GPIO,在使用的时候需要注意,GPIO是否可用,有没有被其他模块使用,可以下载原理图进行查看,本着扩展IO肯定没模块用的心理,就直接上扩展IO了。

      话不多说直接上代码,晚点发GitHub。

      #ifndef MOTOR_H
      #define MOTOR_H
      
      
      #include <stdio.h>
      #include <stdlib.h>
      #include <unistd.h>
      #include <sys/types.h>    
      #include <sys/stat.h>    
      #include <fcntl.h>
      
      #define GPIO_PATH "/sys/class/gpio/gpio%d/value"
      #define GPIO_FUNCTION_PATH "/sys/class/gpio/gpio%d/direction"
      
      
      static int GPIO_Point_IN1_fd;
      static int GPIO_Point_IN2_fd;
      static int GPIO_Point_IN3_fd;
      static int GPIO_Point_IN4_fd;
      
      
      static unsigned char phasecw[4] = {0x08, 0x04, 0x02, 0x01};
      
      
      //
      
      #define GPIO_Point_IN1 2021
      #define GPIO_Point_IN2 2022
      #define GPIO_Point_IN3 2023
      #define GPIO_Point_IN4 2024
      
      #define GPIO_WRITEPIN(Point,value) (write(Point,value,1))
      
      #define delay_ms(x)  (usleep(x*1000))
      
      #define GPIO_Point_IN1_Set write(GPIO_Point_IN1_fd, "1", 1)
      #define GPIO_Point_IN2_Set write(GPIO_Point_IN2_fd, "1", 1)
      #define GPIO_Point_IN3_Set write(GPIO_Point_IN3_fd, "1", 1)
      #define GPIO_Point_IN4_Set write(GPIO_Point_IN4_fd, "1", 1)
      
      #define GPIO_Point_IN1_ReSet write(GPIO_Point_IN1_fd, "0", 1)
      #define GPIO_Point_IN2_ReSet write(GPIO_Point_IN2_fd, "0", 1)
      #define GPIO_Point_IN3_ReSet write(GPIO_Point_IN3_fd, "0", 1)
      #define GPIO_Point_IN4_ReSet write(GPIO_Point_IN4_fd, "0", 1)
      
      
      
      int Motor_Init();
      
      void GPIO_WrietPin(int GPIO_Point,int GPIO_Value);
      void MotorCW( void );
      #endif
      
      
      #include "Motor.h"
      
      static int export_fd, direction_fd, gpiovalue_fd;
      
      static int Motor_GPIO_Init();
      static int Motor_GPIO_Function();
      static int Motor_GPIO_File_FOK();
      
      
      
      int Motor_Init(){
          //1、检查文件是否存在 4 个 GPIO 都需要检查.
          if(-1 == Motor_GPIO_File_FOK()){
              printf("Motor_GPIO_File creat Faild\n");
              return -1;
          }
          //2、设置IO功能为 out 
          if(-1 == Motor_GPIO_Function()){
              printf("Motor_GPIO_Function set Faild \n");
              return -1;
          }
          //2、IO file open return fd*
          if(-1 == Motor_GPIO_Init()){
              printf("Motor_GPIO_File Open Faild \n");
              return -1;
          }
          return 1;
      }
      void MotorCW( void )
      {
          unsigned char i;
          unsigned char temp = 0;
          for( i = 0; i < 4; i++ )
          {
              temp = phasecw[i];
      
              // LD = ( temp >> 3 ) & 0x01;							//取bit4的值
              // LC = ( temp >> 2 ) & 0x01;
              // LB = ( temp >> 1 ) & 0x01;
              // LA = ( temp >> 0 ) & 0x01;							//取bit0的值
              GPIO_WrietPin(GPIO_Point_IN1, ( temp >> 3 ) & 0x01);
              GPIO_WrietPin(GPIO_Point_IN2, ( temp >> 2 ) & 0x01);
              GPIO_WrietPin(GPIO_Point_IN3, ( temp >> 1 ) & 0x01);
              GPIO_WrietPin(GPIO_Point_IN4, ( temp >> 0 ) & 0x01);
      
              delay_ms(2); 										
          }
      }
      
      void GPIO_WrietPin(int GPIO_Point,int GPIO_Value){
          if(!(GPIO_Value == 0 || GPIO_Value == 1)){
              printf("GPIO_Value Faild %d \n",GPIO_Value);
              return -1;
          }
          switch(GPIO_Point){
              case GPIO_Point_IN1:
                  //GPIO_WRITEPIN(GPIO_Point_IN1_fd,GPIO_Value);
                  if(GPIO_Value) GPIO_Point_IN1_Set;
                  else GPIO_Point_IN1_ReSet;
                  break;
              case GPIO_Point_IN2:
                  //GPIO_WRITEPIN(GPIO_Point_IN2_fd,GPIO_Value);
                  if(GPIO_Value) GPIO_Point_IN2_Set;
                  else GPIO_Point_IN2_ReSet;
                  break;
              case GPIO_Point_IN3:
                  //GPIO_WRITEPIN(GPIO_Point_IN3_fd,GPIO_Value);
                  if(GPIO_Value) GPIO_Point_IN3_Set;
                  else GPIO_Point_IN3_ReSet;
                  break;
              case GPIO_Point_IN4:
                  //GPIO_WRITEPIN(GPIO_Point_IN4_fd,GPIO_Value);
                  if(GPIO_Value) GPIO_Point_IN4_Set;
                  else GPIO_Point_IN4_ReSet;
                  break;
              default:
                  break;
          }
      
      }
      
      static int Motor_GPIO_File_FOK(){
      
          char gpio_file_path[40] = {0}; 
          char gpio_Point_File_Num[4] = {0};
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN1);
          sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN1);
          if(access(gpio_file_path,F_OK)){
              printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN1);
      
              export_fd = open("/sys/class/gpio/export", O_WRONLY);
              if(-1 == export_fd)
              {
                      printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                      return -1;
              }
              if(-1 == write(export_fd, gpio_Point_File_Num, 4))
              {
                      printf("write file operation error\r\n");
                      return -1;
              }
              close(export_fd);
          }
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN2);
          sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN2);
          if(access(gpio_file_path,F_OK)){
              printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN2);
      
              export_fd = open("/sys/class/gpio/export", O_WRONLY);
              if(-1 == export_fd)
              {
                      printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                      return -1;
              }
              if(-1 == write(export_fd, gpio_Point_File_Num, 4))
              {
                      printf("write file operation error\r\n");
                      return -1;
              }
              close(export_fd);
          }
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN3);
          sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN3);
          if(access(gpio_file_path,F_OK)){
              printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN3);
      
              export_fd = open("/sys/class/gpio/export", O_WRONLY);
              if(-1 == export_fd)
              {
                      printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                      return -1;
              }
              if(-1 == write(export_fd, gpio_Point_File_Num, 4))
              {
                      printf("write file operation error\r\n");
                      return -1;
              }
              close(export_fd);
          }
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN4);
          sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN4);
          if(access(gpio_file_path,F_OK)){
              printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN4);
      
              export_fd = open("/sys/class/gpio/export", O_WRONLY);
              if(-1 == export_fd)
              {
                      printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                      return -1;
              }
              if(-1 == write(export_fd, gpio_Point_File_Num, 4))
              {
                      printf("write file operation error\r\n");
                      return -1;
              }
              close(export_fd);
          }
          return 1;
      }
      
      
      static int Motor_GPIO_Function(){
          char GPIO_Function_Path[40] = {0};
          sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN1);
          direction_fd = open(GPIO_Function_Path, O_WRONLY);
          if(-1 == direction_fd)
          {
              printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
              
              return -1;
          }
          if(-1 == write(direction_fd, "out", sizeof("out")))
          {
              printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
              close(direction_fd);
              return -1;
          }
          close(direction_fd);
      
           sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN2);
          direction_fd = open(GPIO_Function_Path, O_WRONLY);
          if(-1 == direction_fd)
          {
              printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
              
              return -1;
          }
          if(-1 == write(direction_fd, "out", sizeof("out")))
          {
              printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
              close(direction_fd);
              return -1;
          }
          close(direction_fd);
      
           sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN3);
          direction_fd = open(GPIO_Function_Path, O_WRONLY);
          if(-1 == direction_fd)
          {
              printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
              
              return -1;
          }
          if(-1 == write(direction_fd, "out", sizeof("out")))
          {
              printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
              close(direction_fd);
              return -1;
          }
          close(direction_fd);
      
           sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN4);
          direction_fd = open(GPIO_Function_Path, O_WRONLY);
          if(-1 == direction_fd)
          {
              printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
              
              return -1;
          }
          if(-1 == write(direction_fd, "out", sizeof("out")))
          {
              printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
              close(direction_fd);
              return -1;
          }
          close(direction_fd);
      
          return 1;
      }
      // int GPIO_Point_IN1_fd;
      // int GPIO_Point_IN2_fd;
      // int GPIO_Point_IN3_fd;
      // int GPIO_Point_IN4_fd;
      static int Motor_GPIO_Init(){
      
          char gpio_file_path[40] = {0}; 
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN1);
          GPIO_Point_IN1_fd = open(gpio_file_path, O_WRONLY);  //RDWR
          if(-1 == GPIO_Point_IN1_fd)
          {
              printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
              return -1;
          }
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN2);
          GPIO_Point_IN2_fd = open(gpio_file_path, O_WRONLY);  //RDWR
          if(-1 == GPIO_Point_IN2_fd)
          {
              printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
              return -1;
          }
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN3);
          GPIO_Point_IN3_fd = open(gpio_file_path, O_WRONLY);  //RDWR
          if(-1 == GPIO_Point_IN3_fd)
          {
              printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
              return -1;
          }
      
          sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN4);
          GPIO_Point_IN4_fd = open(gpio_file_path, O_WRONLY);  //RDWR
          if(-1 == GPIO_Point_IN4_fd)
          {
              printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
              return -1;
          }
          return 1;
      }
      
      
      CC := riscv64-unknown-linux-gnu-gcc
      
      target = Motor
      
      LDFLAGS ?= -lm
      CFLAGS ?= -I .
      
      
      
      CSRCS += Motor.c
      MAINSRC  = ./main.c
      
      
      all : default
      
      
      OBJEXT ?= .o
      
      COBJS = $(CSRCS:.c=$(OBJEXT))
      MAINOBJ = $(MAINSRC:.c=$(OBJEXT))
      
      # SRCS =  $(CSRCS) $(MAINSRC)s
      %.o: %.c
      	@$(CC)  $(CFLAGS) -c $< -o $@
      	@echo "CC $<"
      
      default:  $(COBJS) $(MAINOBJ)
      	$(CC) -o $(target) $(MAINOBJ)  $(COBJS) $(LDFLAGS)
      	
      
      clean: 
      	rm -f $(target) $(AOBJS) $(COBJS) $(MAINOBJ)
      
      1 Reply Last reply Reply Quote Share 1
      • 1 / 1
      • First post
        Last post

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

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