详细注释FL2440按键中断驱动(含poll机制)


详细注释FL2440按键中断驱动(含poll机制),测试成功。

平台:FL2440 

内核版本linux 2.6.28

效果:没有按键时,程序进入休眠,每5秒打印超时信息。按键时响应中断,并输出是按下还是松开。

#include <linux/module.h> /*模块有关的*/

#include <linux/kernel.h>/*内核有关的*/
#include <linux/fs.h>/*文件系统有关的*/
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>/*linux中断*/
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>  //copy_to_user
#include <mach/regs-gpio.h>/*寄存器设置*/S3C2410_GPF0等的定义
#include <mach/hardware.h>//s3c2410_gpio_getpin等的定义
#include <mach/irqs.h> //IRQ_EINT0等的定义

#include <asm/system.h> 

 


static struct class *thirddrv_class;加载模块时,自动创建文件设备

static struct class_device*thirddrv_class_dev;

 


volatile unsigned long *gpfcon;//IO口配置寄存器,FL2440为F口
volatile unsigned long *gpfdat;//IO口数据寄存器

static unsigned char keyval;判断所用键值,返回用户空间,1、2、3、4为按下,81、82、83、84为松开

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);向内核初始化机构体button_waitq,read休眠用
static volatile int ev_press=0;判断是否唤醒进程的标志参数

struct pin_desc{
unsigned int pin;
unsigned int key_val;
};引脚相关结构体

struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 0x04},
};引脚相关结构体,FL2440按键IO实例化

static irqreturn_t buttons_irq(int irq,void *dev_id)//按键中断服务函数
{


struct pin_desc *pindesc=(struct pin_desc*)dev_id;//引脚相关结构体参数传入,中断request_irq内核函数传入
unsigned int pinval;//按键引脚状态
pinval = s3c2410_gpio_getpin(pindesc->pin);获得按键引脚电平
if (pinval)//根据高低电平返回不同的键值
{
keyval=0x80|pindesc->key_val;
}
else
{
keyval=pindesc->key_val;
}
ev_press=1;进程唤醒参数
wake_up_interruptible(&button_waitq);唤醒睡眠的read进程


return IRQ_HANDLED;
}
static int third_drv_open(struct inode *inode, struct file *file)打开驱动设备文件
{
*gpfcon &= ~((0x2<<(0*2)) | (0x2<<(2*2))| (0x2<<(3*2))| (0x2<<(4*2)));设置按键引脚为中断状态
request_irq(IRQ_EINT0,buttons_irq,IRQF_TRIGGER_RISING,"s2",&pins_desc[0]);linux驱动中断设置函数,十分重要,设置中断号,中断引脚,中断服务程序,及中断触发方式,名称。需要查看按键原理图,0、2设置为上升沿触发,3、4设置为上升沿下降沿都触发。
request_irq(IRQ_EINT2,buttons_irq,IRQF_TRIGGER_RISING,"s3",&pins_desc[1]);
request_irq(IRQ_EINT3,buttons_irq,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"s4",&pins_desc[2]);
request_irq(IRQ_EINT4,buttons_irq,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"s5",&pins_desc[3]);
return 0;
}


ssize_t third_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);如果ev_press为0,则进程进入休眠状态
copy_to_user(buf, &keyval, 1);当中断发生时,ev_press=1,唤醒进程。将按键键值返回用户空间
ev_press=0;设置参数归0

return sizeof(keyval);
}


static unsigned int third_drv_poll(struct file *file, poll_table *wait)//引入poll机制,类似select
{
unsigned int mask=0;

poll_wait(file,&button_waitq,wait);

//将进程挂载到等待队列中,此处不休眠,根据mask返回值决定休眠否(在schedule——timeout中)

if(ev_press)
mask |= POLLIN|POLLRDNORM;中断发生,ev_press为1,设置mask为非零。唤醒休眠
return mask;
}


int third_drv_close(struct inode *inode, struct file *file)//关闭中断,释放中断引脚资源
{
free_irq(IRQ_EINT0,&pins_desc[0]);
free_irq(IRQ_EINT2,&pins_desc[0]);
free_irq(IRQ_EINT3,&pins_desc[0]);
free_irq(IRQ_EINT4,&pins_desc[0]);
return 0;
}


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


int major;
static int third_drv_init(void)//向内核注册模块
{
major = register_chrdev(0, "third_drv", &sencod_drv_fops);


thirddrv_class = class_create(THIS_MODULE, "third_drv");


thirddrv_class_dev = device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */创建设备


gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);//IO口引脚物理地址转为虚拟地址
gpfdat = gpfcon + 1;

 


return 0;
}


static void third_drv_exit(void)//从内核卸载模块
{
unregister_chrdev(major, "third_drv");
device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);
iounmap(gpfcon);
iounmap(gpgcon);


return 0;
}


module_init(third_drv_init);

module_exit(third_drv_exit);


MODULE_LICENSE("GPL");

  • 1
  • 2
  • 下一页

相关内容