设备注册:https://blog.csdn.net/u011425939/article/details/80546761
驱动注册:https://blog.csdn.net/u011425939/article/details/80636776
前面介绍完设备的注册和对应的驱动注册过程,现在来讲讲怎么表用驱动写运用程序。
1、头文件
– #include <stdio.h>调用打印函数printf
– #include <sys/types.h>基本系统数据类型。系统的基本数据类型在 32 编译环境中保持为 32 位值,并会在 64 编译环境中增长为 64 位值。
– #include <sys/stat.h>系统调用函数头文件。可以调用普通文件,目录,管道,socket,字符,块的属性
– #include <fcntl.h>定义了open函数
– #include <unistd.h>定义了close函数
– #include <sys/ioctl.h>定义了ioctl函数
2、invoke_hello.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
main(){
int fd;
char *hello_node = "/dev/hello";
char buf[10]={0};
/*O_RDWR只读打开,O_NDELAY非阻塞方式*/
if((fd = open(hello_node,O_RDWR|O_NDELAY))<0){
printf("APP open %s failed\n",hello_node);
}
else{
printf("APP open %s success\n",hello_node);
ioctl(fd,1,6);
}
read(fd,buf,sizeof(buf));
write(fd,buf,sizeof(buf));
close(fd);
}
3、编译
arm-none-linux-gnueabi-gcc -o invoke_hello invoke_hello.c
4、运行
[root@iTOP-4412]#
[root@iTOP-4412]# ./invoke_hello
[ 6564.326699] hello open
[ 6564.327709] cmd is 1,arg is 6
[ 6564.331677] helo_read
[ 6564.332798] helo_write
[ 6564.335163] write :hello word
[ 6564.338089] ppos :0
[ 6564.340215] hello release
APP open /dev/hello success
[root@iTOP-4412]#