这是本文档旧的修订版!


C参考代码如下:

#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <string.h> 
static char gpio_path[100];
//设置GPIO方向、高低电平
static int gpio_config(const char *value) 
{ 
	char config_path[100]; 
	int len; 
	int fd; 
	sprintf(config_path, "%s", gpio_path); 
	if (0 > (fd = open(config_path, O_WRONLY))) 
	{
		perror("open error"); 
		return fd; 
	} 
	len = strlen(value); 
	if (len != write(fd, value, len))
	{ 
		perror("write error"); 
		close(fd); 
		return -1; 
	} 
	close(fd); 
	return 0; 
} 
int main(int argc, char *argv[]) 
{ 
	 if (4 != argc) 
	{ 
		if (3 != argc) 
		{
			fprintf(stderr, "set gpio out : %s <gpio> <out> <value>\n", argv[0]); 
			fprintf(stderr, "set gpio in  : %s <gpio> <in>\n", argv[0]); 
			exit(-1); 
		}
	}	 
	sprintf(gpio_path, "/sys/class/io_control/gpio_op%s", argv[1]); 
	if (access(gpio_path, F_OK)) 
	{ 
		printf("%s inexistence,export %s... \n",gpio_path,argv[1]);
	} 	
	if ( 0 == strcmp("out",argv[2] ) && argc == 4 )
	{
		if(gpio_config(argv[3]))
		exit(-1); 
	}
	printf("gpio_op%s:\n",argv[1]);
	printf("direction : %s\n", argv[2]);
	printf("value : %s\n", argv[3]);
	 exit(0); 
}

交叉编译源码:

aarch64-linux-gnu-gcc -o gpio gpio.c

将编译好的gpio程序使用scp拷贝到 3399 主板上,执行测试:

使用方法:

set gpio out : ./gpio <gpio> <out> <value>
set gpio in  : ./gpio <gpio> <in>

打印/导出