一、環境介紹
作業系統: ubuntu18.04 64位
二、編寫Makefile(PC機)
先輸入uname -r指令檢視目前系統的版本.
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# uname -r
5.3.0-40-generic
檢視系統核心的路徑是否存在:
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# ls /usr/src/
linux-headers-5.3.0-40 linux-headers-5.4.0-53-generic linux-hwe-5.4-headers-5.4.0-47
linux-headers-5.3.0-40-generic linux-hwe-5.4-headers-5.4.0-42 linux-hwe-5.4-headers-5.4.0-48
linux-headers-5.4.0-48-generic linux-hwe-5.4-headers-5.4.0-45 linux-hwe-5.4-headers-5.4.0-53
根據目前系統的版本,确定對應的路徑: /usr/src/linux-headers-5.3.0-40
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# ls /usr/src/
linux-headers-5.3.0-40 linux-headers-5.4.0-53-generic linux-hwe-5.4-headers-5.4.0-47
linux-headers-5.3.0-40-generic linux-hwe-5.4-headers-5.4.0-42 linux-hwe-5.4-headers-5.4.0-48
linux-headers-5.4.0-48-generic linux-hwe-5.4-headers-5.4.0-45 linux-hwe-5.4-headers-5.4.0-53
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# ls /usr/src/linux-headers-5.3.0-40
arch certs Documentation fs init Kbuild kernel Makefile net scripts sound ubuntu virt
block crypto drivers include ipc Kconfig lib mm samples security tools usr
完整的編寫Makefile如下:
Linux_ADD=/usr/src/linux-headers-5.3.0-40-generic
app_dev:
make -C $(Linux_ADD) M=`pwd` modules clean
make -C $(Linux_ADD) M=`pwd` modules
gcc spectrometer_app.c -o app
obj-m +=spectrometer_usb_drv.o
編譯安裝測試:
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# insmod spectrometer_usb_drv.ko
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# modinfo spectrometer_usb_drv.ko
filename: /mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu/spectrometer_usb_drv.ko
license: GPL
author: xiaolong
srcversion: 40F0CA1A1C1E3BA787F1B57
alias: usb:v0661p294Bd*dc*dsc*dp*ic*isc*ip*in*
alias: usb:v148Fp5370d*dc*dsc*dp*ic*isc*ip*in*
depends:
retpoline: Y
name: spectrometer_usb_drv
vermagic: 5.3.0-40-generic SMP mod_unload
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# rmmod spectrometer_usb_drv.ko
關于日志顯示的問題: 驅動程式正常會使用printk向終端列印調試資訊, 如果驅動安裝之後目前終端沒有反應,可以先使用dmesg指令檢視日志裡是否有輸出。
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# dmesg
[137747.993110] 總線名稱:usb
[137747.993111] 端點号[0]:129
[137747.993111] 端點[0] 輸入端點(裝置到主機)
[137747.993112] 端點[0] 裝置支援批量傳輸.
[137747.993112] 端點[0] 傳輸的資料大小:512
[137747.993112] probe:dev->bulk_in_endpointAddr=129
[137747.993113] 端點号[1]:1
[137747.993113] 端點[1] 輸出端點(主機到裝置)
[137747.993113] 端點[1] 裝置支援批量傳輸.
[137747.993114] 端點[1] 傳輸的資料大小:512
[137747.993114] probe:dev->bulk_out_endpointAddr=1
[137747.993114] 端點号[2]:2
[137747.993115] 端點[2] 輸出端點(主機到裝置)
[137747.993115] 端點[2] 裝置支援批量傳輸.
[137747.993115] 端點[2] 傳輸的資料大小:512
[137747.993115] 端點号[3]:3
[137747.993116] 端點[3] 輸出端點(主機到裝置)
[137747.993116] 端點[3] 裝置支援批量傳輸.
[137747.993116] 端點[3] 傳輸的資料大小:512
[137747.993116] 端點号[4]:4
[137747.993116] 端點[4] 輸出端點(主機到裝置)
[137747.993117] 端點[4] 裝置支援批量傳輸.
[137747.993117] 端點[4] 傳輸的資料大小:512
[137747.993157] USB光譜儀裝置節點注冊成功:/dev/spectrometer_usb_drv ,主裝置号:10,次裝置号:53
[137747.993173] usbcore: registered new interface driver spectrometer_usb_drv
三、順便說下嵌入式Linux驅動Makefile編譯方式
Linux_ADD=/home/wbyq/work/linux-3.5/linux-3.5
app_dev:
make -C $(Linux_ADD) M=`pwd` modules clean
make -C $(Linux_ADD) M=`pwd` modules
cp *.ko /home/wbyq/project/ -fv
arm-linux-gcc spectrometer_app.c -o app
cp app /home/wbyq/project/ -fv
obj-m +=spectrometer_usb_drv.o