4---linux中断之按键查询

上一篇中我们用了简单的字符设备驱动来点亮led灯,这一篇我们来搞一搞中断。

为了引入中断,我们先看一看这么一个程序

******led.c******

#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;
static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

static int second_drv_open(struct inode *inode,struct file *file)
{
	*gpfcon &= ~((0x3<<(0*2)) | (0x3 << (2*2)));
	*gpgcon &= ~((0x3<<(3*2)) | (0x3 << (11*2)));
    
     return 0;
}

ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	/* 返回4个引脚的电平 */
	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 second_drv_fops={
        .owner  = THIS_MODULE,
        .open   = second_drv_open,
        .read   = second_drv_read,
};



static int second_drv_init(void)
{
	major = register_chrdev(0,"second_drv",&second_drv_fops);
	seconddrv_class = class_create(THIS_MODULE, "seconddrv");
	seconddrv_class_dev = class_device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/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 second_drv_exit(void)
{

	unregister_chrdev(major,"second_drv");
	class_device_unregister(seconddrv_class_dev);
    class_destroy(seconddrv_class);

	iounmap(gpfcon);
	iounmap(gpgcon);
}
module_init(second_drv_init);
module_exit(second_drv_exit);
MODULE_LICENSE("GPL");


测试程序:

*****led_test*****

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc,char **argv)
{
        int cnt = 0;
        int fd;
        unsigned char key_vals[4];

        fd = open("/dev/buttons", O_RDWR);
        if(fd  < 0)
        {
                printf("can't 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("%04d key pressed : %d %d %d %d\n",cnt++,key_vals[0],key_vals[1],key_vals[2],key_vals[3]);
                }
        }
        return 0;

}

驱动程序提供了read函数,在read里面我们读出了key的电平
测试程序在一个死循环里面查看电平状态

我们把它们拷贝到开发板并运行:

./led_test &

在使用ps命令查看进程:

ps

会发现led_test cpu的占有率竟然是99,这个可是非常的霸道了,让其它进程怎么活

因此我们可以使用中断

查看原理图:中断号为0 2 11 19

1.注册中断
request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE, “S1”, &pins_desc[0]);  
request_irq(IRQ_EINT2, buttons_irq,IRQT_ BOTHEDGE, “S2”, &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq,IRQT_ BOTHEDGE, “S3”, &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq,IRQT_ BOTHEDGE, “S4”, &pins_desc[3]);

在入口函数注册中断
int request_irq(unsigned int irq, irq_handler_t handler,unsigned long irqflags, const char *devname, void *dev_id)
irq:中断号,一般在内核有宏定义了
handler :中断处理函数
irqgflags:触发模式,一般内核里也有宏定义,
name : 中断名字
dev_id : 中断的id

2.read函数
static DECLARE_WAIT_QUEUE_HEAD(button_wait);//声明等待队列
static int led_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{       
         wait_event_interruptible(button_wait, even_press);            

        /*有按键按下,退出等待队列,上传key_val 给用户层*/
         if(copy_to_user(buf,&key_val,sizeof(key_val)))
         return EFAULT;

     even_press=0; //数据发完后,立马设为休眠状态,避免误操作
         return 0;
}

当我们在应用程序调用open时,如果按键没有变化,那么会进入休眠状态,等待被唤醒
wait_event_interruptible(wq, condition) //将中断进入等待队列(休眠状态)
wq:等待队列,需要声明
condition:当为0时,进入休眠状态

3.中断服务函数
static irqreturn_t  buttons_irq (int irq, void *dev_id)       //中断服务函数
{
      struct pin_desc *pindesc=(struct pin_desc *)dev_id;     //获取引脚描述结构体
      unsigned int  pin_val=0;                                               
      pin_val=s3c2410_gpio_getpin(pindesc->pin);

        if(pin_val)
        {
                   /*没有按下 (下降沿),清除0x80*/                 
                   key_val=pindesc->pin_status&0xef;
         }
         else
         {
                   /*按下(上升沿),加上0x80*/
                   key_val=pindesc->pin_status|0x80;
         }             

            even_press=1;               //退出等待队列
            wake_up_interruptible(&button_wait);
                          
         return IRQ_HANDLED;
}

中断服务函数里面,按键按下时我们改变key_val的值,按键松开时我们改变key_val的值
wake_up_interruptible(&button_wait); //唤醒 中断

编写测试程序
#include <sys/types.h>    //调用sys目录下types.h文件
#include <sys/stat.h>      //stat.h获取文件属性
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
/*secondtext            while一直获取按键信息   */
int main(int argc,char **argv)
{
 int fd,ret;
 unsigned int val=0;               
 fd=open("/dev/buttons",O_RDWR);       
 if(fd<0)
     {printf("can't open!!!\n");
     return -1;}

 while(1)
 {
     ret=read(fd,&val,1);    //读取一个值,(当在等待队列时,本进程就会进入休眠状态)
     if(ret<0)
     {
     printf("read err!\n");    
     continue;
     } 
  printf("key_val=0X%x\r\n",val);
}
 return 0;
}

在死循环里面一直read()

扫描二维码关注公众号,回复: 4804105 查看本文章
编译,拷贝,加载
insmod key.c

执行测试程序:

./key_test &

使用ps命令:

ps

这次我们发现测试程序对cpu占有非常的低。按下按键,打印信息,松开按键,打印信息。

完成!!

*****key.c*****


#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>


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 DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,key_drv_read将它清0 */
static volatile int ev_press = 0;


struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};


/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;

struct pin_desc pins_desc[4] = {
	{S3C2410_GPF0, 0x01},
	{S3C2410_GPF2, 0x02},
	{S3C2410_GPG3, 0x03},
	{S3C2410_GPG11, 0x04},
};


/*
  * 确定按键值
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
	struct pin_desc * pindesc = (struct pin_desc *)dev_id;
	unsigned int pinval;
	
	pinval = s3c2410_gpio_getpin(pindesc->pin);

	if (pinval)
	{
		/* 松开 */
		key_val = 0x80 | pindesc->key_val;
	}
	else
	{
		/* 按下 */
		key_val = pindesc->key_val;
	}

    ev_press = 1;                  /* 表示中断发生了 */
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */

	
	return IRQ_RETVAL(IRQ_HANDLED);
}

static int key_drv_open(struct inode *inode, struct file *file)
{
	/* 配置GPF0,2为输入引脚 */
	/* 配置GPG3,11为输入引脚 */
	request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
	request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
	request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
	request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);	

	return 0;
}

ssize_t key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	if (size != 1)
		return -EINVAL;

	/* 如果没有按键动作, 休眠 */
	wait_event_interruptible(button_waitq, ev_press);

	/* 如果有按键动作, 返回键值 */
	copy_to_user(buf, &key_val, 1);
	ev_press = 0;
	
	return 1;
}


int key_drv_close(struct inode *inode, struct file *file)
{
	free_irq(IRQ_EINT0, &pins_desc[0]);
	free_irq(IRQ_EINT2, &pins_desc[1]);
	free_irq(IRQ_EINT11, &pins_desc[2]);
	free_irq(IRQ_EINT19, &pins_desc[3]);
	return 0;
}


static struct file_operations sencod_drv_fops = {
    .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open    =  key_drv_open,     
	.read	 =	key_drv_read,	   
	.release =  key_drv_close,	   
};


int major;
static int key_drv_init(void)
{
	major = register_chrdev(0, "key_drv", &sencod_drv_fops);

	keydrv_class = class_create(THIS_MODULE, "key_drv");

	keydrv_class_dev = class_device_create(keydrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/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");

猜你喜欢

转载自blog.csdn.net/cnqiuge123/article/details/85868345
今日推荐