arm-linux 应用层调用驱动函数

需要调用头文件

#include "stdio.h"

#include "unistd.h" 

 /*

unistd.h 是 C 和 C++ 程序设计语言中提供对 POSIX 操作系统 API 的访问功能的头文件的名称。该头文件由 POSIX.1 标准(单一UNIX规范的基础)提出,故所有遵循该标准的操作系统和编译器均应提供该头文件(如 Unix 的所有官方版本,包括 Mac OS X、Linux 等)。

对于类 Unix 系统,unistd.h 中所定义的接口通常都是大量针对系统调用的封装(英语:wrapper functions),如 fork、pipe 以及各种 I/O 原语(read、write、close 等等)。
*/

#include "sys/types.h" //数据类型

#include "sys/stat.h"

/*

#include <sys/stat.h> 文件状态,是unix/linux系统定义文件状态所在的伪标准头文件。

*/

#include "fcntl.h"

//引用linux c头文件#include<sys/types.h>和#include<fcntl.h>头文件总结_码莎拉蒂 .的博客-CSDN博客

#include "stdlib.h"

#include "string.h"

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"


#define LEDOFF 	0
#define LEDON 	1

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd, retvalue;
	char *filename;
	unsigned char databuf[1];
	
	if(argc != 3){
		printf("Error Usage!\r\n");
		return -1;
	}

	filename = argv[1];

	/* 打开led驱动 */
	fd = open(filename, O_RDWR);
	if(fd < 0){
		printf("file %s open failed!\r\n", argv[1]);
		return -1;
	}

	databuf[0] = atoi(argv[2]);	/* 要执行的操作:打开或关闭 */

	/* 向/dev/led文件写入数据 */
	retvalue = write(fd, databuf, sizeof(databuf));
	if(retvalue < 0){
		printf("LED Control Failed!\r\n");
		close(fd);
		return -1;
	}

	retvalue = close(fd); /* 关闭文件 */
	if(retvalue < 0){
		printf("file %s close failed!\r\n", argv[1]);
		return -1;
	}
	return 0;
}

在开发板运行/ledApp /dev/gpioled 1     gpioled为驱动的名字 通过filename = argv[1]获取;

                                1位值 通过    databuf[0] = atoi(argv[2]);    /* 要执行的操作:打开或关闭 */获取

猜你喜欢

转载自blog.csdn.net/L1153413073/article/details/125500727