搜索
bottom↓
回复: 1

【正点原子Linux连载】第五十四章 platform设备驱动实验--摘自【正点原子】I.MX6U嵌入式Linux驱动开发指南

[复制链接]

出0入234汤圆

发表于 2020-7-6 10:29:41 | 显示全部楼层 |阅读模式
本帖最后由 正点原子 于 2020-10-26 11:49 编辑

1)实验平台:正点原子阿尔法Linux开发板
2)平台购买地址:https://item.taobao.com/item.htm?id=603672744434
3)全套实验源码+手册+视频下载地址:http://www.openedv.com/thread-300792-1-1.html  
4)对正点原子Linux感兴趣的同学可以加群讨论:
876919289 QQ群头像.png
5)关注正点原子公众号,获取最新资料
100846rel79a9p4uelap24.jpg

100846f1ce1fg14zbg0va4.png


第五十四章 platform设备驱动实验


        我们在前面几章编写的设备驱动都非常的简单,都是对IO进行最简单的读写操作。像I2C、SPI、LCD等这些复杂外设的驱动就不能这么去写了,Linux系统要考虑到驱动的可重用性,因此提出了驱动的分离与分层这样的软件思路,在这个思路下诞生了我们将来最常打交道的platform设备驱动,也叫做平台设备驱动。本章我们就来学习一下Linux下的驱动分离与分层,以及plartorm框架下的设备驱动该如何不编写。



54.1 Linux驱动的分离与分层
54.1.1 驱动的分隔与分离
        对于Linux这样一个成熟、庞大、复杂的操作系统,代码的重用性非常重要,否则的话就会在Linux内核中存在大量无意义的重复代码。尤其是驱动程序,因为驱动程序占用了Linux内核代码量的大头,如果不对驱动程序加以管理,任由重复的代码肆意增加,那么用不了多久Linux内核的文件数量就庞大到无法接受的地步。
        假如现在有三个平台A、B和C,这三个平台(这里的平台说的是SOC)上都有MPU6050这个I2C接口的六轴传感器,按照我们写裸机I2C驱动的时候的思路,每个平台都有一个MPU6050的驱动,因此编写出来的最简单的驱动框架如图54.1.1所示:
image002.gif

图54.1.1 传统的I2C设备驱动

        从图54.1.1可以看出,每种平台下都有一个主机驱动和设备驱动,主机驱动肯定是必须要的,毕竟不同的平台其I2C控制器不同。但是右侧的设备驱动就没必要每个平台都写一个,因为不管对于那个SOC来说,MPU6050都是一样,通过I2C接口读取数据就行了,只需要一个MPU6050的驱动程序即可。如果在来几个I2C设备,比如AT24C02、FT5206(电容触摸屏)等,如果按照图54.1.1中的写法,那么设备端的驱动将会重复的编写好几次。显然在Linux驱动程序中这种写法是不推荐的,最好的做法就是每个平台的I2C控制器都提供一个统一的接口(也叫做主机驱动),每个设备的话也只提供一个驱动程序(设备驱动),每个设备通过统一的I2C接口驱动来访问,这样就可以大大简化驱动文件,比如54.1.1中三种平台下的MPU6050驱动框架就可以简化为图54.1.2所示:
image004.gif

图54.1.2 改进后的设备驱动

        实际的I2C驱动设备肯定有很多种,不止MPU6050这一个,那么实际的驱动架构如图54.1.3所示:
image006.gif

图54.1.3 分隔后的驱动框架

这个就是驱动的分隔,也就是将主机驱动和设备驱动分隔开来,比如I2C、SPI等等都会采用驱动分隔的方式来简化驱动的开发。在实际的驱动开发中,一般I2C主机控制器驱动已经由半导体厂家编写好了,而设备驱动一般也有设备器件的厂家编写好了,我们只需要提供设备信息即可,比如I2C设备的话提供设备连接到了哪个I2C接口上,I2C的速度是多少等等。相当于将设备信息从设备驱动中剥离开来,驱动使用标准方法去获取到设备信息(比如从设备树中获取到设备信息),然后根据获取到的设备信息来初始化设备。这样就相当于驱动只需要负责驱动,设备只需要设备,想办法将两者进行匹配即可。这个就是Linux中的总线(bus)、驱动(driver)和设备(device)模型,也就是常说的驱动分离。总线就是驱动和设备信息的月老,负责给两者牵线搭桥,如图54.1.4所示:
image008.gif

图54.1.4 Linux总线、驱动和设备模式

        当我们向系统注册一个驱动的时候,总线就会在右侧的设备中查找,看看有没有与之匹配的设备,如果有的话就将两者联系起来。同样的,当向系统中注册一个设备的时候,总线就会在左侧的驱动中查找看有没有与之匹配的设备,有的话也联系起来。Linux内核中大量的驱动程序都采用总线、驱动和设备模式,我们一会要重点讲解的platform驱动就是这一思想下的产物。
54.1.2 驱动的分层
        上一小节讲了驱动的分隔与分离,本节我们来简单看一下驱动的分层,大家应该听说过网络的7层模型,不同的层负责不同的内容。同样的,Linux下的驱动往往也是分层的,分层的目的也是为了在不同的层处理不同的内容。以其他书籍或者资料常常使用到的input(输入子系统,后面会有专门的章节详细的讲解)为例,简单介绍一下驱动的分层。input子系统负责管理所有跟输入有关的驱动,包括键盘、鼠标、触摸等,最底层的就是设备原始驱动,负责获取输入设备的原始值,获取到的输入事件上报给input核心层。input核心层会处理各种IO模型,并且提供file_operations操作集合。我们在编写输入设备驱动的时候只需要处理好输入事件的上报即可,至于如何处理这些上报的输入事件那是上层去考虑的,我们不用管。可以看出借助分层模型可以极大的简化我们的驱动编写,对于驱动编写来说非常的友好。
54.2 platform平台驱动模型简介
        前面我们讲了设备驱动的分离,并且引出了总线(bus)、驱动(driver)和设备(device)模型,比如I2C、SPI、USB等总线。但是在SOC中有些外设是没有总线这个概念的,但是又要使用总线、驱动和设备模型该怎么办呢?为了解决此问题,Linux提出了platform这个虚拟总线,相应的就有platform_driver和platform_device。
54.2.1platform总线
Linux系统内核使用bus_type结构体表示总线,此结构体定义在文件include/linux/device.h,bus_type结构体内容如下:
示例代码54.2.1.1 bus_type结构体代码段
  1. 1struct bus_type {
  2. 2        constchar*name;                /* 总线名字        */
  3. 3        constchar*dev_name;
  4. 4        struct device       *dev_root;
  5. 5        struct device_attribute *dev_attrs;
  6. 6                conststruct attribute_group **bus_groups;        /* 总线属性        */
  7. 7        conststruct attribute_group **dev_groups;        /* 设备属性        */
  8. 8        conststruct attribute_group **drv_groups;        /* 驱动属性        */
  9. 9
  10. 10        int(*match)(struct device *dev,struct device_driver *drv);
  11. 11        int(*uevent)(struct device *dev,struct kobj_uevent_env *env);
  12. 12        int(*probe)(struct device *dev);
  13. 13        int(*remove)(struct device *dev);
  14. 14        void(*shutdown)(struct device *dev);
  15. 15
  16. 16        int(*online)(struct device *dev);
  17. 17        int(*offline)(struct device *dev);
  18. 18        int(*suspend)(struct device *dev, pm_message_t state);
  19. 19        int(*resume)(struct device *dev);
  20. 20        conststruct dev_pm_ops *pm;
  21. 21        conststruct iommu_ops *iommu_ops;
  22. 22        struct subsys_private *p;
  23. 23        struct lock_class_key lock_key;
  24. 24};
复制代码

        第10行,match函数,此函数很重要,单词match的意思就是“匹配、相配”,因此此函数就是完成设备和驱动之间匹配的,总线就是使用match函数来根据注册的设备来查找对应的驱动,或者根据注册的驱动来查找相应的设备,因此每一条总线都必须实现此函数。match函数有两个参数:dev和drv,这两个参数分别为device和device_driver类型,也就是设备和驱动。
platform总线是bus_type的一个具体实例,定义在文件drivers/base/platform.c,platform总线定义如下:
示例代码54.2.1.2 platform总线实例
  1. 1struct bus_type platform_bus_type ={
  2. 2                .name               ="platform",
  3. 3        .dev_groups         = platform_dev_groups,
  4. 4        .match              = platform_match,
  5. 5        .uevent             = platform_uevent,
  6. 6        .pm                     =&platform_dev_pm_ops,
  7. 7};
复制代码

platform_bus_type就是platform平台总线,其中platform_match就是匹配函数。我们来看一下驱动和设备是如何匹配的,platform_match函数定义在文件drivers/base/platform.c中,函数内容如下所示:
示例代码54.2.1.3 platform总线实例
  1. 1staticint platform_match(struct device *dev,
  2. struct device_driver *drv)
  3. 2{
  4. 3        struct platform_device *pdev = to_platform_device(dev);
  5. 4        struct platform_driver *pdrv = to_platform_driver(drv);
  6. 5
  7. 6        /*When driver_override is set,only bind to the matching driver*/
  8. 7        if(pdev->driver_override)
  9. 8return!strcmp(pdev->driver_override, drv->name);
  10. 9
  11. 10        /* Attempt an OF style match first */
  12. 11        if(of_driver_match_device(dev, drv))
  13. 12        return1;
  14. 13
  15. 14        /* Then try ACPI style match */
  16. 15        if(acpi_driver_match_device(dev, drv))
  17. 16        return1;
  18. 17
  19. 18        /* Then try to match against the id table */
  20. 19        if(pdrv->id_table)
  21. 20        return platform_match_id(pdrv->id_table, pdev)!=NULL;
  22. 21
  23. 22        /* fall-back to driver name match */
  24. 23        return(strcmp(pdev->name, drv->name)==0);
  25. 24}
复制代码

驱动和设备的匹配有四种方法,我们依次来看一下:
        第11~12行,第一种匹配方式, OF类型的匹配,也就是设备树采用的匹配方式,of_driver_match_device函数定义在文件include/linux/of_device.h中。device_driver结构体(表示设备驱动)中有个名为of_match_table的成员变量,此成员变量保存着驱动的compatible匹配表,设备树中的每个设备节点的compatible属性会和of_match_table表中的所有成员比较,查看是否有相同的条目,如果有的话就表示设备和此驱动匹配,设备和驱动匹配成功以后probe函数就会执行。
        第15~16行,第二种匹配方式,ACPI匹配方式。
        第19~20行,第三种匹配方式,id_table匹配,每个platform_driver结构体有一个id_table成员变量,顾名思义,保存了很多id信息。这些id信息存放着这个platformd驱动所只是的驱动类型。
        第23行,第四种匹配方式,如果第三种匹配方式的id_table不存在的话就直接比较驱动和设备的name字段,看看是不是相等,如果相等的话就匹配成功。
对于支持设备树的Linux版本号,一般设备驱动为了兼容性都支持设备树和无设备树两种匹配方式。也就是第一种匹配方式一般都会存在,第三种和第四种只要存在一种就可以,一般用的最多的还是第四种,也就是直接比较驱动和设备的name字段,毕竟这种方式最简单了。
54.2.2 platform驱动
        platform_driver结构体表示platform驱动,此结构体定义在文件include/linux/platform_device.h中,内容如下:
示例代码54.2.2.1 platform_driver结构体
  1. 1struct platform_driver {
  2. 2        int(*probe)(struct platform_device *);
  3. 3        int(*remove)(struct platform_device *);
  4. 4        void(*shutdown)(struct platform_device *);
  5. 5        int(*suspend)(struct platform_device *, pm_message_t state);
  6. 6                int(*resume)(struct platform_device *);
  7. 7        struct device_driver driver;
  8. 8        conststruct platform_device_id *id_table;
  9. 9        bool prevent_deferred_probe;
  10. 10};
复制代码

第2行,probe函数,当驱动与设备匹配成功以后probe函数就会执行,非常重要的函数!!一般驱动的提供者会编写,如果自己要编写一个全新的驱动,那么probe就需要自行事项。
        第7行,driver成员,为device_driver结构体变量,Linux内核里面大量使用到了面向对象的思维,device_driver相当于基类,提供了最基础的驱动框架。plaform_driver继承了这个基类,然后在此基础上又添加了一些特有的成员变量。
        第8行,id_table表,也就是我们上一小节讲解platform总线匹配驱动和设备的时候采用的第三种方法,id_table是个表(也就是数组),每个元素的类型为platform_device_id,platform_device_id结构体内容如下:
示例代码54.2.2.2 platform_device_id结构体
  1. 1struct platform_device_id {
  2. 2        char name[PLATFORM_NAME_SIZE];
  3. 3        kernel_ulong_t driver_data;
  4. 4};
复制代码

device_driver结构体定义在include/linux/device.h,device_driver结构体内容如下:
示例代码54.2.2.3 device_driver结构体
  1. 1struct device_driver {
  2. 2constchar                *name;
  3. 3struct bus_type             *bus;
  4. 4
  5. 5struct module               *owner;
  6. 6constchar                *mod_name;/* used for built-in modules */
  7. 7
  8. 8bool suppress_bind_attrs;/* disables bind/unbind via sysfs */
  9. 9
  10. 10conststruct of_device_id                   *of_match_table;
  11. 11conststruct acpi_device_id         *acpi_match_table;
  12. 12
  13. 13int(*probe)(struct device *dev);
  14. 14int(*remove)(struct device *dev);
  15. 15void(*shutdown)(struct device *dev);
  16. 16int(*suspend)(struct device *dev, pm_message_t state);
  17. 17int(*resume)(struct device *dev);
  18. 18conststruct attribute_group **groups;
  19. 19
  20. 20conststruct dev_pm_ops *pm;
  21. 21
  22. 22struct driver_private *p;
  23. 23};
复制代码

第10行,of_match_table就是采用设备树的时候驱动使用的匹配表,同样是数组,每个匹配项都为of_device_id结构体类型,此结构体定义在文件include/linux/mod_devicetable.h中,内容如下:
示例代码54.2.2.4 of_device_id结构体
  1. 1struct of_device_id {
  2. 2char                name[32];
  3. 3char                type[32];
  4. 4char                compatible[128];
  5. 5constvoid        *data;
  6. 6};
复制代码

第4行的compatible非常重要,因为对于设备树而言,就是通过设备节点的compatible属性值和of_match_table中每个项目的compatible成员变量进行比较,如果有相等的就表示设备和此驱动匹配成功。
在编写platform驱动的时候,首先定义一个platform_driver结构体变量,然后实现结构体中的各个成员变量,重点是实现匹配方法以及probe函数。当驱动和设备匹配成功以后probe函数就会执行,具体的驱动程序在probe函数里面编写,比如字符设备驱动等等。
当我们定义并初始化好platform_driver结构体变量以后,需要在驱动入口函数里面调用platform_driver_register函数向Linux内核注册一个platform驱动,platform_driver_register函数原型如下所示:
int platform_driver_register (struct platform_driver         *driver)
        函数参数和返回值含义如下:
        driver:要注册的platform驱动。
        返回值:负数,失败;0,成功。
        还需要在驱动卸载函数中通过platform_driver_unregister函数卸载platform驱动,platform_driver_unregister函数原型如下:
void platform_driver_unregister(struct platform_driver *drv)
函数参数和返回值含义如下:
        drv:要卸载的platform驱动。
        返回值:无。
        platform驱动框架如下所示:
示例代码54.2.2.5 platform驱动框架
     
  1.   /* 设备结构体 */
  2. 1        struct xxx_dev{
  3. 2        struct cdev cdev;
  4. 3        /* 设备结构体其他具体内容 */
  5. 4        };
  6. 5
  7. 6        struct xxx_dev xxxdev;/* 定义个设备结构体变量 */
  8. 7
  9. 8        staticint xxx_open(struct inode *inode,struct file *filp)
  10. 9        {
  11. 10        /* 函数具体内容 */
  12. 11        return0;
  13. 12        }
  14. 13
  15. 14static ssize_t xxx_write(struct file *filp,constchar __user *buf,
  16. size_t cnt, loff_t *offt)
  17. 15        {
  18. 16        /* 函数具体内容 */
  19. 17        return0;
  20. 18        }
  21. 19
  22. 20/*
  23. 21  * 字符设备驱动操作集
  24. 22  */
  25. 23        staticstruct file_operations xxx_fops ={
  26. 24        .owner = THIS_MODULE,
  27. 25        .open = xxx_open,
  28. 26        .write = xxx_write,
  29. 27        };
  30. 28
  31. 29/*
  32. 30  * platform驱动的probe函数
  33. 31  * 驱动与设备匹配成功以后此函数就会执行
  34. 32  */
  35. 33        staticint xxx_probe(struct platform_device *dev)
  36. 34        {
  37. 35        ......
  38. 36        cdev_init(&xxxdev.cdev,&xxx_fops);/* 注册字符设备驱动 */
  39. 37        /* 函数具体内容 */
  40. 38        return0;
  41. 39        }
  42. 40
  43. 41        staticintxxx_remove(struct platform_device *dev)
  44. 42        {
  45. 43        ......
  46. 44        cdev_del(&xxxdev.cdev);/*  删除cdev */
  47. 45        /* 函数具体内容 */
  48. 46        return0;
  49. 47        }
  50. 48
  51. 49/* 匹配列表 */
  52. 50staticconststruct of_device_id xxx_of_match[]={
  53. 51        {.compatible ="xxx-gpio"},
  54. 52                {/* Sentinel */}
  55. 53};
  56. 54
  57. 55/*
  58. 56  * platform平台驱动结构体
  59. 57  */
  60. 58        staticstruct platform_driver xxx_driver ={
  61. 59        .driver ={
  62. 60        .name       ="xxx",
  63. 61        .of_match_table = xxx_of_match,
  64. 62        },
  65. 63        .probe      = xxx_probe,
  66. 64        .remove     = xxx_remove,
  67. 65        };
  68. 66
  69. 67        /* 驱动模块加载 */
  70. 68        staticint __init xxxdriver_init(void)
  71. 69        {
  72. 70        return platform_driver_register(&xxx_driver);
  73. 71}
  74. 72
  75. 73        /* 驱动模块卸载 */
  76. 74        staticvoid __exit xxxdriver_exit(void)
  77. 75        {
  78. 76        platform_driver_unregister(&xxx_driver);
  79. 77        }
  80. 78
  81. 79        module_init(xxxdriver_init);
  82. 80        module_exit(xxxdriver_exit);
  83. 81        MODULE_LICENSE("GPL");
  84. 82        MODULE_AUTHOR("zuozhongkai");
复制代码

        第1~27行,传统的字符设备驱动,所谓的platform驱动并不是独立于字符设备驱动、块设备驱动和网络设备驱动之外的其他种类的驱动。platform只是为了驱动的分离与分层而提出来的一种框架,其驱动的具体实现还是需要字符设备驱动、块设备驱动或网络设备驱动。
第33~39行,xxx_probe函数,当驱动和设备匹配成功以后此函数就会执行,以前在驱动入口init函数里面编写的字符设备驱动程序就全部放到此probe函数里面。比如注册字符设备驱动、添加cdev、创建类等等。
第41~47行,xxx_remove函数,platform_driver结构体中的remove成员变量,当关闭platfor备驱动的时候此函数就会执行,以前在驱动卸载exit函数里面要做的事情就放到此函数中来。比如,使用iounmap释放内存、删除cdev,注销设备号等等。
第50~53行,xxx_of_match匹配表,如果使用设备树的话将通过此匹配表进行驱动和设备的匹配。第51行设置了一个匹配项,此匹配项的compatible值为“xxx-gpio”,因此当设备树中设备节点的compatible属性值为“xxx-gpio”的时候此设备就会与此驱动匹配。第52行是一个标记,of_device_id表最后一个匹配项必须是空的。
第58~65行,定义一个platform_driver结构体变量xxx_driver,表示platform驱动,第59~62行设置paltform_driver中的device_driver成员变量的name和of_match_table这两个属性。其中name属性用于传统的驱动与设备匹配,也就是检查驱动和设备的name字段是不是相同。of_match_table属性就是用于设备树下的驱动与设备检查。对于一个完整的驱动程序,必须提供有设备树和无设备树两种匹配方法。最后63和64这两行设置probe和remove这两成员变量。
第68~71行,驱动入口函数,调用platform_driver_register函数向Linux内核注册一个platform驱动,也就是上面定义的xxx_driver结构体变量。
        第74~77行,驱动出口函数,调用platform_driver_unregister函数卸载前面注册的platform驱动。
总体来说,platform驱动还是传统的字符设备驱动、块设备驱动或网络设备驱动,只是套上了一张“platform”这张皮皮,目的是为了使用总线、驱动和设备这个驱动模型来实现驱动的分离与分层。
54.2.3 platform设备
        platform驱动已经准备好了,我们还需要platform设备,否则的话单单一个驱动也做不了什么。platform_device这个结构体表示platform设备,这里我们要注意,如果内核支持设备树的话就不要在使用platform_device来描述设备了,因为改用设备树去描述了。当然了,你如果一定要用platform_device来描述设备信息的话也是可以的。platform_device结构体定义在文件include/linux/platform_device.h中,结构体内容如下:
示例代码54.2.3.1 platform_device结构体代码段
  1. 22struct platform_device {
  2. 23        constchar*name;
  3. 24        int     id;
  4. 25        bool        id_auto;
  5. 26        struct device   dev;
  6. 27        u32     num_resources;
  7. 28        struct resource *resource;
  8. 29
  9. 30                conststruct platform_device_id *id_entry;
  10. 31        char*driver_override;/* Driver name to force a match */
  11. 32
  12. 33        /* MFD cell pointer */
  13. 34        struct mfd_cell *mfd_cell;
  14. 35
  15. 36        /* arch specific additions */
  16. 37        struct pdev_archdata    archdata;
  17. 38};
  18.         
复制代码

        第23行,name表示设备名字,要和所使用的platform驱动的name字段相同,否则的话设备就无法匹配到对应的驱动。比如对应的platform驱动的name字段为“xxx-gpio”,那么此name字段也要设置为“xxx-gpio”。
        第27行,num_resources表示资源数量,一般为第28行resource资源的大小。
        第28行,resource表示资源,也就是设备信息,比如外设寄存器等。Linux内核使用resource结构体表示资源,resource结构体内容如下:
示例代码54.2.3.2 resource结构体代码段
  1. 18struct resource {
  2. 19        resource_size_t         start;
  3. 20        resource_size_t         end;
  4. 21        constchar                *name;
  5. 22        unsignedlong                flags;
  6. 23        struct resource         *parent,*sibling,*child;
  7. 24};
复制代码

        start和end分别表示资源的起始和终止信息,对于内存类的资源,就表示内存起始和终止地址,name表示资源名字,flags表示资源类型,可选的资源类型都定义在了文件include/linux/ioport.h里面,如下所示:
示例代码54.2.3.3 资源类型
  1. 29  #define IORESOURCE_BITS                     0x000000ff/* Bus-specific bits */
  2. 30
  3. 31  #define IORESOURCE_TYPE_BITS        0x00001f00/* Resource type         */
  4. 32  #define IORESOURCE_IO                       0x00000100/* PCI/ISA I/O ports */
  5. 33  #define IORESOURCE_MEM                      0x00000200
  6. 34  #define IORESOURCE_REG                      0x00000300/* Register offsets */
  7. 35  #define IORESOURCE_IRQ                      0x00000400
  8. 36  #define IORESOURCE_DMA                      0x00000800
  9. 37  #define IORESOURCE_BUS                     0x00001000
  10. ......
  11. 104/* PCI control bits.  Shares IORESOURCE_BITS with above PCI ROM.  */
  12. 105 #define IORESOURCE_PCI_FIXED           (1<<4)/* Do not move resource */
复制代码

在以前不支持设备树的Linux版本中,用户需要编写platform_device变量来描述设备信息,然后使用platform_device_register函数将设备信息注册到Linux内核中,此函数原型如下所示:
int platform_device_register(struct platform_device *pdev)
        函数参数和返回值含义如下:
        pdev:要注册的platform设备。
        返回值:负数,失败;0,成功。
如果不再使用platform的话可以通过platform_device_unregister函数注销掉相应的platform设备,platform_device_unregister函数原型如下:
  1. void platform_device_unregister(struct platform_device *pdev)
复制代码

        函数参数和返回值含义如下:
        pdev:要注销的platform设备。
        返回值:无。
platform设备信息框架如下所示:
示例代码54.2.3.4 platform设备框架
  1. 1/* 寄存器地址定义*/
  2. 2  #define PERIPH1_REGISTER_BASE          (0X20000000)/* 外设1寄存器首地址 */
  3. 3  #define PERIPH2_REGISTER_BASE          (0X020E0068)/* 外设2寄存器首地址 */
  4. 4  #define REGISTER_LENGTH                  4
  5. 5
  6. 6/* 资源 */
  7. 7staticstruct resource xxx_resources[]={
  8. 8        [0]={
  9. 9                .start  = PERIPH1_REGISTER_BASE,
  10. 10        .end    =(PERIPH1_REGISTER_BASE + REGISTER_LENGTH -1),
  11. 11        .flags  = IORESOURCE_MEM,
  12. 12        },
  13. 13        [1]={
  14. 14        .start  = PERIPH2_REGISTER_BASE,
  15. 15        .end    =(PERIPH2_REGISTER_BASE + REGISTER_LENGTH -1),
  16. 16        .flags  = IORESOURCE_MEM,
  17. 17        },
  18. 18};
  19. 19
  20. 20/* platform设备结构体 */
  21. 21staticstruct platform_device xxxdevice ={
  22. 22        .name ="xxx-gpio",
  23. 23        .id =-1,
  24. 24        .num_resources = ARRAY_SIZE(xxx_resources),
  25. 25        .resource = xxx_resources,
  26. 26};
  27. 27
  28. 28/* 设备模块加载 */
  29. 29staticint __init xxxdevice_init(void)
  30. 30{
  31. 31        return platform_device_register(&xxxdevice);
  32. 32}
  33. 33
  34. 34/* 设备模块注销 */
  35. 35staticvoid __exit xxx_resourcesdevice_exit(void)
  36. 36{
  37. 37        platform_device_unregister(&xxxdevice);
  38. 38}
  39. 39
  40. 40 module_init(xxxdevice_init);
  41. 41 module_exit(xxxdevice_exit);
  42. 42 MODULE_LICENSE("GPL");
  43. 43 MODULE_AUTHOR("zuozhongkai");
复制代码

        第7~18行,数组xxx_resources表示设备资源,一共有两个资源,分别为设备外设1和外设2的寄存器信息。因此flags都为IORESOURCE_MEM,表示资源为内存类型的。
        第21~26行,platform设备结构体变量,注意name字段要和所使用的驱动中的name字段一致,否则驱动和设备无法匹配成功。num_resources表示资源大小,其实就是数组xxx_resources的元素数量,这里用ARRAY_SIZE来测量一个数组的元素个数。
        第29~32行,设备模块加载函数,在此函数中调用platform_device_register向Linux内核注册platform设备。
        第35~38行,设备模块卸载函数,在此函数中调用platform_device_unregister从Linux内核中卸载platform设备。
        示例代码54.2.3.4主要是在不支持设备树的Linux版本中使用的,当Linux内核支持了设备树以后就不需要用户手动去注册platform设备了。因为设备信息都放到了设备树中去描述,Linux内核启动的时候会从设备树中读取设备信息,然后将其组织成platform_device形式,至于设备树到platform_device的具体过程就不去详细的追究了,感兴趣的可以去看一下,网上也有很多博客详细的讲解了整个过程。
        关于platform下的总线、驱动和设备就讲解到这里,我们接下来就使用platform驱动框架来编写一个LED灯驱动,本章我们不使用设备树来描述设备信息,我们采用自定义platform_device这种“古老”方式来编写LED的设备信息。下一章我们来编写设备树下的platform驱动,这样我们就掌握了无设备树和有设备树这两种platform驱动的开发方式。
54.3硬件原理图分析
本章实验我们只使用到IMX6U-ALPHA开发板上的LED灯,因此实验硬件原理图参考8.3小节即可。
54.4试验程序编写
本实验对应的例程路径为:开发板光盘->2、Linux驱动例程->17_platform。
本章实验我们需要编写一个驱动模块和一个设备模块,其中驱动模块是platform驱动程序,设备模块是platform的设备信息。当这两个模块都加载成功以后就会匹配成功,然后platform驱动模块中的probe函数就会执行,probe函数中就是传统的字符设备驱动那一套。
54.4.1platform设备与驱动程序编写
新建名为“17_platform”的文件夹,然后在17_platform文件夹里面创建vscode工程,工作区命名为“platform”。新建名为leddevice.c和leddriver.c这两个文件,这两个文件分别为LED灯的platform设备文件和LED灯的platform的驱动文件。在leddevice.c中输入如下所示内容:
示例代码54.4.1.1 leddevice.c文件代码段
  1. 1   #include <linux/types.h>
  2. 2   #include <linux/kernel.h>
  3. 3   #include <linux/delay.h>
  4. 4   #include <linux/ide.h>
  5. 5   #include <linux/init.h>
  6. 6   #include <linux/module.h>
  7. 7   #include <linux/errno.h>
  8. 8   #include <linux/gpio.h>
  9. 9   #include <linux/cdev.h>
  10. 10  #include <linux/device.h>
  11. 11  #include <linux/of_gpio.h>
  12. 12  #include <linux/semaphore.h>
  13. 13  #include <linux/timer.h>
  14. 14  #include <linux/irq.h>
  15. 15  #include <linux/wait.h>
  16. 16  #include <linux/poll.h>
  17. 17  #include <linux/fs.h>
  18. 18  #include <linux/fcntl.h>
  19. 19  #include <linux/platform_device.h>
  20. 20  #include <asm/mach/map.h>
  21. 21  #include <asm/uaccess.h>
  22. 22  #include <asm/io.h>
  23. 23/***************************************************************
  24. 24  Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
  25. 25文件名        : leddevice.c
  26. 26作者        : 左忠凯
  27. 27版本        : V1.0
  28. 28描述        : platform设备
  29. 29其他        : 无
  30. 30论坛        : <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  31. 31日志        : 初版V1.0 2019/8/13 左忠凯创建
  32. 32  ***************************************************************/
  33. 33
  34. 34/*
  35. 35   * 寄存器地址定义
  36. 36   */
  37. 37  #define CCM_CCGR1_BASE                    (0X020C406C)
  38. 38  #define SW_MUX_GPIO1_IO03_BASE      (0X020E0068)
  39. 39  #define SW_PAD_GPIO1_IO03_BASE      (0X020E02F4)
  40. 40  #define GPIO1_DR_BASE                      (0X0209C000)
  41. 41  #define GPIO1_GDIR_BASE                     (0X0209C004)
  42. 42  #define REGISTER_LENGTH                     4
  43. 43
  44. 44/* @description         : 释放flatform设备模块的时候此函数会执行
  45. 45   * @param - dev         : 要释放的设备
  46. 46   * @return                : 无
  47. 47   */
  48. 48staticvoid led_release(struct device *dev)
  49. 49{
  50. 50      printk("led device released!\r\n");
  51. 51}
  52. 52
  53. 53/*  
  54. 54   * 设备资源信息,也就是LED0所使用的所有寄存器
  55. 55   */
  56. 56staticstruct resource led_resources[]={
  57. 57[0]={
  58. 58.start  = CCM_CCGR1_BASE,
  59. 59.end    =(CCM_CCGR1_BASE + REGISTER_LENGTH -1),
  60. 60.flags  = IORESOURCE_MEM,
  61. 61},
  62. 62[1]={
  63. 63.start  = SW_MUX_GPIO1_IO03_BASE,
  64. 64.end    =(SW_MUX_GPIO1_IO03_BASE + REGISTER_LENGTH -1),
  65. 65.flags  = IORESOURCE_MEM,
  66. 66},
  67. 67[2]={
  68. 68.start  = SW_PAD_GPIO1_IO03_BASE,
  69. 69.end    =(SW_PAD_GPIO1_IO03_BASE + REGISTER_LENGTH -1),
  70. 70.flags  = IORESOURCE_MEM,
  71. 71},
  72. 72[3]={
  73. 73.start  = GPIO1_DR_BASE,
  74. 74.end    =(GPIO1_DR_BASE + REGISTER_LENGTH -1),
  75. 75.flags  = IORESOURCE_MEM,
  76. 76},
  77. 77[4]={
  78. 78.start  = GPIO1_GDIR_BASE,
  79. 79.end    =(GPIO1_GDIR_BASE + REGISTER_LENGTH -1),
  80. 80.flags  = IORESOURCE_MEM,
  81. 81},
  82. 82};
  83. 83
  84. 84
  85. 85/*
  86. 86   * platform设备结构体
  87. 87   */
  88. 88staticstruct platform_device leddevice ={
  89. 89.name ="imx6ul-led",
  90. 90.id =-1,
  91. 91.dev ={
  92. 92.release =&led_release,
  93. 93},
  94. 94.num_resources = ARRAY_SIZE(led_resources),
  95. 95.resource = led_resources,
  96. 96};
  97. 97
  98. 98/*
  99. 99   * @description         : 设备模块加载
  100. 100  * @param               : 无
  101. 101  * @return              : 无
  102. 102  */
  103. 103staticint __init leddevice_init(void)
  104. 104{
  105. 105return platform_device_register(&leddevice);
  106. 106}
  107. 107
  108. 108/*
  109. 109  * @description         : 设备模块注销
  110. 110  * @param               : 无
  111. 111  * @return              : 无
  112. 112  */
  113. 113staticvoid __exit leddevice_exit(void)
  114. 114{
  115. 115     platform_device_unregister(&leddevice);
  116. 116}
  117. 117
  118. 118 module_init(leddevice_init);
  119. 119 module_exit(leddevice_exit);
  120. 120 MODULE_LICENSE("GPL");
  121. 121 MODULE_AUTHOR("zuozhongkai");
复制代码

        leddevice.c文件内容就是按照示例代码54.2.3.4的platform设备模板编写的。
        第56~82行,led_resources数组,也就是设备资源,描述了LED所要使用到的寄存器信息,也就是IORESOURCE_MEM资源。
        第88~96,platform设备结构体变量leddevice,这里要注意name字段为“imx6ul-led”,所以稍后编写platform驱动中的name字段也要为“imx6ul-led”,否则设备和驱动匹配失败。
        第103~106行,设备模块加载函数,在此函数里面通过platform_device_register向Linux内核注册leddevice这个platform设备。
        第113~116行,设备模块卸载函数,在此函数里面通过platform_device_unregister从Linux内核中删除掉leddevice这个platform设备。
        leddevice.c文件编写完成以后就编写leddriver.c这个platform驱动文件,在leddriver.c里面输入如下内容:
示例代码54.4.1.2 leddriver.c文件代码段
  1. 1   #include <linux/types.h>
  2. 2   #include <linux/kernel.h>
  3. 3   #include <linux/delay.h>
  4. 4   #include <linux/ide.h>
  5. 5   #include <linux/init.h>
  6. 6   #include <linux/module.h>
  7. 7   #include <linux/errno.h>
  8. 8   #include <linux/gpio.h>
  9. 9   #include <linux/cdev.h>
  10. 10  #include <linux/device.h>
  11. 11  #include <linux/of_gpio.h>
  12. 12  #include <linux/semaphore.h>
  13. 13  #include <linux/timer.h>
  14. 14  #include <linux/irq.h>
  15. 15  #include <linux/wait.h>
  16. 16  #include <linux/poll.h>
  17. 17  #include <linux/fs.h>
  18. 18  #include <linux/fcntl.h>
  19. 19  #include <linux/platform_device.h>
  20. 20  #include <asm/mach/map.h>
  21. 21  #include <asm/uaccess.h>
  22. 22  #include <asm/io.h>
  23. 23/***************************************************************
  24. 24  Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
  25. 25文件名                : leddriver.c
  26. 26作者        : 左忠凯
  27. 27版本        : V1.0
  28. 28描述        : platform驱动
  29. 29其他        : 无
  30. 30论坛        : <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  31. 31日志        : 初版V1.0 2019/8/13 左忠凯创建
  32. 32  ***************************************************************/
  33. 33
  34. 34  #define LEDDEV_CNT                   1                /* 设备号长度        */
  35. 35  #define LEDDEV_NAME                  "platled"        /* 设备名字     */
  36. 36  #define LEDOFF                  0
  37. 37  #define LEDON                   1
  38. 38
  39. 39/* 寄存器名 */
  40. 40staticvoid __iomem *IMX6U_CCM_CCGR1;
  41. 41staticvoid __iomem *SW_MUX_GPIO1_IO03;
  42. 42staticvoid __iomem *SW_PAD_GPIO1_IO03;
  43. 43staticvoid __iomem *GPIO1_DR;
  44. 44staticvoid __iomem *GPIO1_GDIR;
  45. 45
  46. 46/* leddev设备结构体 */
  47. 47struct leddev_dev{
  48. 48      dev_t devid;        /* 设备号        */
  49. 49struct cdev cdev;        /* cdev             */
  50. 50struct class *class;        /* 类                */
  51. 51struct device *device;        /* 设备        */
  52. 52int major;        /* 主设备号                */
  53. 53};
  54. 54
  55. 55struct leddev_dev leddev;        /* led设备        */
  56. 56
  57. 57/*
  58. 58   * @description         : LED打开/关闭
  59. 59   * @param - sta          : LEDON(0) 打开LED,LEDOFF(1) 关闭LED
  60. 60   * @return               : 无
  61. 61   */
  62. 62void led0_switch(u8 sta)
  63. 63{
  64. 64      u32 val =0;
  65. 65if(sta == LEDON){
  66. 66          val = readl(GPIO1_DR);
  67. 67          val &=~(1<<3);
  68. 68          writel(val, GPIO1_DR);
  69. 69}elseif(sta == LEDOFF){
  70. 70          val = readl(GPIO1_DR);
  71. 71          val|=(1<<3);
  72. 72          writel(val, GPIO1_DR);
  73. 73}
  74. 74}
  75. 75
  76. 76/*
  77. 77   * @description          : 打开设备
  78. 78   * @param – inode        : 传递给驱动的inode
  79. 79   * @param - filp         : 设备文件,file结构体有个叫做private_data的成员变量
  80. 80   *                    一般在open的时候将private_data指向设备结构体。
  81. 81   * @return                : 0 成功;其他失败
  82. 82   */
  83. 83staticint led_open(struct inode *inode,struct file *filp)
  84. 84{
  85. 85      filp->private_data =&leddev;/* 设置私有数据  */
  86. 86return0;
  87. 87}
  88. 88
  89. 89/*
  90. 90   * @description         : 向设备写数据
  91. 91   * @param – filp        : 设备文件,表示打开的文件描述符
  92. 92   * @param - buf          : 要写给设备写入的数据
  93. 93   * @param - cnt          : 要写入的数据长度
  94. 94   * @param - offt         : 相对于文件首地址的偏移
  95. 95   * @return               : 写入的字节数,如果为负值,表示写入失败
  96. 96   */
  97. 97static ssize_t led_write(struct file *filp,constchar __user *buf,
  98. size_t cnt, loff_t *offt)
  99. 98{
  100. 99int retvalue;
  101. 100unsignedchar databuf[1];
  102. 101unsignedchar ledstat;
  103. 102
  104. 103     retvalue = copy_from_user(databuf, buf, cnt);
  105. 104if(retvalue <0){
  106. 105return-EFAULT;
  107. 106}
  108. 107
  109. 108     ledstat = databuf[0];        /* 获取状态值        */
  110. 109if(ledstat == LEDON){
  111. 110         led0_switch(LEDON);        /* 打开LED灯        */
  112. 111}elseif(ledstat == LEDOFF){
  113. 112         led0_switch(LEDOFF);        /* 关闭LED灯        */
  114. 113}
  115. 114return0;
  116. 115}
  117. 116
  118. 117/* 设备操作函数 */
  119. 118staticstruct file_operations led_fops ={
  120. 119.owner = THIS_MODULE,
  121. 120.open = led_open,
  122. 121.write = led_write,
  123. 122};
  124. 123
  125. 124/*
  126. 125  * @description        : flatform驱动的probe函数,当驱动与设备
  127. 126  *                  匹配以后此函数就会执行
  128. 127  * @param - dev          : platform设备
  129. 128  * @return                : 0,成功;其他负值,失败
  130. 129  */
  131. 130staticint led_probe(struct platform_device *dev)
  132. 131{
  133. 132int i =0;
  134. 133int ressize[5];
  135. 134     u32 val =0;
  136. 135struct resource *ledsource[5];
  137. 136
  138. 137     printk("led driver and device has matched!\r\n");
  139. 138/* 1、获取资源 */
  140. 139for(i =0; i <5; i++){
  141. 140         ledsource= platform_get_resource(dev, IORESOURCE_MEM, i);
  142. 141if(!ledsource){
  143. 142             dev_err(&dev->dev,"No MEM resource for always on\n");
  144. 143return-ENXIO;
  145. 144}
  146. 145         ressize= resource_size(ledsource);
  147. 146}
  148. 147
  149. 148/* 2、初始化LED */
  150. 149/* 寄存器地址映射 */
  151. 150     IMX6U_CCM_CCGR1 = ioremap(ledsource[0]->start, ressize[0]);
  152. 151     SW_MUX_GPIO1_IO03 = ioremap(ledsource[1]->start, ressize[1]);
  153. 152     SW_PAD_GPIO1_IO03 = ioremap(ledsource[2]->start, ressize[2]);
  154. 153     GPIO1_DR = ioremap(ledsource[3]->start, ressize[3]);
  155. 154     GPIO1_GDIR = ioremap(ledsource[4]->start, ressize[4]);
  156. 155
  157. 156     val = readl(IMX6U_CCM_CCGR1);
  158. 157     val &=~(3<<26);        /* 清除以前的设置        */
  159. 158     val |=(3<<26);        /* 设置新值                        */
  160. 159     writel(val, IMX6U_CCM_CCGR1);
  161. 160
  162. 161/* 设置GPIO1_IO03复用功能,将其复用为GPIO1_IO03 */
  163. 162     writel(5, SW_MUX_GPIO1_IO03);
  164. 163     writel(0x10B0, SW_PAD_GPIO1_IO03);
  165. 164
  166. 165/* 设置GPIO1_IO03为输出功能 */
  167. 166     val = readl(GPIO1_GDIR);
  168. 167     val &=~(1<<3);                /* 清除以前的设置        */
  169. 168     val |=(1<<3);                /* 设置为输出                */
  170. 169     writel(val, GPIO1_GDIR);
  171. 170
  172. 171/* 默认关闭LED1 */
  173. 172     val = readl(GPIO1_DR);
  174. 173     val |=(1<<3);
  175. 174     writel(val, GPIO1_DR);
  176. 175
  177. 176/* 注册字符设备驱动 */
  178. 177/*1、创建设备号 */
  179. 178if(leddev.major){        /*  定义了设备号        */
  180. 179         leddev.devid = MKDEV(leddev.major,0);
  181. 180         register_chrdev_region(leddev.devid, LEDDEV_CNT,
  182. LEDDEV_NAME);
  183. 181}else{                /* 没有定义设备号        */
  184. 182         alloc_chrdev_region(&leddev.devid,0, LEDDEV_CNT,
  185. LEDDEV_NAME);        
  186. 183         leddev.major = MAJOR(leddev.devid);
  187. 184}
  188. 185
  189. 186/* 2、初始化cdev */
  190. 187     leddev.cdev.owner = THIS_MODULE;
  191. 188     cdev_init(&leddev.cdev,&led_fops);
  192. 189
  193. 190/* 3、添加一个cdev */
  194. 191     cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);
  195. 192
  196. 193/* 4、创建类 */
  197. 194     leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
  198. 195if(IS_ERR(leddev.class)){
  199. 196return PTR_ERR(leddev.class);
  200. 197}
  201. 198
  202. 199/* 5、创建设备 */
  203. 200     leddev.device = device_create(leddev.class,NULL, leddev.devid,
  204. NULL, LEDDEV_NAME);
  205. 201if(IS_ERR(leddev.device)){
  206. 202return PTR_ERR(leddev.device);
  207. 203}
  208. 204
  209. 205return0;
  210. 206}
  211. 207
  212. 208/*
  213. 209  * @description         :移除platform驱动的时候此函数会执行
  214. 210  * @param - dev          : platform设备
  215. 211  * @return               : 0,成功;其他负值,失败
  216. 212  */
  217. 213staticint led_remove(struct platform_device *dev)
  218. 214{
  219. 215     iounmap(IMX6U_CCM_CCGR1);
  220. 216     iounmap(SW_MUX_GPIO1_IO03);
  221. 217     iounmap(SW_PAD_GPIO1_IO03);
  222. 218     iounmap(GPIO1_DR);
  223. 219     iounmap(GPIO1_GDIR);
  224. 220
  225. 221     cdev_del(&leddev.cdev);                /*  删除cdev */
  226. 222     unregister_chrdev_region(leddev.devid, LEDDEV_CNT);
  227. 223     device_destroy(leddev.class, leddev.devid);
  228. 224     class_destroy(leddev.class);
  229. 225return0;
  230. 226}
  231. 227
  232. 228/* platform驱动结构体 */
  233. 229staticstruct platform_driver led_driver ={
  234. 230.driver     ={
  235. 231.name   ="imx6ul-led",/* 驱动名字,用于和设备匹配 */
  236. 232},
  237. 233.probe      = led_probe,
  238. 234.remove     = led_remove,
  239. 235};
  240. 236
  241. 237/*
  242. 238  * @description         : 驱动模块加载函数
  243. 239  * @param               : 无
  244. 240  * @return              : 无
  245. 241  */
  246. 242staticint __init leddriver_init(void)
  247. 243{
  248. 244return platform_driver_register(&led_driver);
  249. 245}
  250. 246
  251. 247/*
  252. 248  * @description         : 驱动模块卸载函数
  253. 249  * @param                    : 无
  254. 250  * @return              : 无
  255. 251  */
  256. 252staticvoid __exit leddriver_exit(void)
  257. 253{
  258. 254     platform_driver_unregister(&led_driver);
  259. 255}
  260. 256
  261. 257 module_init(leddriver_init);
  262. 258 module_exit(leddriver_exit);
  263. 259 MODULE_LICENSE("GPL");
  264. 260 MODULE_AUTHOR("zuozhongkai");
复制代码

        leddriver.c文件内容就是按照示例代码54.2.2.5的platform驱动模板编写的。
        第34~122行,传统的字符设备驱动。
        第130~206行,probe函数,当设备和驱动匹配以后此函数就会执行,当匹配成功以后会在终端上输出“led driver and device has matched!”这样语句。在probe函数里面初始化LED、注册字符设备驱动。也就是将原来在驱动加载函数里面做的工作全部放到probe函数里面完成。
        第213~226行,remobe函数,当卸载platform驱动的时候此函数就会执行。在此函数里面释放内存、注销字符设备等。也就是将原来驱动卸载函数里面的工作全部都放到remove函数中完成。
        第229~235行,platform_driver驱动结构体,注意name字段为"imx6ul-led",和我们在leddevice.c文件里面设置的设备name字段一致。
        第242~245行,驱动模块加载函数,在此函数里面通过platform_driver_register向Linux内核注册led_driver驱动。
        第252~255行,驱动模块卸载函数,在此函数里面通过platform_driver_unregister从Linux内核卸载led_driver驱动。
54.4.2 测试APP编写
        测试APP的内容很简单,就是打开和关闭LED灯,新建ledApp.c这个文件,然后在里面输入如下内容:
示例代码54.4.2.1 ledApp.c文件代码段
  1. 1  #include "stdio.h"
  2. 2  #include "unistd.h"
  3. 3  #include "sys/types.h"
  4. 4  #include "sys/stat.h"
  5. 5  #include "fcntl.h"
  6. 6  #include "stdlib.h"
  7. 7  #include "string.h"
  8. 8/***************************************************************
  9. 9  Copyright © ALIENTEK Co., Ltd. 1998-2029. All rights reserved.
  10. 10文件名                : ledApp.c
  11. 11作者        : 左忠凯
  12. 12版本        : V1.0
  13. 13描述        : platform驱动驱测试APP。
  14. 14其他        : 无
  15. 15使用方法                :./ledApp /dev/platled  0 关闭LED
  16. 16          ./ledApp /dev/platled  1 打开LED     
  17. 17论坛        : <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  18. 18日志        : 初版V1.0 2019/8/16 左忠凯创建
  19. 19 ***************************************************************/
  20. 20 #define LEDOFF   0
  21. 21 #define LEDON    1
  22. 22
  23. 23/*
  24. 24  * @description         : main主程序
  25. 25  * @param - argc         : argv数组元素个数
  26. 26  * @param - argv         : 具体参数
  27. 27  * @return                : 0 成功;其他失败
  28. 28  */
  29. 29int main(int argc,char*argv[])
  30. 30{
  31. 31        int fd, retvalue;
  32. 32        char*filename;
  33. 33        unsignedchar databuf[2];
  34. 34
  35. 35        if(argc !=3){
  36. 36                printf("Error Usage!\r\n");
  37. 37        return-1;
  38. 38        }
  39. 39
  40. 40        filename = argv[1];
  41. 41        /* 打开led驱动 */
  42. 42        fd = open(filename, O_RDWR);
  43. 43        if(fd <0){
  44. 44        printf("file %s open failed!\r\n", argv[1]);
  45. 45        return-1;
  46. 46        }
  47. 47
  48. 48        databuf[0]= atoi(argv[2]);/* 要执行的操作:打开或关闭 */
  49. 49        retvalue = write(fd, databuf,sizeof(databuf));
  50. 50        if(retvalue <0){
  51. 51        printf("LED Control Failed!\r\n");
  52. 52        close(fd);
  53. 53        return-1;
  54. 54        }
  55. 55
  56. 56        retvalue = close(fd);/* 关闭文件 */
  57. 57        if(retvalue <0){
  58. 58        printf("file %s close failed!\r\n", argv[1]);
  59. 59                return-1;
  60. 60        }
  61. 61        return0;
  62. 62}
复制代码

        ledApp.c文件内容很简单,就是控制LED灯的亮灭,和第四十一章的测试APP基本一致,这里就不重复讲解了。
54.5 运行测试
54.5.1 编译驱动程序和测试APP
1、编译驱动程序
        编写Makefile文件,本章实验的Makefile文件和第四十章实验基本一样,只是将obj-m变量的值改为“leddevice.o leddriver.o”,Makefile内容如下所示:
示例代码54.5.1.1 Makefile文件
  1. 1  KERNELDIR:= /home/zuozhongkai/linux/IMX6ULL/linux/temp/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek
  2. ......
  3. 4  obj-m := leddevice.o leddriver.o
  4. ......
  5. 11 clean:
  6. 12$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
复制代码

        第4行,设置obj-m变量的值为“leddevice.o leddriver.o”。
        输入如下命令编译出驱动模块文件:
  1. make-j32
复制代码

        编译成功以后就会生成一个名为“leddevice.ko leddriver.ko”的驱动模块文件。
        2、编译测试APP
        输入如下命令编译测试ledApp.c这个测试程序:
  1. arm-linux-gnueabihf-gccledApp.c -o ledApp
复制代码

        编译成功以后就会生成ledApp这个应用程序。
54.4.2 运行测试
        将上一小节编译出来leddevice.ko、leddriver.ko和ledApp这两个文件拷贝到rootfs/lib/modules/4.1.15目录中,重启开发板,进入到目录lib/modules/4.1.15中,输入如下命令加载leddevice.ko设备模块和leddriver.ko这个驱动模块。
  1. depmod                                //第一次加载驱动的时候需要运行此命令
  2. modprobe leddevice.ko        //加载设备模块
  3. modprobeleddriver.ko        //加载驱动模块
复制代码

        根文件系统中/sys/bus/platform/目录下保存着当前板子platform总线下的设备和驱动,其中devices子目录为platform设备,drivers子目录为plartofm驱动。查看/sys/bus/platform/devices/目录,看看我们的设备是否存在,我们在leddevice.c中设置leddevice(platform_device类型)的name字段为“imx6ul-led”,也就是设备名字为imx6ul-led,因此肯定在/sys/bus/platform/devices/目录下存在一个名字“imx6ul-led”的文件,否则说明我们的设备模块加载失败,结果如图54.4.2.1所示:
image010.gif

图54.4.2.1 imx6ul-led设备

        同理,查看/sys/bus/platform/drivers/目录,看一下驱动是否存在,我们在leddriver.c中设置led_driver(platform_driver类型)的name字段为“imx6ul-led”,因此会在/sys/bus/platform/drivers/目录下存在名为“imx6ul-led”这个文件,结果如图54.4.2.2所示:
image012.gif

图54.4.2.2 imx6ul-led驱动

驱动模块和设备模块加载成功以后platform总线就会进行匹配,当驱动和设备匹配成功以后就会输出如图54.4.2.3所示一行语句:
image014.gif

图54.4.2.3 驱动和设备匹配成功

        驱动和设备匹配成功以后就可以测试LED灯驱动了,输入如下命令打开LED灯:
  1. ./ledApp /dev/platled 1        //打开LED灯
  2.         在输入如下命令关闭LED灯:
  3. ./ledApp /dev/platled 0        //关闭LED灯
复制代码

        观察一下LED灯能否打开和关闭,如果可以的话就说明驱动工作正常,如果要卸载驱动的话输入如下命令即可:
  1. rmmod leddevice.ko
  2. rmmodleddriver.ko
复制代码


阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出16170入6148汤圆

发表于 2020-7-6 12:38:53 来自手机 | 显示全部楼层
打赏!

庆祝论坛“打赏”功能实施, 现在开始发技术主题,可以获得打赏
https://www.amobbs.com/thread-5735948-1-1.html
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-26 23:34

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表