在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            博客專欄

            EEPW首頁 > 博客 > 迅為i.MX6ULL終結者設備樹下的Platform驅動實驗程序編寫

            迅為i.MX6ULL終結者設備樹下的Platform驅動實驗程序編寫

            發(fā)布人:daybydayi 時間:2020-12-31 來源:工程師 發(fā)布文章
            文章目錄

             

             

            1 修改設備樹文件

            設備樹文件可以直接使用第三十五章中添加的gpioled子節(jié)點即可,不用重復添加。

            2 platform驅動程序

            本實驗例程路徑:i.MX6UL終結者光盤資料/06_Linux驅動例程/15_gpioled_dts
            創(chuàng)建led_driver.c文件,具體內容如下:


            _ueditor_page_break_tag_

             1 #include <linux/types.h>

              2 #include <linux/kernel.h>

              3 #include <linux/delay.h>

              4 #include <linux/ide.h>

              5 #include <linux/init.h>

              6 #include <linux/module.h>

              7 #include <linux/errno.h>

              8 #include <linux/gpio.h>

              9 #include <linux/cdev.h>

             10 #include <linux/device.h>

             11 #include <linux/of_gpio.h>

             12 #include <linux/semaphore.h>

             13 #include <linux/timer.h>

             14 #include <linux/irq.h>

             15 #include <linux/wait.h>

             16 #include <linux/poll.h>

             17 #include <linux/fs.h>

             18 #include <linux/fcntl.h>

             19 #include <linux/platform_device.h>

             20 #include <asm/mach/map.h>

             21 #include <asm/uaccess.h>

             22 #include <asm/io.h>

             23 

             24 #define LEDDEV_CNT              1                    /* 設備號長度   */

             25 #define LEDDEV_NAME             "dtsplatled"    /* 設備名字     */

             26 #define LEDOFF                  0

             27 #define LEDON                   1

             28 

             29 /* leddev設備結構體 */

             30 struct leddev_dev{

             31         dev_t devid;                            /* 設備號       */

             32         struct cdev cdev;                       /* cdev         */

             33         struct class *class;            /* 類           */

             34         struct device *device;          /* 設備 */

             35         int major;                     /* 主設備號     */

             36         struct device_node *node;       /* LED設備節(jié)點 */

             37         int led0;                      /* LED燈GPIO標號 */

             38 };

             39 

             40 struct leddev_dev leddev;               /* led設備 */

             41 

             42 /*

             43  * @description         : LED打開/關閉

             44  * @param - sta         : LEDON(0) 打開LED,LEDOFF(1) 關閉LED

             45  * @return                      : 無

             46  */

             47 void led0_switch(u8 sta)

             48 {

             49         if (sta == LEDON )

             50                 gpio_set_value(leddev.led0, 0);

             51         else if (sta == LEDOFF)

             52                 gpio_set_value(leddev.led0, 1);

             53 }

             54 

             55 /*

             56  * @description         : 打開設備

             57  * @param - inode       : 傳遞給驅動的inode

             58  * @param - filp        : 設備文件,file結構體有個叫做private_data的成員變量

             59  * 一般在open的時候將private_data指向設備結構體。

             60  * @return                      : 0 成功;其他 失敗

             61  */

             62 static int led_open(struct inode *inode, struct file *filp)

             63 {

             64         filp->private_data = &leddev; /* 設置私有數據  */

             65         return 0;

             66 }

             67 

             68 /*

             69  * @description         : 向設備寫數據 

             70  * @param - filp        : 設備文件,表示打開的文件描述符

             71  * @param - buf         : 要寫給設備寫入的數據

             72  * @param - cnt         : 要寫入的數據長度

             73  * @param - offt        : 相對于文件首地址的偏移

             74  * @return                      : 寫入的字節(jié)數,如果為負值,表示寫入失敗

             75  */

             76 static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)

             77 {

             78         int retvalue;

             79         unsigned char databuf[2];

             80         unsigned char ledstat;

             81 

             82         retvalue = copy_from_user(databuf, buf, cnt);

             83         if(retvalue < 0) {

             84 

             85                 printk("kernel write failed!\r\n");

             86                 return -EFAULT;

             87         }

             88 

             89         ledstat = databuf[0];

             90         if (ledstat == LEDON) {

             91                 led0_switch(LEDON);

             92         } else if (ledstat == LEDOFF) {

             93                 led0_switch(LEDOFF);

             94         }

             95         return 0;

             96 }

             97 

             98 /* 設備操作函數 */

             99 static struct file_operations led_fops = {

            100         .owner = THIS_MODULE,

            101         .open = led_open,

            102         .write = led_write,

            103 };

            104 

            105 /*

            106  * @description         : flatform驅動的probe函數,當驅動與

            107  *                                        設備匹配以后此函數就會執(zhí)行

            108  * @param - dev         : platform設備

            109  * @return                      : 0,成功;其他負值,失敗

            110  */

            111 static int led_probe(struct platform_device *dev)

            112 {

            113         printk("led driver and device was matched!\r\n");

            114         /* 1、設置設備號 */

            115         if (leddev.major) {

            116                 leddev.devid = MKDEV(leddev.major, 0);

            117               register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);

            118         } else {

            119              alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);

            120                 leddev.major = MAJOR(leddev.devid);

            121         }

            122 

            123         /* 2、注冊設備      */

            124         cdev_init(&leddev.cdev, &led_fops);

            125         cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);

            126 

            127         /* 3、創(chuàng)建類      */

            128         leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);

            129         if (IS_ERR(leddev.class)) {

            130                 return PTR_ERR(leddev.class);

            131         }

            132 

            133         /* 4、創(chuàng)建設備 */

            134         leddev.device = device_create(leddev.class, NULL, leddev.devid, 

            NULL, LEDDEV_NAME);

            135         if (IS_ERR(leddev.device)) {

            136                 return PTR_ERR(leddev.device);

            137         }

            138 

            139         /* 5、初始化IO */

            140         leddev.node = of_find_node_by_path("/gpioled");

            141         if (leddev.node == NULL){

            142                 printk("gpioled node nost find!\r\n");

            143                 return -EINVAL;

            144         }

            145 

            146         leddev.led0 = of_get_named_gpio(leddev.node, "led-gpio", 0);

            147         if (leddev.led0 < 0) {

            148                 printk("can't get led-gpio\r\n");

            149                 return -EINVAL;

            150         }

            151 

            152         gpio_request(leddev.led0, "led0");

            153     gpio_direction_output(leddev.led0, 1); /* led0 IO設置為輸出,默認高電平 */

            154         return 0;

            155 }

            156 

            157 /*

            158  * @description : platform驅動的remove函數,移除platform

            驅動的時候此函數會執(zhí)行

            159  * @param - dev         : platform設備

            160  * @return         : 0,成功;其他負值,失敗

            161  */

            162 static int led_remove(struct platform_device *dev)

            163 {

            164         gpio_set_value(leddev.led0, 1);         /* 卸載驅動的時候關閉LED */

            165 

            166         cdev_del(&leddev.cdev);                         /*  刪除cdev */

            167         unregister_chrdev_region(leddev.devid, LEDDEV_CNT); /* 注銷設備號 */

            168         device_destroy(leddev.class, leddev.devid);

            169         class_destroy(leddev.class);

            170         return 0;

            171 }

            172 

            173 /* 匹配列表 */

            174 static const struct of_device_id led_of_match[] = {

            175         { .compatible = "gpioled" },

            176         { /* Sentinel */ }

            177 };

            178 

            179 /* platform驅動結構體 */

            180 static struct platform_driver led_driver = {

            181         .driver         = {

            182                 .name   = "imx6ul-led",     /* 驅動名字,用于和設備匹配 */

            183                 .of_match_table = led_of_match, /* 設備樹匹配表 */

            184         },

            185         .probe          = led_probe,

            186         .remove         = led_remove,

            187 };

            188 

            189 /*

            190  * @description : 驅動模塊加載函數

            191  * @param               : 無

            192  * @return              : 無

            193  */

            194 static int __init leddriver_init(void)

            195 {

            196         return platform_driver_register(&led_driver);

            197 }

            198 

            199 /*

            200  * @description : 驅動模塊卸載函數

            201  * @param               : 無

            202  * @return              : 無

            203  */

            204 static void __exit leddriver_exit(void)

            205 {

            206         platform_driver_unregister(&led_driver);

            207 }

            208 

            209 module_init(leddriver_init);

            210 module_exit(leddriver_exit);

            211 MODULE_LICENSE("GPL");

            212 MODULE_AUTHOR("topeet");


            _ueditor_page_break_tag_

            第 174~177 行,匹配表,描述了此驅動都和什么樣的設備匹配,第 175 行添加了一條值為"gpioled"的 compatible 屬性值,當設備樹中某個設備節(jié)點的 compatible 屬性值也為 “gpioled”的時候就會與此驅動匹配。

            第 180~187 行,platform_driver 驅動結構體,182 行設置這個 platform 驅動的名字為“imx6ul-led”,因此,當驅動加載成功以后就會在/sys/bus/platform/drivers/目錄下存在一個名為“imx6u-led”的文件。 第 183 行設置 of_match_table 為上面的 led_of_match。

            3 應用測試程序

            應用測試程序直接使用上一章44.3.2的led_test.c即可。


            *博客內容為網友個人發(fā)布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



            關鍵詞:

            相關推薦

            技術專區(qū)

            關閉