您的位置:首页 > 其它

简单字符设备驱动编写,可传参,在内核之外编译

2014-09-16 18:51 295 查看
驱动程序:hello.c

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Hanson He");

static char *whom = "world";
static int howmany = 5;

static int hello_init(void)
{
	int i;
	for(i=0;i<howmany;i++)
	{
        	printk(KERN_ALERT "Hello %s\n",whom);
	}
        return 0;
}
static void hello_exit(void)
{
        printk(KERN_ALERT " Hello world exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
module_param(howmany, int, S_IRUGO);//参数名,参数类型,参数读/写权限
module_param(whom, charp, S_IRUGO);
EXPORT_SYMBOL(howmany);
EXPORT_SYMBOL(whom);


Makefile:Makefile

ifeq ($(KERNELRELEASE),)
KERNELDIR ?=/home/user/kernel  #内核所在目录

#当前Makefile所在目录
PWD := $(shell pwd)	

#	-C $(KERNELDIR)指明跳转到内核源码目录下读取那里的Makefile
#	M=$(PWD) 表明然后返回到当前目录继续读入、执行当前的Makefile
modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
    obj-m := hello.o
endif
Makefile相关解释,点击打开链接

make之后把hello.ko 通过网线tftp下载到开发板上,通过下面命令运行(带参数):

insmod hello.ko whom=Students howmany=5

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: