本文共 3230 字,大约阅读时间需要 10 分钟。
本博客是在虚拟机中编写驱动程序,然后交叉编译到树莓派。所以需要确认几件事:
1. 确保树莓派的内核版本和虚拟机中的Linux内核版本保持一致,否则无法安装驱动; 2. 虚拟机中有交叉编译工具; 3. 对于树莓派4来说,交叉编译驱动模块的时候,KERNEL=kernel7l,树莓派2、3代KERNEL=kernel7;驱动的编写同样会根据上一篇博客()的框架来写:
#include#include #include #include #include #include #include static int Test_open(struct inode *inode,struct file *file){ printk("pin4_open\n"); return 0;}static ssize_t Test_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos){ printk("pin4_write\n"); return 0;}static struct file_operations pin4_fops ={ .owner=THIS_MODULE, .open= Test_open, .write=Test_write,}
使用static关键字是为了函数名冲突,谁都不能保证拥有一万多C文件的Linux内核中中会不会有名字冲突,所以 static很有必要。这个程序的功能会在内核环境打印相关信息。
#include#include #include #include #include #include #include static struct class *Test_class;static struct device *Test_dev;static dev_t devno; //device numblestatic int major=240; //major device numble static int minor=0; //minor device numblestatic char *module_name = "test";//device namestatic intTest_open(struct inode *inode,struct file *file){ printk("Test_open\n"); return 0;}static ssize_t Test_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos){ printk("Test_write\n"); return 0;}static struct file_operations Test_fops ={ .owner=THIS_MODULE, .open=Test_open, .write=Test_write,}/* 初始化函数 */int __init Test_dev_init(void){ int ret; devno =MKDEV(major,minor); ret=register_chrdev(major,module_name,&Test_fops); //在驱动链表中注册驱动为字符设备 Test_class=class_create(THIS_MODULE,"Test_class");//创建类 Test_dev =device_create(pin4_class,NULL,devno,NULL,module_name);//创建驱动文件 return 0;}
void __exit Test_exit(void) { device_destroy(Test_class,devno); class_destroy(Test_class); unregister_chrdev(major,module_name);} module_init(Test_dev_init); module_exit(Test_exit); MODULE_LICENSE("GPL v2");
写好驱动之后需要把驱动文件放到内核文件的 /drivers/char/ 目录下,即字符设备文件夹:
cp Test.c /home/.../drivers/char/
然后在内核文件的 /drivers/char/ 修改 Makefile 文件,添加:
obj-m += test.o //obj-m 即编译成模块,
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- KERNEL=kernel7l make -j4 modules
一次性编译驱动模块:
scp drivers/char/xxx.ko pi@/*树莓派的IP地址*/:/home/xxx//*树莓派的某个路径*/
在树莓派里使用:
sudo insmod xxx.ko sudo chmod 666 /dev/xxx
安装驱动之后,给驱动添加权限,这样我们用户才能去使用这个驱动;
驱动的测试非常简单,我们只要写一个简单的程序去运行就可以:
测试程序代码:#include#include #include #include #include int main(){ int fd; int cmd=1; fd=open("/dev/test",O_RDWR); if(fd<0){ perror("reson"); return -1; } fd=write(fd,&cmd,sizeof(int)); return 0;}
dmesg
指令来查看内核的打印信息,驱动的安装错误提示也可以用该指令来查看,驱动的名字最好用小写字母来命名;
师承(某(dou)音)上官可编程转载地址:http://stlwz.baihongyu.com/