在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,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首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 字符設(shè)備驅(qū)動(dòng)-按鍵驅(qū)動(dòng)

            字符設(shè)備驅(qū)動(dòng)-按鍵驅(qū)動(dòng)

            作者: 時(shí)間:2016-11-21 來源:網(wǎng)絡(luò) 收藏
            驅(qū)動(dòng)源碼:
            #include "linux/module.h"
            #include"linux/kernel.h"
            #include"linux/fs.h"
            #include"linux/init.h"
            #include"linux/delay.h"
            #include"asm/uaccess.h"
            #include"asm/irq.h"
            #include"asm/io.h"
            #include"asm/arch/regs-gpio.h"
            #include"asm/hardware.h"
            int major = 0;
            static struct class *keydrv_class;
            static struct class_device *keydrv_class_dev;
            volatile unsigned long *gpfcon;
            volatile unsigned long *gpfdat;
            volatile unsigned long *gpgcon;
            volatile unsigned long *gpgdat;
            static int key_drv_open(struct inode *inode, struct file *file)
            {
            // 配置GPF0,2為輸入引腳
            *gpfcon &= ~((0x3<<(0*2)) | (0x3<<(2*2)));
            // 配置GPG3,11為輸入引腳
            *gpgcon &= ~((0x3<<(3*2)) | (0x3<<(11*2)));
            return 0;
            }
            ssize_t key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
            {
            // 返回4個(gè)引腳的電平
            unsigned char key_vals[4];
            int regval;
            if(size != sizeof(key_vals))
            return -EINVAL;
            // 讀GPF0,2
            regval = *gpfdat;
            key_vals[0] = (regval & (1<<0)) ? 1 : 0;
            key_vals[1] = (regval & (1<<2)) ? 1 : 0;
            // 讀GPG3,11
            regval = *gpgdat;
            key_vals[2] = (regval & (1<<3)) ? 1 : 0;
            key_vals[3] = (regval & (1<<11)) ? 1 : 0;
            copy_to_user(buf, key_vals, sizeof(key_vals));
            return sizeof(key_vals);
            }
            static struct file_operations key_drv_fops = {
            .owner = THIS_MODULE, // 這是一個(gè)宏,推向編譯模塊時(shí)自動(dòng)創(chuàng)建的__this_module變量
            .open = key_drv_open,
            .read = key_drv_read,
            };
            static int key_drv_init(void)
            {
            major = register_chrdev(0, "key_drv", &key_drv_fops);
            keydrv_class = class_create(THIS_MODULE, "key_drv");
            keydrv_class_dev = class_device_create(keydrv_class, NULL, MKDEV(major, 0), NULL, "buttons");
            gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
            gpfdat = gpfcon + 1;
            gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
            gpgdat = gpgcon + 1;
            return 0;
            }
            static void key_drv_exit(void)
            {
            unregister_chrdev(major, "key_drv");
            class_device_unregister(keydrv_class_dev);
            class_destroy(keydrv_class);
            iounmap(gpfcon);
            iounmap(gpgcon);
            return 0;
            }
            module_init(key_drv_init);
            module_exit(key_drv_exit);
            MODULE_LICENSE("GPL");
            =================================================================================================
            測(cè)試程序:
            #include "sys/types.h"
            #include "sys/stat.h"
            #include "fcntl.h"
            #include "stdio.h"
            int main(int argc, char **argv)
            {
            int fd;
            unsigned char key_vals[4];
            fd = open("/dev/buttons", O_RDWR);
            if(fd < 0)
            printf("cant open!n");
            while(1)
            {
            read(fd, key_vals, sizeof(key_vals));
            if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3])
            {
            printf("key pressed: %d %d %d %dn", key_vals[0], key_vals[1], key_vals[2], key_vals[3]);
            }
            }
            return 0;
            }



            評(píng)論


            技術(shù)專區(qū)

            關(guān)閉