一、环境介绍
操作系统: ubuntu18.04 64位
二、编写Makefile(PC机)
先输入uname -r命令查看当前系统的版本.
代码语言:javascript复制root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# uname -r
5.3.0-40-generic
查看系统内核的路径是否存在:
代码语言:javascript复制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
代码语言:javascript复制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如下:
代码语言:javascript复制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
编译安装测试:
代码语言:javascript复制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命令查看日志里是否有输出。
代码语言:javascript复制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编译方式
代码语言:javascript复制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