为 raspberrypi 开发内核模块
生活随笔
收集整理的這篇文章主要介紹了
为 raspberrypi 开发内核模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有時候為了方便與硬件更好交互,可能會需要為它專門寫個驅動。這就涉及到了內核模塊的開發。 其實內核模塊的開發沒有想象的那么復雜,也就是準備工作有點繁瑣,開源的東西都這樣,一切都得自己來。 ## 安裝kernel header源碼 首先查看當前使用的內核版本 $ uname -r 3.6.11+ 我的系統是`2013-02-09-wheezy-raspbian`,所以顯示出來的內核版本是`3.6.11+` 首先看看apt源里面有沒有,有的話直接安裝 $ apt-cache search linux-headers 沒有找到`3.6.11+`相關的,其中的`3.6`的一些down下來也沒法用。 下面就手動把`3.6.11+`的header-src搞下來。 下載3.6.11+的內核源碼 $ wget https://github.com/raspberrypi/linux/archive/rpi-3.6.y.tar.gz 解壓 $ tar xvfz rpi-3.6.y.tar.gz 取得root權限 $ su 將源碼移至/usr/src目錄 # mv linux-rpi-3.6.y /usr/src 建立內核模塊庫目錄的鏈接 # ln -s /usr/src/linux-rpi-3.6.y /lib/modules/3.6.11+/build # cd /lib/modules/3.6.11+/build make mrproper可以看作更加強勁的make clean命令,用來清除環境變量,即清除配置文件,一般編譯內核前都要運行 # make mrproper 將當前正在使用的系統的內核配置生成內核配置信息 # gzip -dc /proc/config.gz > .config 生成編譯內核所需要的東西 # make modules_prepare 獲取內核編譯時生成的內核模塊導出符號文件,因為不是從頭編譯內核,所以沒有,但是編譯內核模塊需要這個 # wget https://github.com/raspberrypi/firmware/raw/master/extra/Module.symvers 退出root # exit ## 編寫hello world 內核模塊 寫一個最簡單的內核模塊 `hello.c`, 這個內核只會在加載的時候輸出 `helo,word`,移除時輸出`goodbye` 在樹梅派上找個地方新建一個目錄,創建文件`hello.c`: #include #include MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello, world"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, cruel world"); } module_init(hello_init); module_exit(hello_exit); 創建`Makefile`文件: #include #include MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello, world"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, cruel world"); } module_init(hello_init); module_exit(hello_exit); 準備工作做好了,現在開始編譯 $ make 會生成以下文件: $ ls hello.c hello.mod.c hello.o modules.order hello.ko hello.mod.o Makefile Module.symvers 查看這個內核模塊的信息: $ modinfo hello.ko filename: /home/pi/sensors/kernel-model/hello.ko license: Dual BSD/GPL srcversion: 9AE281A251509460F68E55F depends: vermagic: 3.6.11 preempt mod_unload modversions ARMv6 加載內核: $ sudo sudo insmod hello.ko 命令行沒有輸出,別急,繼續移除它: $ sudo rmmod hello 現在查看輸出: $ dmesg | tail -5 [ 7175.039569] Hello, world [ 7199.989110] Goodbye, cruel world 可以看到,這個hello內核模塊工作的很正常
總結
以上是生活随笔為你收集整理的为 raspberrypi 开发内核模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery eaayui 学习(三)t
- 下一篇: Windows Server 2012