zoukankan      html  css  js  c++  java
  • 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  删除设备节点:rm /dev/buttons
    3、加载/移除驱动模块:insmod/rmmod charButtons.ko
    4、执行应用程序:./buttons即可调用按键驱动

  • 相关阅读:
    MFC TeeChart 用法整理二 (画二维图)
    CFileFind类的详解以及应用实例
    error C2065: 'AfxCriticalTerm' : undeclared identifier 解决办法
    error LNK2001
    ShellExecute详细用法(可以单独打开一个文件)
    java连接SQL Server数据库
    Java 异常处理
    eclipse 无法自动匹配
    Java简单的表格模型
    Java 字符串常用操作(String类)
  • 原文地址:https://www.cnblogs.com/embeded-linux/p/10701654.html
Copyright © 2011-2022 走看看