基于S3C2440,Linux版本2.6.22.6的按键驱动程序编写
1.头文件包含
#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>
分析:由于使用中断方式触发按键,故需要增添头文件#include <linux/irq.h>。
2.具体函数
1)请求中断
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值:按下时,0x01,0x02,0x03,0x04
* 键值:松开时,0x81,0x82,0x83,0x84
*/
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},
{S3C2410_GPG3,0x03},
{S3C2410_GPG11,0x04},
};
static int key_int_drv_open(struct inode *inode,struct file *file)
{
request_irq(IRQ_EINT0, key_int_irq,IRQT_BOTHEDGE,"S2",&pins_desc[0]);
request_irq(IRQ_EINT2, key_int_irq,IRQT_BOTHEDGE,"S3",&pins_desc[1]);
request_irq(IRQ_EINT11,key_int_irq,IRQT_BOTHEDGE,"S4",&pins_desc[2]);
request_irq(IRQ_EINT19,key_int_irq,IRQT_BOTHEDGE,"S5",&pins_desc[3]);
return 0;
}
实际上此函数的原型为linux2.6.22.6/include/linux/fs.h文件下的int (*open) (struct inode *, struct file *);函数。
通过request_irq();函数请求中断。request_irq();函数出自linux-2.6.22.6\kernel\irq\manage.c
原型为int request_irq(unsigned int irq, irq_handler_t handler,unsigned long irqflags, const char *devname, void *dev_id)
- 第一个参数unsigned int irq为中断号,需要根据按键对应的硬件中断号确认。
如下开发板原理图所示,对应管脚硬件中断号分别为EINT0,EINT2,EINT11,EINT19。
- 第二个参数 irq_handler_t handler,填写的key_int_irq为请求中断的处理函数。
- 第三个参数unsigned long irqflags,为触发方式。IRQT_BOTHEDGE表示双边沿触发。还可以定义为其他方式,见linux-2.6.22.6\include\asm-arm\irq.h。
IRQT_NOEDGE
IRQT_RISING//上升沿触发
IRQT_FALLING //下降沿触发
IRQT_BOTHEDGE//双边沿触发
IRQT_LOW//低电平触发
IRQT_HIGH//高电平触发
IRQT_PROBE
- 第四个参数const char *devname为请求的中断名称,可以随便取。为字符串的形式。
- 第五个参数void *dev_id为设备ID,主要配合卸载中断时使用。可以随便取。
2)中断初始化函数
static volatile int ev_press = 0;
static unsigned char key_val;
static irqreturn_t key_int_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;
}
//printk("Kye_val is :%x",key_val);
ev_press = 1; //表示中断发生
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_RETVAL(IRQ_HANDLED);
}
这个函数为上面request_irq();函数里请求的irq初始化函数。传入的参数
- 第一个参数int irq为中断号
- 第二个参数void *dev_id为设备id。就是request_irq();函数第五个参数传进来的。所以可以自己定义用法。
这里是定义了一个结构体,传进来按键对应的管脚以及给4个按键各定义了编号0x01~0x04。
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值:按下时,0x01,0x02,0x03,0x04
* 键值:松开时,0x81,0x82,0x83,0x84
*/
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},
{S3C2410_GPG3,0x03},
{S3C2410_GPG11,0x04},
};
- struct pin_desc * pindesc = (struct pin_desc *)dev_id;
为将请求的中断,中断引脚以及编号按结构体pin_desc的格式存入结构体pindesc中。
- pinval = s3c2410_gpio_getpin(pindesc->pin);
s3c2410_gpio_getpin();参数里填入PIN脚,返回值为PIN脚对应的状态。
返回值为0:对应pin脚为0(低电平)。
返回值为1:对应pin脚为1(高电平)。
if(pinval) //松开
{
key_val = 0x80 | pindesc->key_val;
}
else //按下
{
key_val = pindesc->key_val;
}
按键处理,从上面原理图可以看出。按下为低电平,松开为高电平。
故两种状态做个处理,并将值保存到全局变量key_val中。
- ev_press = 1; //表示中断发生
定义的一个变量,为1表示中断发生了,唤醒进程。为0表示中断结束,使进程休眠。
- wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
使用wake_up_interruptible();。需在程序开头定义进程。即static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//定义初始化等待队列头
static volatile int ev_press = 0;
3)读函数
ssize_t key_int_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;
}
函数原型为linux2.6.22.6/include/linux/fs.h中的ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
用来从设备中获取数据. 在这个位置的一个空指针导致 read 系统调用以 -EINVAL("Invalid argument") 失败. 一个非负返回值代表了成功读取的字节数( 返回值是一个 "signed size" 类型, 常常是目标平台本地的整数类型)。
这个函数中的第一个参数是文件的描述符,第二个参数是用户空间中的缓存的指针,第三个参数是请求传输的数据的大小。
buff参数是一个用户空间的指针,不能够直接让内核代码使用。
- 判断请求的数据大小
if(size != 1)
return -EINVAL;
准备在测试函数中,请求驱动传递1个字节的键值,故size大小为1。如果不为1则返回失败。
- 进入休眠,等待状态
wait_event_interruptible(button_waitq,ev_press);
第一个参数:为定义的进程
第二个参数:状态。为0则进程继续休眠,为1则执行进程。
- 发生中断,进入中断处理
copy_to_user(buf,&key_val,1);//将数据返回用户
ev_press = 0; //表示中断结束
将处理好的键值通过copy_to_user函数返回给用户使用。用户在测试程序中就可以调用read();函数读取键值数据。
4)释放中断
int key_int_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;
}
- 释放请求的中断
如果需要卸载驱动,则需要先释放申请的中断。
free_irq();和request_irq();函数对应。有请求就有释放。
第一个参数为中断号,和request_irq();函数第一个参数相对应。
第二个参数为设备id,和request_irq();函数第五个参数相对应。
释放将对应中断清除,以及用户自定义的信息(设备id)所占用的空间清除。
另贴出全部驱动代码,供参考
#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 *key_int_drv_class;
static struct class_device *key_int_drv_class_dev;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static unsigned char key_val;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值:按下时,0x01,0x02,0x03,0x04
* 键值:松开时,0x81,0x82,0x83,0x84
*/
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},
{S3C2410_GPG3,0x03},
{S3C2410_GPG11,0x04},
};
static int key_int_drv_open(struct inode *inode,struct file *file);
ssize_t key_int_drv_read(struct file *file,char __user *buf,size_t size,loff_t *ppos);
static int key_int_drv_init(void);
static void key_int_drv_exit(void);
int key_int_int_drv_close(struct inode *inode, struct file *file);
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgcon = NULL;
volatile unsigned long *gpgdat = NULL;
static irqreturn_t key_int_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;
}
//printk("Kye_val is :%x",key_val);
ev_press = 1; //表示中断发生
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_RETVAL(IRQ_HANDLED);
}
int key_int_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 key_int_drv_fops = {
.owner = THIS_MODULE,
.open = key_int_drv_open,
.read = key_int_drv_read,
.release = key_int_drv_close, //关闭设备时用到的函数
};
static int key_int_drv_open(struct inode *inode,struct file *file)
{
request_irq(IRQ_EINT0, key_int_irq,IRQT_BOTHEDGE,"S2",&pins_desc[0]);
request_irq(IRQ_EINT2, key_int_irq,IRQT_BOTHEDGE,"S3",&pins_desc[1]);
request_irq(IRQ_EINT11,key_int_irq,IRQT_BOTHEDGE,"S4",&pins_desc[2]);
request_irq(IRQ_EINT19,key_int_irq,IRQT_BOTHEDGE,"S5",&pins_desc[3]);
return 0;
}
ssize_t key_int_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 major;
static int key_int_drv_init(void) //入口函数
{
major = register_chrdev(0,"key_int_drv",&key_int_drv_fops);//注册,告诉内核
key_int_drv_class = class_create(THIS_MODULE,"key_int_drv");
key_int_drv_class_dev = class_device_create(key_int_drv_class,NULL,MKDEV(major,0),NULL,"key"); //在dev/中创建文件key
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_int_drv_exit(void)
{
unregister_chrdev(major,"key_int_drv");//卸载驱动
class_device_unregister(key_int_drv_class_dev);
class_destroy(key_int_drv_class);
iounmap(gpfcon);
iounmap(gpgcon);
}
module_init(key_int_drv_init);
module_exit(key_int_drv_exit);
MODULE_LICENSE("GPL");