色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(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"linux/irq.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 irqreturn_t buttons_irq(int irq, void *dev_id)
          {
          printk("irq = %dn",irq);
          return IRQ_RETVAL(IRQ_HANDLED);
          }
          static int key_drv_open(struct inode *inode, struct file *file)
          {
          request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S2",1);
          request_irq(IRQ_EINT2,buttons_irq,IRQT_BOTHEDGE,"S3",1);
          request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"S4",1);
          request_irq(IRQ_EINT19,buttons_irq,IRQT_BOTHEDGE,"S5",1);
          return 0;
          }
          ssize_t key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
          {
          unsigned char key_vals[4];
          int regval;
          if(size != sizeof(key_vals))
          return -EINVAL;
          regval = *gpfdat;
          key_vals[0] = (regval & (1<<0)) ? 1 : 0;
          key_vals[1] = (regval & (1<<2)) ? 1 : 0;
          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);
          }
          int key_drv_close(struct inode *inode, struct file *file)
          {
          free_irq(IRQ_EINT0, 1);
          free_irq(IRQ_EINT2, 1);
          free_irq(IRQ_EINT11, 1);
          free_irq(IRQ_EINT19, 1);
          return 0;
          }
          static struct file_operations key_drv_fops = {
          .owner = THIS_MODULE,
          .open = key_drv_open,
          .read = key_drv_read,
          .release = key_drv_close,
          };
          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");
          =================================================================================================
          測試命令:
          lsmod :檢測當(dāng)前已經(jīng)安裝的驅(qū)動(dòng)程序
          insmod ./keydrv.ko :安裝keydrv.ko驅(qū)動(dòng)程序
          cat /proc/devices :查看當(dāng)前設(shè)備
          cat /proc/interrupts :查看當(dāng)前中斷程序
          exec 5<|dev/buttons :打開設(shè)備(等同于應(yīng)用程序調(diào)用open函數(shù)),定位到5
          此時(shí)再執(zhí)行:cat /proc/interrupts 命令就可以看到S2、S3、S4、S5四個(gè)中斷程序
          ps :查看所有進(jìn)程(770 0 3096 S -sh)
          top :查看各進(jìn)程所占CPU比例
          ls -l /proc/770/fd :可以查看到定位到5上面(lr-x-- 1 0 0 64 Jan1 00:22 5 -> /dev/buttons)
          exec 5<&- :關(guān)閉設(shè)備(等同于應(yīng)用程序調(diào)用close函數(shù))



          評論


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

          關(guān)閉