一.寻找s3c2410的request_irq(irq,handler, irqflags, *devname, *dev_id)的参数
由于request_irq自动帮我们设置为中断引脚,所以我们可以在我们之前轮询模式的按键驱动代码的open函数修改,把虚拟地址的操作改成request_irq
1.确定参数irq
2.参数handler是我们自己写的中断处理函数,先写名称
3..确定参数irqflags
在s3c24xx_init_irq里有set_irq_chip(irqno, &s3c_irqext_chip);等例子,点进去,找到s3c_irqext_type,
这里我们用双边沿触发IRQT_BOTHEDGE
4.参数*devname, *dev_id随便写
二.写free_irq释放中断服务程序
要在file_operations加.release
三.编写中断处理函数(1)
(1)先只测试中断是否编写成功
拿去开发板测试
拿去开发板测试
执行insmod mybuttons.ko
按道理执行exec 5</dev/mybuttons (打开命令) 就会执行open语句,用cat /proc/interrupts查看中断
已经有s1,s2,s3,s4四个中断设备,关闭命令用 exec 5<&-,当前进程是-sh,可以用命令ls -l /proc/771/fd参考文件描述符重定位。用具体可以参考https://blog.csdn.net/qq_31186123/article/details/82190776
三.编写中断处理函数(2)
(1)思考:
request_irq帮我们把引脚设置为中断引脚,一有双边沿触发(按键按下或者松开)就会去执行中断处理函数mybuttons_irq,并把最后一个参数传给它。有四个按键,后面要读取引脚值可以用内核提供的读取引脚值函数unsigned int s3c2410_gpio_getpin(unsigned int pin),参数pin是引脚名称,为此还要设置返回值pin_val =s3c2410_gpio_getpin(unsigned int pin)。四个按键按下我们可以用0x01 ,0x02,0x03,0x04打印输出,因此我们要设置每个按键有2个变量。可以这样设置:
这样以后按键按下,就会把结构体变量pins_desc[]传进mybuttons_irq中断处理函数
(2)完善mybuttons_irq中断处理函数
可以定义一个新结构体指针指向变量dev_id,使用pin_val =s3c2410_gpio_getpin(pindesc->pin)获取引脚值,判断。
由于中断不可能一直执行,为了节省cpu,可以用睡眠唤醒机制,在读函数中:没有按下就睡眠,有按下,就唤醒——>把键值传到应用层打印
四.测试
ps后看到./mybuttons_test为睡眠模式S,按下后输出键值,表明中断成功
五.代码
驱动代码:
#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>
#include <linux/irq.h>
#include <linux/poll.h>
static struct class *seconddrv_buttons_class;
static struct class_device *seconddrv_buttons_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,third_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 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
static unsigned char key_val;
static irqreturn_t mybuttons_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) // 1为松开
{
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 seconddrv_buttons_open(struct inode *inode, struct file *file)
{
/*配置GPF0,2为输入引脚,中断号参考arch\arm\plat-s3c24xx\Irq.c的s3c24xx_init_irq*/
/*配置GPG3,11为输入引脚*/
request_irq(IRQ_EINT0, mybuttons_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);
request_irq(IRQ_EINT2, mybuttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[1]);
request_irq(IRQ_EINT11, mybuttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[2]);
request_irq(IRQ_EINT19, mybuttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[3]);
//printk("seconddrv_open\n");
return 0;
}
ssize_t seconddrv_buttons_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;
}
static int seconddrv_buttons_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 seconddrv_buttons_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = seconddrv_buttons_open,
.read = seconddrv_buttons_read,
.release= seconddrv_buttons_close,
};
int major;
int seconddrv_buttons_init(void)
{
major = register_chrdev(0,"seconddrv",&seconddrv_buttons_fops);
seconddrv_buttons_class = class_create(THIS_MODULE,"buttons");
seconddrv_buttons_class_dev = class_device_create(seconddrv_buttons_class,NULL,MKDEV(major,0),NULL,"mybuttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon +1;
return 0;
}
void seconddrv_buttons_exit(void)
{
unregister_chrdev(major,"seconddrv");
class_destroy(seconddrv_buttons_class);
class_device_unregister(seconddrv_buttons_class_dev);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(seconddrv_buttons_init);
module_exit(seconddrv_buttons_exit);
MODULE_LICENSE("GPL");
测试代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
/*
*mybuttons_test
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
fd = open("/dev/mybuttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
while (1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
//sleep(1);
}
return 0;
}