riscv开发板visionfive2操作gpio

首先是GPIO的编号,右下角编号40的 GPIO接口对应的是GPIO44 , 应该使用44这个编号,而不是40.

https://doc-en.rvspace.org/VisionFive2/PDF/VisionFive2_QSG.pdf

 

用以下的命令进行操作和可看。

具体的介绍 export  in out, 这些的意思,不讲了。 树莓派的文档里面很多。

cd /sys/class/gpio
echo 44 > export
cd gpio44

echo out > direction
echo 1 > value
cat value
echo 0 > value
cat value



echo in > direction
cat value

分享一个main.c文件, 作用是模拟树莓派里面的gpio程序。用来进行读写gpio引脚

gcc   main.c  -o gpio

gpio read  44  
gpio mode  44 in / out
gpio write 44 0

main.c文件内容:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>


//#define GPIO44 	492	
//#define GPIO22 	470
              
#define MAX_BUF	      128                   //Define array size
#define StarF_Gpio_Dir "/sys/class/gpio"    //GPIO control paths


//
//  GND
//  GPIO36
//  GPIO61 
//  GPIO44
//


int StarF_gpio_export(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];
	// /sys/class/gpio/export
    fd = open( "/sys/class/gpio/export", O_WRONLY);  
    if (fd < 0) {
        perror("gpio/export");
        return fd;
    }

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



int StarF_gpio_unexport(unsigned int gpio)
{
    int fd, len;
    char buf[MAX_BUF];
	// /sys/class/gpio/unexport
    fd = open("/sys/class/gpio/unexport", O_WRONLY);
    if (fd < 0) {
        perror("gpio/export");
        return fd;
    }
 
    len = snprintf(buf, sizeof(buf), "%d", gpio);  
    write(fd, buf, len);                           
    close(fd);                                      
    return 0;
}


//0 input, 1 output
int StarF_gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
    int fd, len;
    char buf[MAX_BUF];
	// /sys/class/gpio/gpioN/direction
    len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/direction", gpio);
 
    fd = open(buf, O_WRONLY);
    if (fd < 0) {
        perror(buf);
        return fd;
    }
 
    if (out_flag)                   //'1' set to output
        write(fd, "out", 4);
    else                            //'0' set input
        write(fd, "in", 3);
 
    close(fd);
    return 0;
}

 
int StarF_gpio_set_value(unsigned int gpio, unsigned int value)
{
    int fd, len;
    char buf[MAX_BUF];
	// /sys/class/gpio/gpioN/value
    len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio); 
    fd = open(buf, O_WRONLY);	    
    if (fd < 0) {
        perror(buf);
        return fd;
    }
 
    if (value)                       //'1' output is high level
        write(fd, "1", 2);
    else                             //'0' output is Low level
        write(fd, "0", 2);
 
    close(fd);
    return 0;
}


 
int StarF_gpio_get_value(unsigned int gpio, unsigned int *value)
{
    int fd, len;
    char buf[MAX_BUF];
    char ch;
	// /sys/class/gpio/gpioN/value
    len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio);
 
    fd = open(buf, O_RDONLY);     
    if (fd < 0) {
        perror("gpio/get-value");
        return fd;
    }
 
    read(fd, &ch, 1);                //Read the external input level

    if (ch != '0') {                 //'1' Input is high level
        *value = 1;                  
    } else {                         //'0' Input is Low level
        *value = 0;                  
    }
 
    close(fd);			   
    return 0;
}


void doMode (int argc, char *argv [])
{
	int pin ;
	char *mode ;

	if (argc != 4)
	{
		fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
		exit (1) ;
	}

	pin = atoi (argv [2]) ;
	mode = argv [3] ;
	int sys = pin;
	
	int ret = StarF_gpio_export(sys);
	if(ret<0)
		return ret;
	
	if (strcasecmp (mode, "in")== 0) 
	    StarF_gpio_set_dir(sys, 0);
	else if (strcasecmp (mode, "input")   == 0)
	    StarF_gpio_set_dir(sys, 0);
	else if (strcasecmp (mode, "out")     == 0) 
	    StarF_gpio_set_dir(sys, 1);
	else if (strcasecmp (mode, "output")  == 0) 
	   StarF_gpio_set_dir(sys, 1);
	else
	{
		fprintf (stderr, "%s: Invalid mode: %s. Should be in/out\n", argv [1], mode) ;
		exit (1) ;
	}
	
	exit(0);
}


void doRead(unsigned int pin)
{
	unsigned int sys = pin;
	int ret = StarF_gpio_export(sys);
	if(ret<0)
		return ret;
	//StarF_gpio_set_dir(sys, 0);
	
	unsigned int value=0;
	StarF_gpio_get_value(sys, &value);
	printf("%d\n", value);
	exit(0);
	return 0;
}

void doWrite(unsigned int pin, unsigned int value)
{
	unsigned int sys = pin;
	int ret = StarF_gpio_export(sys);
	if(ret<0)
		return ret;
	StarF_gpio_set_dir(sys, 1);//output
	
	ret = StarF_gpio_set_value(sys, value);
	return 0;
}


void help()
{
	printf("\t********  gpio HELP  ************\n");
	printf("gpio read  1   \n");
	printf("gpio write 1 0 \n");    
	printf("gpio mode  1 in/out \n");
}

int main(int argc, char **argv) 
{
   
	if(argc < 3)
	{
		help();
		return -1;
	}

	if (strcasecmp (argv [1], "mode") == 0)
	{	if(argc <= 3)
		{	
			help();
			return -1;
		}
		doMode(argc, argv);
	}
    else if (strcasecmp (argv [1], "read") == 0)
		doRead(atoi(argv[2])) ;
    else if (strcasecmp (argv [1], "write") == 0)
	{
		if(argc <= 3)
		{	
			help();
			return -1;
		}
		doWrite(atoi(argv [2]), atoi(argv [3])) ;
	}
	
	//help();
    return -1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路边闲人2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值