linux驱动动态与静态加载

在Linux中驱动的加载方式有动态加载和静态加载。
动态加载,即驱动不添加到内核中,在内核启动完成后,仅在用到这一驱动时才会进行加载
静态加载,驱动编译进内核中,随内核的启动而完成驱动的加载。
添加字符驱动代码到内核的方式:
1、在Linux-3.0.8/drivers/char/Kconfig中为charDev添加一个config条目,比如config TEST_CHAR
2、修改Linux-3.0.8/drivers/char/Makefile,添加如下代码:obj-$(CONFIG_TEST_CHAR)+=charDev.o
3、把charDev.c拷到Linux-3.0.8/drivers/char下
4、make menuconfig,选中CONFIG_TEST_CHAR项目,设定为【M】或者【Y】
5、重新编译即可(设定为M:编译后得到的为动态加载模块*.ko;设定为Y:编译到内核中)

驱动动态加载过程:(以字符按键驱动为例:charButtons.ko,dev_t = 232 0 name= "buttons")

1、编辑并编译应用程序:buttons.c (gcc -c buttons.c -o buttons

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <sys/ioctl.h>
 5 #include <sys/types.h>
 6 #include <sys/stat.h>
 7 #include <fcntl.h>
 8 
 9 int main(int argc, char **argv)
10 {
11     int on;
12     int led_no;
13     int fd;
14 
15 
16     fd = open("/dev/buttons", 0);
17     if (fd < 0) {
18         perror("open device buttons");
19         exit(1);
20     }
21 
22     close(fd);
23 
24     return 0;
25 }

2、创建设备节点:mknod /dev/buttons c 232 0
3、加载驱动模块:insmod charButtons.ko
4、执行应用程序:./buttons即可调用按键驱动

猜你喜欢

转载自www.cnblogs.com/embeded-linux/p/10701654.html
今日推荐