// 在Linux下的中断方式读取按键驱动程序
//包含外部中断 休眠 加入poll机制
// 采用异步通知的方式
// 驱动程序发 ---> app接收 (通过kill_fasync()发送)
// 为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:
// 1. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID。
// 不过此项工作已由内核完成,设备驱动无须处理。
// 2. 支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数将得以执行。
// 驱动中应该实现fasync()函数。
// 3. 在设备资源可获得时,调用kill_fasync()函数激发相应的信号
// 应用程序:
// fcntl(fd, F_SETOWN, getpid()); // 告诉内核,发给谁
// Oflags = fcntl(fd, F_GETFL);
// fcntl(fd, F_SETFL, Oflags | FASYNC); // 改变fasync标记,最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct
// 外部中断测试程序 包含poll机制 进程之间异步通信 加入原子操作
// 原子操作:指的是在执行过程中不会被别的代码路径所中断的操作。
// 信号量的实现
// 阻塞 :是指在执行设备操作时若不能获得资源则挂起进程,直到满足可操作的条件后再进行操作。
// 被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。
// 非阻塞:进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可以进行操作为止。
// 加入定时器消抖动功能
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define usingatomic (0) // 0使用信号量 1使用的是原子操作
//设备类
static struct class *Eint_class;
// 设备节点
static struct class_device *Eint_class_devs;
// 地址映射
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
// 全局变量 存放中断读出的键值
static unsigned int key_val;
//创建一个休眠队列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;
//信号量初始化结构体
static struct fasync_struct *button_async_queue;
// 定时器结构体
struct timer_list buttons_timer;
// 存储外部中断号和键值结构体变量
static struct pin_desc *irq_pd;
//定义结构体 存放按键 pin 端口 key_val键值
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
#if usingatomic
//定义原子变量v并初始化为1
atomic_t canopen = ATOMIC_INIT(1);
#else
//定义互斥锁 信号量
static DECLARE_MUTEX(button_lock);
#endif
//定义结构体数组 存放中断端口和键值
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 *ignored)
{
irq_pd = ( struct pin_desc *)ignored;
mod_timer(&buttons_timer, jiffies+HZ/100); //10ms 产生中断
// return IRQ_RETVAL(IRQ_HANDLED);
return IRQ_HANDLED;
}
//定时器中断函数
static void buttons_timer_function(unsigned long data)
{
struct pin_desc *pins_desc= irq_pd;
unsigned int pinval;
if(!pins_desc)
return;
pinval=s3c2410_gpio_getpin(pins_desc->pin); //读取IO的值
if(pinval)
{
key_val =0x80|pins_desc->key_val;
}
else
{
key_val =pins_desc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync (&button_async_queue, SIGIO, POLL_IN);//发送信号给app
}
//打开设备调用
//初始化IO端口 配置为输入模式
//GPF0-->S2 GPF2-->S3 GPG3-->S4 GPG11-->S5
static int Eint_drv_open(struct inode *inode, struct file *file)
{
// *gpfcon &=~((3<<2*0)|(3<<2*2));
// *gpgcon &=~((3<<3*2)|(3<<11*2));
#if usingatomic
if(!atomic_dec_and_test(&canopen))// 原子操作
{
atomic_inc(&canopen);//自加1
printk("this a user in the use of\n");
return -EBUSY;//返回忙
}
#else
if (file->f_flags & O_NONBLOCK)
{
//非阻塞 立马返回
if (down_trylock(&button_lock))
return -EBUSY;
}
else
{
down(&button_lock);
}
#endif
printk("Eint_drv_open successed!\n");
request_irq(IRQ_EINT0,buttons_irq, IRQT_BOTHEDGE, "s2", &pins_desc[0]);//EINT0边沿触发方式
request_irq(IRQ_EINT2,buttons_irq, IRQT_BOTHEDGE, "s3", &pins_desc[1]);//EINT2边沿触发方式
request_irq(IRQ_EINT11,buttons_irq, IRQT_BOTHEDGE, "s4", &pins_desc[2]);//EINT11边沿触发方式
request_irq(IRQ_EINT19,buttons_irq, IRQT_BOTHEDGE, "s5", &pins_desc[3]);//EINT19边沿触发方式
return 0;
}