fl2440開發板hello world模塊驅動編寫


hello world
           許多的編程都是從hello world開始編寫,雖然簡單但是意義重大!!!
hello.c                                                                                                                                                               
  1 /*********************************************************************************
  2  *      Copyright:  (C) 2015 guanlei
  3  *                  All rights reserved.
  4  *
  5  *       Filename:  hellow.c
  6  *    Description:  This file hellw.c
  7  *                 
  8  *        Version:  1.0.0(2015年10月22日)
  9  *         Author:  guanlei <[email protected]>
 10  *      ChangeLog:  1, Release initial version on "2015年10月22日 19時08分08秒"
 11  *                 
 12  ********************************************************************************/
 13 
 14 #include<linux/init.h>      //所有驅動編寫都要包含的頭文件
 15 #include<linux/module.h>
 16 
 17 
 18 static int hello_init(void)      
 19 {
 20     printk(KERN_ALERT"hello world\n");   //KERN_ALERT是在內核中定義的規定的打印級別
 21     return 0;
 22 }
 23 
 24 
 25 static int hello_exit(void)
 26 {
 27 
 28     printk(KERN_ALERT"GOOD BEY\n");
 29 
 30 }
 31 
 32 module_init(hello_init);  // module_init module_exit 內核中合數的宏 insmod 時會調用的module_init() 中的函數
 33 module_exit(hello_exit);
 34 
~                                                                                                                                                                      
~                             
 

 hello world驅動的Makefile編寫

適用於自己的linux操作系統

  2 obj-m=hello.o      //驅動模塊從目標文件中構造
  3 modules:
  4     make -C /lib/modules/`uname -r`/build/ M=`pwd` modules       //  -C 指定內核源代碼的目錄  uname -r獲得自己內核的版本信息 M指定編譯生成的文件放在哪個路徑中
  5     make clean
  6 
  7 clean:
  8     rm -f *.ko.* *.o *.mod.c *.order *.symvers 
~                                                                                                                                                                      
~                                               
make 成功後,會生成一個hello.ko的文件,insmod hello.ko (2.6版本的內核要加.ko     3.0版本的內核不用加.ko)就會在加載我們的驅動模塊,若果想要看結果,敲dmesg再組後一行就會有hello world ,移除hello.ko的話要用rmmod命令,就會調用module_exit()裏邊調用的函數,dmesg查看信息的就會在最後一行出現GOOD BEY



接下來的makefile是適配fl2440開發板的
 Makefile                                                                                        
  1 CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc    //應爲要把模塊給開發板用所以我們要把交叉編譯器作指定我們的編譯器
  2 KDIR?=/home/guanlei/fl2440/kernel/linux-3.0          //該目錄是告訴Makefile 從哪找到我們的內核
  3 obj-m:=hello.o
  4 
  5 default:                                                                                           
  6     $(MAKE) -C $(KDIR) M=`pwd` modules
  7     make clean
  8 
  9 clean:
 10     rm -f *.ko.* *.o *mod.c *.order *.symvers
~                                                                                                
                                                                                               
make之後和上面的步驟一樣,唯一的區別就是在加載驅動模塊和移除驅動模塊時 insmod 和rmmod不要.ko                                                 

       感興趣的讀者若果想要知道insmod的工作,它就依賴於定義在kernel/module.c中的系統調用  






發佈了30 篇原創文章 · 獲贊 25 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章