tiny6410 蜂鸣器字符设备驱动<2>

来源:互联网 发布:论坛推广软件 编辑:程序博客网 时间:2024/06/02 04:16

用户空间程序也就是测试程序,实现蜂鸣器的关闭和打开操作。用if语句做输入参数的字符串比较判断。

测试代码

#include <stdio.h>

#include <string.h>

#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>


#define MAGIC 'f'
#define START_CMD0 _IO(MAGIC,0)
#define STOP_CMD1 _IO(MAGIC,1) 


int main(int argc,char *argv[])
{
int fp, i;
if(argc > 2)
{
printf("Usage ./beep_test arg");
return -1;
}
fp = open("/dev/beep",O_RDWR,S_IRUSR|S_IWUSR);
if(fp < 0)
{
printf("fail to open\n");
return -1;
}
if(strcmp(argv[1],"start") == 0)
ioctl(fp,START_CMD0,0);
else if(strcmp(argv[1],"stop") == 0)
ioctl(fp,STOP_CMD1,0);
else
{
printf("wrong arg\n");
return -1;
}

close(fp);
return 0;

}

Makefile文件如下:

obj-m := beep_modu.o
CURRENT_PATH = $(shell pwd)
KERNELDIR = /usr/local/FriendlyARM/tiny6410/linux/linux-2.6.38/


all:beep_modu.o beep_test.o


beep_modu.o:beep_modu.c
make -C $(KERNELDIR) M=$(CURRENT_PATH) modules
beep_test.o:beep_test.c
arm-linux-gcc -Wall -o beep_test beep_test.c
clean:
make -C $(KERNELDIR) M=$(CURRENT_PATH) clean
-rm -fr *.o beep_test beep_modu

最后结果:(选择了动态申请设备号)


0 0