ARM-第七次
2017-01-12 本文已影响9人
帅碧
Paste_Image.png
Paste_Image.png4.修改Makefile文件,复制mini2440_hello_module那一行之后,进行修改
Paste_Image.png5.然后保存退出
6.编译:make zImage
7.进入config界面:make menuconfig
- 选择Device Drivers --->
Paste_Image.png
- 选择Character devices
Paste_Image.png
- 选择My module sample,按空格键将M改为*号
内核设备的编写
1.在用户自己创建的目录下:创建一个hello.c与Makefile的文件
2.在hello.c中
#include <linux/kernel.h>
#include <linux/module.h>
static int __init mini2440_hello_module_init(void)
{
printk("Hello,Min2440 module is installed !\n");
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{
printk("Good_bye,mini2440 module was removed!\n");
}
module_init(mini2440_hello_module_init);
module_exit(mini2440_hello_module_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jiang xiu bi");
MODULE_DESCRIPTION("This is example module!!");
MODULE_VERSION("V1.0.0");
- 在Makefile中
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /home/jxb/1612/1/sq1612/linux-2.6.32.2
PWD :=$(shell pwd)
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 *.symvers *.order
.PHONLY:modules modules_install clean
else
obj-m :=hello.o
#obj-m :=hello2.o
endif
- 运行结果
3.代码升级,在hello.c中
#include <linux/kernel.h>
#include <linux/module.h>
//#define __init
//#define __exit
static int howmany=1;
static char * name="Jiang xiu bi";
static int __init mini2440_hello_module_init(void)
{
printk("Hello,Min2440 module is installed !\n");
printk("I am %s!\n",name);
printk("I have %d mini2440 board!\n",howmany);
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{
printk("Good_bye,mini2440 module was removed!\n");
}
module_init(mini2440_hello_module_init);
module_exit(mini2440_hello_module_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jiang xiu bi");
MODULE_DESCRIPTION("This is example module!!");
MODULE_VERSION("V1.0.0");
module_param(howmany,int,S_IRUGO);
module_param(name,charp,S_IRUGO);
EXPORT_SYMBOL(howmany);
EXPORT_SYMBOL(name);
- 在Makefile中
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /home/jxb/1612/1/sq1612/linux-2.6.32.2
PWD :=$(shell pwd)
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 *.symvers *.order
.PHONLY:modules modules_install clean
else
obj-m :=hello.o
#obj-m :=hello2.o
endif
Paste_Image.png Paste_Image.png
- 运行结果
- 再升级版,两个模块的,在hello.c中
#include <linux/kernel.h>
#include <linux/module.h>
//#define __init
//#define __exit
static int howmany=1;
static char * name="Jiang xiu bi";
static int __init mini2440_hello_module_init(void)
{
printk("Hello,Min2440 module is installed !\n");
printk("I am %s!\n",name);
printk("I have %d mini2440 board!\n",howmany);
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{
printk("Good_bye,mini2440 module was removed!\n");
}
module_init(mini2440_hello_module_init);
module_exit(mini2440_hello_module_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jiang xiu bi");
MODULE_DESCRIPTION("This is example module!!");
MODULE_VERSION("V1.0.0");
module_param(howmany,int,S_IRUGO);
module_param(name,charp,S_IRUGO);
EXPORT_SYMBOL(howmany);
EXPORT_SYMBOL(name);
- 在hello2.c中
#include <linux/kernel.h>
#include <linux/module.h>
//#define __init
//#define __
extern int howmany;
extern char * name;
static int __init mini2440_hello_module_init(void)
{
printk("Hello,Min2440 module is installed !\n");
printk("I am %s!\n",name);
printk("I have %d mini2440 board!\n",howmany);
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{
printk("Good_bye,mini2440 module was removed!\n");
}
module_init(mini2440_hello_module_init);
module_exit(mini2440_hello_module_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jiang xiu bi");
MODULE_DESCRIPTION("This is example module!!");
MODULE_VERSION("V1.0.0");
- 在Makefile中
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /home/jxb/1612/1/sq1612/linux-2.6.32.2
PWD :=$(shell pwd)
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 *.symvers *.order
.PHONLY:modules modules_install clean
else
obj-m :=hello.o
obj-m :=hello2.o
endif
Paste_Image.png
- 运行结果
1.先运行模块1
2.再运行模块2
Paste_Image.png