久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

新聞中心

EEPW首頁 > 嵌入式系統 > 牛人業話 > 撥開烏云見天日驅動開發之Ubuntu12.04驅動開發

撥開烏云見天日驅動開發之Ubuntu12.04驅動開發

作者:machinnneee 時間:2014-10-29 來源:電子產品世界 收藏

  現在介紹驅動的開發,以一個簡單的hello進行說明。首先我在 /home/test 目錄下創建2個文本文件 hello.c 和Makefile

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

  相信有驅動基礎的人都能看懂上面的程序。還需要一個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

評論


相關推薦

技術專區

關閉