开发环境:ubuntu16.04 64bit
Linux内核版本:linux-2.6.32.2
1、编写mini2440_hello_module.c
在/linux-2.6.32.2/drivers/char目录下,编写mini2440_hello_module.c,代码如下:
#include <linux/kernel.h>
#include <linux/module.h>
static int __init mini2440_hello_module_init(void)
{
printk("Hello yfw, Mini2440 module is installed !\n");
return 0;
}
static void __exit mini2440_hello_module_cleanup(void)
{
printk("Good-bye yfw, Mini2440 module was removed!\n");
}
module_init(mini2440_hello_module_init);
module_exit(mini2440_hello_module_cleanup);
MODULE_LICENSE("GPL");
2、修改Kconfig文件
编辑/linux-2.6.32.2/drivers/char目录下的Kconfig文件(在内核代码树里增加该项),加入如下内容:
config MINI2440_HELLO_MODULE
tristate "Mini2440 module sample"
depends on MACH_MINI2440
default m if MACH_MINI2440
help
Mini2440 module sample.
3、修改Makefile文件
编辑/linux-2.6.32.2/drivers/char目录下的Makefile文件,加入如下内容:
obj-$(CONFIG_MINI2440_HELLO_MODULE) += mini2440_hello_module.o
4、配置内核
在 linux-2.6.32.2 目录位置运行一下 make menuconfig在 DeviceDrivers -> Character devices 菜单中看到刚才所添加的选项了,我们选择选项意为把该驱动编译成模块。
5、编译模块
到 linux-2.6.32.2 源代码根目录位置,执行 make modules。
编译成功在mini2440_hello_module.c同一目录下可以看到mini2440_hello_module.ko文件,此文件即是我们需要的模块。
6、安装模块
U盘安装:
Linux内核配置对U盘的支持及挂载
将mini2440_hello_module.ko模块拷贝到U盘,插入开发板。
挂载驱动:
insmod /mnt/udisk/mini2440_hello_module.ko
卸载驱动:
常见错误
[root@H3-Studio=W]#insmod ./mini2440_hello_module.ko
Unable to handle kernel paging request at virtual address e1d430b8
pgd = c3a34000
[e1d430b8] *pgd=00000000
Internal error: Oops: 80000005 [#1]
last sysfs file: /sys/devices/virtual/sound/timer/dev
Modules linked in: mini2440_hello_module(+)
CPU: 0 Not tainted (2.6.32.2-FriendlyARM #11)
PC is at 0xe1d430b8
LR is at sys_init_module+0x98/0x1e0
pc : [<e1d430b8>] lr : [<c005e4cc>] psr: 80000013
sp : c3a33f88 ip : 00000000 fp : 00000000
r10: beb16cc8 r9 : c3a32000 r8 : c002a024
r7 : 0000095d r6 : 001c455c r5 : 00000000 r4 : bf000090
r3 : 00020074 r2 : 00000001 r1 : 00000000 r0 : 00000000
Flags: Nzcv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user
Control: c000717f Table: 33a34000 DAC: 00000015
Process insmod (pid: 727, stack limit = 0xc3a32270)
Stack: (0xc3a33f88 to 0xc3a34000)
3f80: 001dcfd8 0000095d 001c455c 00000000 00000069 beb16cc4
3fa0: 00000080 c0029ea0 00000000 00000069 001dcfd8 0000095d 001c455c 00000000
3fc0: 00000000 00000069 beb16cc4 00000080 beb16cc8 001c455c beb16cc8 00000000
3fe0: 00000001 beb1696c 0001cb5c 000094f4 60000010 001dcfd8 33dc13dc 77dc17c8
Code: bad PC value
---[ end trace 069cf01c0e26c1e1 ]---
Segmentation fault
[root@H3-Studio=W]#
注意:编译内核要和开发板内核一致