当前位置:   article > 正文

Linux学习系列二十:Linux驱动编写入门

Linux学习系列二十:Linux驱动编写入门

1.引言

很早之前就有网友建议写一篇关于Linux驱动的文章。之所以拖到现在才写,原因之一是我之前没有在工作中遇到需要自己手动去写驱动的需求,主要是现在Linux内核驱动的支持已经比较完善了,另外一个原因是自己水平实在有限,不敢写驱动这个话题,Linux驱动里涉及到的东西太多了,很多年前专门买过驱动相关的书籍,厚厚的,看的云里雾里。借此机会,在这里给大家做个非常非常入门级的介绍,希望对大家有所帮助。          

2.环境介绍

2.1 硬件

 网上的一个第三方做的NUC972开发板,这里会用到板子上的MPU6050传感器芯片,相关部分原理图如下:

2.2 软件

1) Uboot不需要改动

2) Kernel不需要改动

3) Rootfs不需要重新编译

3.最简单的例子

第1步:编写hello.c

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. static int __init hello_init(void) {
  4. printk(KERN_INFO "module init success\n");
  5. return 0;
  6. }
  7. static void __exit hello_exit(void) {
  8. printk(KERN_INFO "module exit success\n");
  9. }
  10. module_init(hello_init);
  11. module_exit(hello_exit);
  12. MODULE_LICENSE("GPL");
  13. MODULE_AUTHOR("wuya");
  14. MODULE_DESCRIPTION("driver example");

这是一个简单的内核模块程序,可以动态加载和卸载。模块加载的时候系统会打印module init success,模块卸载的时候系统会打印module exit success。

开头的两个头文件,init.h 定义了驱动的初始化和退出相关的函数,module.h 定义了内核模块相关的函数、变量及宏。然后module_init和module_exit是模组加载和卸载相关的两个函数,

第2步:编写Makefile

  1. obj-m := hello.o
  2. PWD := $(shell pwd)
  3. KDIR :=/home/topsemic/nuc972/kernel/NUC970_Linux_Kernel-master/
  4. all:
  5. $(MAKE) -C $(KDIR) M=$(PWD)
  6. clean:
  7. rm -rf *.o *.mod.c *.mod.o *.ko *.symvers *.order *.a

注意:KDIR 取决于你自己Linux内核安装的位置,一定要设置正确,否则编译会报错。

第3步:编译

将hello.c和Makefile放在同一路径下进行编译,输入make即可。编译成功后,会在当前路径下生成hello.ko,这就是我们将要加载到内核的模块。

第4步:将生成的hello.ko放到板子上,然后登录板子输入:

insmod hello.ko

如果模块加载成功的话,可以查看模块加载情况,使用lsmod命令

并且可以查看内核打印的消息,使用dmesg命令,

rmmod hello.ko,用来卸载模块,使用dmesg命令可以看到相关输出信息

4.MPU6050驱动

本章以板子上的MPU6050 传感器为例,来介绍驱动的编写。由于板子上使用的是PE10和PE11,它们不是真正的I2C引脚,所以这里我们使用GPIO来模拟I2C时序。编写驱动前,首先需要下载被控制器件的datasheet,在官网https://www.invensense.com/products/motion-tracking/6-axis/mpu-6050/  可以下载。

第1步:写驱动文件,我们这里在驱动文件里放了三个文件,分别为mpu6050.c、mpu6050bsp.c和mpu6050bsp.h

其中mpu6050.c代码如下:

  1. #include"mpu6050bsp.h"
  2. int MPU6050_MAJOR = 0;
  3. int MPU6050_MINOR = 0;
  4. int NUMBER_OF_DEVICES = 2;
  5. struct class *my_class;
  6. struct cdev cdev;
  7. dev_t devno;
  8. /*************************************************************************************/
  9. #define DRIVER_NAME "mpu6050"
  10. int mpu6050_open(struct inode *inode,struct file *filp)
  11. {
  12. u8 reg;
  13. reg=InitMPU6050();
  14. printk("mpu6050:%d\n",reg);
  15. return nonseekable_open(inode,filp);
  16. }
  17. long mpu6050_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
  18. {
  19. switch(cmd)
  20. {
  21. default:
  22. return -2;
  23. }
  24. return 0;
  25. }
  26. int mpu6050_read(struct file *filp, char *buffer,size_t count, loff_t *ppos)
  27. {
  28. mpu_get_data();
  29. return copy_to_user(buffer, mpu_data, 14);
  30. }
  31. int mpu6050_write(struct file *filp, char *buffer, size_t count, loff_t *ppos)
  32. {
  33. return 0;
  34. }
  35. struct file_operations mpu6050_fops = {
  36. .owner = THIS_MODULE,
  37. .read = mpu6050_read,
  38. .write = mpu6050_write,
  39. .open = mpu6050_open,
  40. .unlocked_ioctl = mpu6050_ioctl,
  41. };
  42. /**************************************************************************************/
  43. static int __init mpu6050_init(void)
  44. {
  45. int result;
  46. devno = MKDEV(MPU6050_MAJOR, MPU6050_MINOR);
  47. if (MPU6050_MAJOR)
  48. result = register_chrdev_region(devno, 2, "mpu6050");
  49. else
  50. {
  51. result = alloc_chrdev_region(&devno, 0, 2, "mpu6050");
  52. MPU6050_MAJOR = MAJOR(devno);
  53. }
  54. printk("MAJOR IS %d\n",MPU6050_MAJOR);
  55. my_class = class_create(THIS_MODULE,"mpu6050_class"); //类名为
  56. if(IS_ERR(my_class))
  57. {
  58. printk("Err: failed in creating class.\n");
  59. return -1;
  60. }
  61. device_create(my_class,NULL,devno,NULL,"mpu6050"); //设备名为mpu6050
  62. if (result<0)
  63. {
  64. printk (KERN_WARNING "hello: can't get major number %d\n", MPU6050_MAJOR);
  65. return result;
  66. }
  67. cdev_init(&cdev, &mpu6050_fops);
  68. cdev.owner = THIS_MODULE;
  69. cdev_add(&cdev, devno, NUMBER_OF_DEVICES);
  70. printk (KERN_INFO "mpu6050 driver Registered\n");
  71. return 0;
  72. }
  73. static void __exit mpu6050_exit (void)
  74. {
  75. cdev_del (&cdev);
  76. device_destroy(my_class, devno); //delete device node under /dev//必须先删除设备,再删除class类
  77. class_destroy(my_class); //delete class created by us
  78. unregister_chrdev_region (devno,NUMBER_OF_DEVICES);
  79. printk (KERN_INFO "char driver cleaned up\n");
  80. }
  81. module_init (mpu6050_init );
  82. module_exit (mpu6050_exit );
  83. MODULE_LICENSE ("GPL");

上述代码整体结构和第3章介绍的hello.c类似,不过为了支持对字符设备的操作,多了open/write/read的几个函数实现。

mpu6050bsp.c由于内容较多,不把代码贴到这里了,大家一看就明白了,它就是用gpio来模拟i2c功能,实现寄存器操作功能。mpu6050bsp.h主要是相关寄存器定义。

第2步:编译,然后把ko文件放到板子,insmod mpu6050d.ko 。模块如果加载成功,在/dev目录下可以看到mpu6050的设备名出现。

第3步:写个应用程序mpu6050app.c,

  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. short x_accel, y_accel, z_accel;
  6. short x_gyro, y_gyro, z_gyro;
  7. short temp;
  8. int main()
  9. {
  10. char buffer[128];
  11. short *value;
  12. int in, out;
  13. int nread;
  14. in = open("/dev/mpu6050", O_RDONLY);
  15. if (!in) {
  16. printf("ERROR: %d, Open /dev/mpu6050 failed.\n", -1);
  17. return -1;
  18. }
  19. nread = read(in, buffer, 12);
  20. close(in);
  21. if (nread < 0) {
  22. printf("ERROR: %d, A read error has occurred\n", nread);
  23. return -1;
  24. }
  25. value = (short*)buffer;
  26. x_accel = *(value);
  27. y_accel = *(value + 1);
  28. z_accel = *(value + 2);
  29. temp = *(value + 3);
  30. x_gyro = *(value + 4);
  31. y_gyro = *(value + 5);
  32. z_gyro = *(value + 6);
  33. printf("x accel is: %d \n", x_accel);
  34. printf("y accel is: %d \n", y_accel);
  35. printf("z accel is: %d \n", z_accel);
  36. printf("x gyro is: %d \n", x_gyro);
  37. printf("y gyro is: %d \n", y_gyro);
  38. printf("z gyro is: %d \n", z_gyro);
  39. printf("temperature is: %d \n", temp);
  40. exit(0);
  41. }

编译arm-linux-gcc mpu6050app.c -o mpu6050app

第4步:将板子水平摆放朝上,运行例子结果如下,

我们来计算下z轴加速度和温度的实际数值。

因为驱动里AFS_SEL寄存器设置的值是2,所以对应量程8g。数字-32767对应-8g,32767对应8g。把32767除以8,就可以得到4096,即1g对应的数值。把从加速度计读出的数字除以4096,就可以换算成加速度的数值。上面我们从加速度计z轴读到的数字是3723,那么对应的加速度数据是3723/4096≈0.91g。g为加速度的单位,重力加速度定义为1g, 等于9.8米每平方秒。由于桌上不是很平,加上传感器自身误差,所以这个值是合理的。

再看看温度计算,从手册中可以看到如下的计算公式

上述的-2352计算后得到温度为29.6℃,注意这个温度不是环境温度,是芯片内部的温度,环境温度会比这个值略低。

由于我是在北京,冬天屋里有暖气,所以这个值也是合理的。

5.结束语

 

本期给大家介绍关于Linux驱动最简单的使用,可以看到驱动开发和应用开发还是有很大的差异,驱动需要关注底层,需要深入的阅读芯片的数据手册,同时也得具备内核的相关知识。市场上Linux应用开发人员相对更多,真正懂驱动的人相对较少,大部分集中在芯片原厂公司。推荐大家在实际做产品时尽量选择官方推荐的元器件,或者选择可以提供Linux驱动的元器件,以降低开发难度。

6.参考资料

https://blog.csdn.net/u010632165/article/details/86541941

https://blog.csdn.net/u010632165/article/month/2019/05

https://www.cnblogs.com/wulei0630/p/9498195.html

https://www.cnblogs.com/amanlikethis/p/4914510.html

https://segmentfault.com/a/1190000020905713

https://blog.csdn.net/m0_37777700/article/details/84305584

https://www.cnblogs.com/sawyer22/p/9652294.html 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号