新闻中心

EEPW首页 > 嵌入式系统 > 牛人业话 > 拨开乌云见天日驱动开发之Ubuntu12.04驱动开发

拨开乌云见天日驱动开发之Ubuntu12.04驱动开发

作者:machinnneee时间:2014-10-29来源:电子产品世界收藏

  现在介绍驱动的开发,以一个简单的hello进行说明。首先我在 /home/test 目录下创建2个文本文件 hello.c 和Makefile

本文引用地址:http://www.eepw.com.cn/article/264572.htm

  //hello.c程序如下

  /*

  * File Name: Hello.c

  *

  * Descriptions:

  * This is the my first module.

  *

  * Author:

  * Majunling

  * Kernel Version: 3.2.0-23-generic-pae

  *

  * Update:

  * - 2014-10-18 Majunling Creat this file

  */

  #include

  #include

  static int hello_init(void)

  {

  printk(KERN_ALERT "Hello, worldn");

  return 0;

  }

  static void hello_exit(void)

  {

  printk(KERN_ALERT "Goodbye, worldn");

  }

  module_init(hello_init);

  module_exit(hello_exit);

  MODULE_LICENSE("Dual BSD/GPL");

  相信有驱动基础的人都能看懂上面的程序。还需要一个makefile文件,具体如下

  //Makefile 文件

  # To build modules outside of the kernel tree, we run "make"

  # in the kernel source tree; the Makefile these then includes this

  # Makefile once again.

  # This conditional selects whether we are being included from the

  # kernel Makefile or not.

  ifeq ($(KERNELRELEASE),)

  # Assume the source tree is where the running kernel was built

  # You should set KERNELDIR in the environment if it's elsewhere

  KERNELDIR ?= /lib/modules/3.2.0-23-generic-pae/build

  # The current directory is passed to sub-makes as argument

  PWD := $(shell pwd)

  modules:

  (一个TAB开始)$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

  modules_install:

  (一个TAB开始)$(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

  # called from kernel build system: just declare what our modules are

  obj-m := hello.o

  endif

  此时需要注意Makefile格式,在module和module_install的下一行要以一个tab开始。完成这些工作之后进入test目录看看有什么内容:

  这时万事俱备,直接make

  mjl@mjl-machine:~/test$ make

  make -C /lib/modules/3.2.0-23-generic-pae/build M=/home/mjl/test modules

  make[1]: Entering directory `/usr/src/linux-headers-3.2.0-23-generic-pae'

  Building modules, stage 2.

  MODPOST 1 modules

  make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-23-generic-pae'

  然后查看make之后的文件内容:

  mjl@mjl-machine:~/test$ ls

  hello.c hello.mod.c hello.o modules.order

  hello.ko hello.mod.o Makefile Module.symvers

  安装驱动模块:

  mjl@mjl-machine:~/test$ sudo insmod ./hello.ko

  [sudo] password for mjl:

  我是在普通用户下进行的驱动安装,所以需要超级用户的密码,输入之后用lsmod可以查看安装的hello模块。

  mjl@mjl-machine:~/test$ lsmod

  Module Size Used by

  hello 12448 0

  vmhgfs 53736 1

  vsock 39001 0

  删除模块使用rmmod进行,如下:

  mjl@mjl-machine:~/test$ sudo rmmod hello

  但是不管加载还是卸载都没有打印信息,来这里查看

  mjl@mjl-machine:~/test$ cat /var/log/syslog |grep world

  Oct 18 16:12:20 mjl-machine kernel: [ 3335.231700] Hello, world

  Oct 18 16:14:15 mjl-machine kernel: [ 3449.649224] Goodbye, world

  究其原因,是因为如果你在字符终端而不是终端模拟器下运行的话,就会输出,因为在终端模拟器下时会把内核消息输出到日志文件/var/log/kern.log中。

  现在基于ubuntu系统的驱动开发需要的平台已经搭建完毕,经过测试可以使用,下一步就是进行驱动的系统学习,那是一项长期和艰巨的任务,你准备好了吗?如果在学习过程中碰到什么问题,欢迎来论坛forum.eepw.com.cn/forum/p/id/84进行提问,我们会在这里给你最及时最准确的解答。

linux相关文章:linux教程



上一页 1 2 下一页

关键词: Ubuntu12.04

评论


相关推荐

技术专区

关闭