字符设备驱动之按键驱动

前言

       这次的按键驱动会分为两部分来介绍。第一,用不停轮询的方式来监听按键事件;第二,以中断的方式来响应按键事件。可以对比看一下这两种方式有什么差别。

正文

轮询方式

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

static int major;
static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpgcon = NULL;

volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgdat = NULL;


static int second_drv_open(struct inode *inode, struct file *file)
{
	//GPF0,2配置为输入引脚
	*gpfcon &= ~( (0x3<<0) | (0x3<<2) );
	
	//GPG3,11配置为输入引脚
	*gpgcon &= ~( (0x3<<3) | (0x3<<11) );
	
	return 0;
}

static int second_drv_read(struct file *file, const __user *buf, size_t count, loff_t *ppos)
{
	unsigned int key_values[4];
	if (count < sizeof(key_values))
		return -EINVAL;

	key_values[0] = ( (*gpfdat)&(1<<0) ) ? 1 : 0;
	key_values[1] = ( (*gpfdat)&(1<<2) ) ? 1 : 0;

	key_values[2] = ( (*gpgdat)&(1<<3) ) ? 1 : 0;
	key_values[3] = ( (*gpgdat)&(1<<11) ) ? 1 : 0;

	copy_to_user(buf, key_values, sizeof(key_values));
	return sizeof(key_values);
}

static struct file_operations second_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_fops);
	seconddrv_class = class_create(THIS_MODULE, "second_drv");
	seconddrv_class_dev = class_device_create(seconddrv_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;
}

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");

1、字符设备书写的老套路:

(1)注册字符设备register_chrdev(),并且将second_fops联系起来。

(2)在/sys/class目录下创建相应类second_drv,class_create()

(3)然后再调用class_device_create()函数为每个设备在/dev目录下创建buttons节点

上面三步就算完成了字符设备的创建。

2、

以前写裸板按键程序的时候,我们都是直接操作相应的寄存器地址,但是在Linux系统下,我们只能操作虚拟地址,而且CPU不会为这些已知的外设IO内存资源预先指定虚拟地址的值,所以我们需要使用ioremap函数将这些物理地址映射到虚拟地址,然后才能通过映射后的虚拟地址操作相应的寄存器。

3、

创建完一个按键的字符设备后,我们再写一个测试程序,使用不停轮询的方式检测按键事件

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

int main(int argc, char **argv)
{
	int val[4];
	int fd;
	int cnt = 0;
	fd = open("/dev/buttons", O_RDWR);
	if (fd < 0)
	{
		printf("open fd failed\n");
		return 0;
	}
	while (1)
	{
		read(fd, val, sizeof(val));
		if (!val[0] || !val[1] || !val[2] || !val[3])
		{
			printf("%04d  key pressed: %d %d %d %d\n", cnt++, val[0], val[1], val[2], val[3]);
		}
	}
	
	return 1;
}

因为不停的轮询访问,所以占用的CPU资源会非常多,接近99%。所以实际生产中,这种方式肯定是不行的,下面我们使用中断的方式。

中断方式

#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 int major;
static struct class *thirddrv_class;
static struct class_device *thirddrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpgcon = NULL;

volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgdat = NULL;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/*中断事件标识,中断服务程序将它置为1,third_drv_read将它清零*/
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_GPG0, 0x01},	
	{S3C2410_GPG2, 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 third_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;
}

static int third_drv_read(struct file *file, const __user *buf, size_t count, loff_t *ppos)
{
	if (count != 1)
		return -EINVAL;

	/*如果没有按键动作,就休眠,
	  *当ev_press为0就进入休眠
	  */
	wait_event_interruptible(button_waitq, ev_press);

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

static int third_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]);
}

static struct file_operations third_fops = {
	.owner   = THIS_MODULE,
	.open    = third_drv_open,
	.read    = third_drv_read,
	.release = third_drv_close,
};

static int third_drv_init(void)
{
	major = register_chrdev(0, "third_drv", &third_fops);
	thirddrv_class = class_create(THIS_MODULE, "third_drv");
	thirddrv_class_dev = class_device_create(thirddrv_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;
}

void third_drv_exit(void)
{
	unregister_chrdev(major, "third_drv");
	class_device_unregister(thirddrv_class_dev);
	class_destroy(thirddrv_class);
	iounmap(gpfcon);
	iounmap(gpgcon);
}

module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");

1、

创建字符设备的步骤和轮询方式是一样的

2、

重点在于注册中断服务,在open函数中我们对四个按键的中断服务进行注册。代码中是使用request_irq()函数,函数原型是:

int request_irq(unsigned int irq, irq_handler_t handler,
        unsigned long irqflags, const char *devname, void *dev_id)

(1)irq是中断号,用来将我们的中断处理函数buttons_irq(),注册到相应的irq描述结构体irq_desc[irq]中,因为不同的中断是由不同的中断引脚产生的,我们必须用不同的中断号加以区分

(2)handler就是我们自己书写的中断处理函数,当中断发生的时候,就会回调传递进去的函数buttons_irq()

(3)irqflags是用来传递一些标志位,比如是否是共享中断(可能多个中断使用同一个中断引脚),我们的程序传递的标志位表示是双边沿触发

(4)devname可以cat /proc/interrupts看到注册了的中断服务

(5)dev_id用来区分共享中断中哪个才是我们想要调用的中断,也可以用来指定中断服务函数需要参考的数据地址。建议将设备结构指针作为dev_id参数,比如我们就是将每个中断引脚的描述结构体的首地址作为dev_id参数进行传递。

对于request_irq()函数的参数作用,可以参考这篇文章:request_irq() | 注册中断服务

      如果想了解整个中断处理的架构,可以参考我另外一篇文章:Linux异常中断处理结构。可以弄清楚注册中断处理函数的流程。

3、

       我们这个驱动程序的功能比较简陋,当有按键事件发生的时候,就把读取的简直打印出来,如果没有按键事件发生时,就进入等待状态,避免程序不停的轮询,占用过多的资源。third_drv_read()函数中调用的wait_event_interruptible()就是使测试程序在没有中断发生时进入等待状态。但是一般需要先定义一个队列:

DECLARE_WAIT_QUEUE_HEAD(button_waitq)

并定义一个全局变量标示当前中断服务是否正在执行

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

声明完队列之后,就在中断没有发生时,进入等待队列

wait_event_interruptible(button_waitq, ev_press);

当中断发生后,就重新将挂起的程序放入执行队列中

ev_press = 1;  //表示中断发生了
wake_up_interruptible(&button_waitq);

 

猜你喜欢

转载自blog.csdn.net/lee_jimmy/article/details/82924888