<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 牛人業(yè)話(huà) > 撥開(kāi)烏云見(jiàn)天日驅動(dòng)開(kāi)發(fā)之Ubuntu12.04驅動(dòng)開(kāi)發(fā)

撥開(kāi)烏云見(jiàn)天日驅動(dòng)開(kāi)發(fā)之Ubuntu12.04驅動(dòng)開(kāi)發(fā)

作者:machinnneee 時(shí)間:2014-10-29 來(lái)源:電子產(chǎn)品世界 收藏

  現在介紹驅動(dòng)的開(kāi)發(fā),以一個(gè)簡(jiǎn)單的hello進(jìn)行說(shuō)明。首先我在 /home/test 目錄下創(chuàng )建2個(gè)文本文件 hello.c 和Makefile

本文引用地址:http://dyxdggzs.com/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");

  相信有驅動(dòng)基礎的人都能看懂上面的程序。還需要一個(gè)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:

  (一個(gè)TAB開(kāi)始)$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

  modules_install:

  (一個(gè)TAB開(kāi)始)$(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

  此時(shí)需要注意Makefile格式,在module和module_install的下一行要以一個(gè)tab開(kāi)始。完成這些工作之后進(jìn)入test目錄看看有什么內容:

  這時(shí)萬(wàn)事俱備,直接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

  安裝驅動(dòng)模塊:

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

  [sudo] password for mjl:

  我是在普通用戶(hù)下進(jìn)行的驅動(dòng)安裝,所以需要超級用戶(hù)的密碼,輸入之后用lsmod可以查看安裝的hello模塊。

  mjl@mjl-machine:~/test$ lsmod

  Module Size Used by

  hello 12448 0

  vmhgfs 53736 1

  vsock 39001 0

  刪除模塊使用rmmod進(jìn)行,如下:

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

  但是不管加載還是卸載都沒(méi)有打印信息,來(lái)這里查看

  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

  究其原因,是因為如果你在字符終端而不是終端模擬器下運行的話(huà),就會(huì )輸出,因為在終端模擬器下時(shí)會(huì )把內核消息輸出到日志文件/var/log/kern.log中。

  現在基于ubuntu系統的驅動(dòng)開(kāi)發(fā)需要的平臺已經(jīng)搭建完畢,經(jīng)過(guò)測試可以使用,下一步就是進(jìn)行驅動(dòng)的系統學(xué)習,那是一項長(cháng)期和艱巨的任務(wù),你準備好了嗎?如果在學(xué)習過(guò)程中碰到什么問(wèn)題,歡迎來(lái)論壇forum.eepw.com.cn/forum/p/id/84進(jìn)行提問(wèn),我們會(huì )在這里給你最及時(shí)最準確的解答。

linux相關(guān)文章:linux教程



上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞: Ubuntu12.04

評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>