************************************************************************************************************************
#操作系统:CentOS-6.7
#linux内核:linux-3.0
#开发板 :fl440
#编译器 :arm-linux-gcc(buildroot-2012.08)
#cpu :s3c2440
#开发模块 :button
*************************************************************************************************************************
前面已经学习了两个led驱动,这次学习的这个按键驱动与前两个驱动不同的是多了中断处理,轮询机制等
贴上按键驱动代码:button_led.c
/********************************************************************************
* Copyright: (C) 2017 zoulei<zoulei121@gmail.com>
* All rights reserved.
*
* Filename: button_led.c
* Description: This file
*
* Version: 1.0.0(08/4/2017)
* Author: zoulei <zoulei121@gmail.com>
* ChangeLog: 1, Release initial version on "08/4/2017 11:03:40 AM"
*
********************************************************************************/
#include "s3c_driver.h"
#define DRV_AUTHOR "zoulei <zoulei@gmail.com>"
#define DRV_DESC "S3C24XX button driver"
/* Driver version*/
#define DRV_MAJOR_VER 1
#define DRV_MINOR_VER 0
#define DRV_REVER_VER 0
#define DEV_NAME DEV_BUTTON_NAME
//#define DEV_MAJOR DEV_BUTTON_MAJOR
#ifndef DEV_MAJOR
#define DEV_MAJOR 0 /* dynamic major by default */
#endif
#define BUTTON_UP 0 /* Button status is up */
#define BUTTON_DOWN 1 /* Button status is pushed down */
#define BUTTON_UNCERTAIN 2 /* Button status uncerntain */
#define TIMER_DELAY_DOWN (HZ/50) /*Remove button push down dithering timer delay 20ms */
#define TIMER_DELAY_UP (HZ/10) /*Remove button up dithering timer delay 100ms */
static int debug = DISABLE;
static int dev_major = DEV_MAJOR;
static int dev_minor = 0;
/* Button hardware informtation structure*/
struct s3c_button_info
{
unsigned char num; /*Button nubmer 按键号*/
char * name; /*Button nubmer 按键名*/
int nIRQ; /*Button IRQ number 中断号*/
unsigned int setting; /*Button IRQ Pin Setting 中断引脚配置*/
unsigned int gpio; /*Button GPIO port 对应的IO引脚*/
};
/* The button plaotform device private data structure */
struct s3c_button_platform_data //按键数据结构体
{
struct s3c_button_info *buttons; //用来访问按键硬件信息的指针
int nbuttons;//按键数量
};
/* Button hardware informtation data*/ //具体的相应按键信息
static struct s3c_button_info s3c_buttons[] = {
[0] = {
.num = 1,
.name = "KEY1",
.nIRQ = IRQ_EINT0,//中断号
.gpio = S3C2410_GPF(0),
.setting = S3C2410_GPF0_EINT0,//datasheet手册上对应的IO中断口
},
[1] = {
.num = 2,
.name = "KEY2",
.nIRQ = IRQ_EINT2,
.gpio = S3C2410_GPF(2),
.setting = S3C2410_GPF2_EINT2,
},
[2] = {
.num = 3,
.name = "KEY3",
.nIRQ = IRQ_EINT3,
.gpio = S3C2410_GPF(3),
.setting = S3C2410_GPF3_EINT3,
},
[3] = {
.num = 4,
.name = "KEY4",
.nIRQ = IRQ_EINT4,
.gpio = S3C2410_GPF(4),
.setting = S3C2410_GPF4_EINT4,
},
};
/* The button platform device private data */
static struct s3c_button_platform_data s3c_button_data = {
.buttons = s3c_buttons,
.nbuttons = ARRAY_SIZE(s3c_buttons),
};
struct button_device
{
unsigned char *status; /* The buttons Push down or up status */
struct s3c_button_platform_data *data; /* The buttons hardware information data */
struct timer_list *timers; /* The buttons remove dithering timers */
wait_queue_head_t waitq; /* Wait queue for poll() */
volatile int ev_press; /* Button pressed event */
struct cdev cdev;
struct class *dev_class;
} button_device;
static void platform_button_release(struct device * dev)
{
return;
}
static struct platform_device s3c_button_device = {
.name = "s3c_button",
.id = 1,
.dev =
{
.platform_data = &s3c_button_data,
.release = platform_button_release,
},
};
static irqreturn_t s3c_button_intterupt(int irq,void *de_id) //按键中断服务程序
{
int i;
int found = 0;
struct s3c_button_platform_data *pdata = button_device.data;
for(i=0; i<pdata->nbuttons; i++)
{
if(irq == pdata->buttons[i].nIRQ)//找到具体的中断号
{
found = 1;
break;
}
}
if(!found) /* An ERROR interrupt */
return IRQ_NONE;
/* Only when button is up then we will handle this event */
if(BUTTON_UP == button_device.status[i])
{
button_device.status[i] = BUTTON_UNCERTAIN;//因为要防抖动,且不确定是否为有效按键,所以先设置为不确定状态
mod_timer(&(button_device.timers[i]), jiffies+TIMER_DELAY_DOWN);
}
return IRQ_HANDLED;
}
static void button_timer_handler(unsigned long data) //定时器中断服务程序
{
struct s3c_button_platform_data *pdata = button_device.data;
int num =(int)data;
int status = s3c2410_gpio_getpin( pdata->buttons[num].gpio );
if(LOWLEVEL == status)
{
if(BUTTON_UNCERTAIN == button_device.status[num]) /* Come from interrupt */
{
button_device.status[num] = BUTTON_DOWN;
printk("%s pressed.\n", pdata->buttons[num].name); //~!@#)¥%……&^.,(
/* Wake up the wait queue for read()/poll() */
button_device.ev_press = 1;
wake_up_interruptible(&(button_device.waitq)); //ev_press唤醒等待队列
}
/* Cancel the dithering */
mod_timer(&(button_device.timers[num]), jiffies+TIMER_DELAY_UP);//重新激活并设置定时器
}
else
{
button_device.status[num] = BUTTON_UP;
}
return ;
}
/*===================== Button device driver part ===========================*/
static int button_open(struct inode *inode, struct file *file)
{
struct button_device *pdev ;
struct s3c_button_platform_data *pdata;
int i, result;
pdev = container_of(inode->i_cdev,struct button_device, cdev);
pdata = pdev->data;
file->private_data = pdev;
/* Malloc for all the buttons remove dithering timer */
pdev->timers = (struct timer_list *) kmalloc(pdata->nbuttons*sizeof(struct timer_list), GFP_KERNEL);
if(NULL == pdev->timers)//内核里的内存申请失败
{
printk("Alloc %s driver for timers failure.\n", DEV_NAME);
return -ENOMEM;
}
memset(pdev->timers, 0, pdata->nbuttons*sizeof(struct timer_list));
/* Malloc for all the buttons status buffer */
pdev->status = (unsigned char *)kmalloc(pdata->nbuttons*sizeof(unsigned char), GFP_KERNEL);
if(NULL == pdev->status)
{
printk("Alloc %s driver for status failure.\n", DEV_NAME);
result = -ENOMEM;
goto ERROR;
}
memset(pdev->status, 0, pdata->nbuttons*sizeof(unsigned char));
init_waitqueue_head(&(pdev->waitq));//初始化等待队列头
for(i=0; i<pdata->nbuttons; i++)
{
/* Initialize all the buttons status to UP */
pdev->status[i] = BUTTON_UP;
/* Initialize all the buttons' remove dithering timer */
setup_timer(&(pdev->timers[i]), button_timer_handler, i); //初始化每个定时器
/* Set all the buttons GPIO to EDGE_FALLING interrupt mode */
s3c2410_gpio_cfgpin(pdata->buttons[i].gpio, pdata->buttons[i].setting);//按键相应的管脚配置成IRQ中断模式
irq_set_irq_type(pdata->buttons[i].nIRQ, IRQ_TYPE_EDGE_FALLING);//把相应的中断号设置为下降沿触发方式
/* Request for button GPIO pin interrupt */
result = request_irq(pdata->buttons[i].nIRQ, s3c_button_intterupt, IRQF_DISABLED, DEV_NAME, (void *)i);//注册给内核,一旦发生中断号的中断就调用s3c_button_intterupt这个中断处理程序
if( result )
{
result = -EBUSY;
goto ERROR1;
}
}
return 0;
ERROR1:
kfree((unsigned char *)pdev->status);
while(--i)
{
disable_irq(pdata->buttons[i].nIRQ);
free_irq(pdata->buttons[i].nIRQ, (void *)i);
}
ERROR:
kfree(pdev->timers);
return result;
}
static int button_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
struct button_device *pdev = file->private_data;
struct s3c_button_platform_data *pdata;
int i, ret;
unsigned int status = 0;
pdata = pdev->data;
dbg_print("ev_press: %d\n", pdev->ev_press);
if(!pdev->ev_press) //ev_press为按键标识符,即按下时才为1.结合下面等待队列程序
{
if(file->f_flags & O_NONBLOCK)
{
dbg_print("read() without block mode.\n");// O_NONBLOCK是设置为非阻塞模式
return -EAGAIN;//若没有按键按下,还采用非阻塞模式则会一直浪费CPU的时间等待。
}
else
{
/* Read() will be blocked here */
dbg_print("read() blocked here now.\n");
wait_event_interruptible(pdev->waitq, pdev->ev_press); //在阻塞模式读取且没按键按下则让等待队列进入睡眠,直到ev_press为1
}
}
pdev->ev_press = 0;//清除标识,准备下一次
for(i=0; i<pdata->nbuttons; i++)
{
dbg_print("button[%d] status=%d\n", i, pdev->status[i]);
status |= (pdev->status[i]<<i);
}
ret = copy_to_user(buf, (void *)&status, min(sizeof(status), count));
return ret ? -EFAULT : min(sizeof(status), count);
}
static unsigned int button_poll(struct file *file, poll_table * wait)//类似于应用层的select轮询监听
{
struct button_device *pdev = file->private_data;
unsigned int mask = 0;
poll_wait(file, &(pdev->waitq), wait);//添加等待队列到等待队列中
if(pdev->ev_press)
{
mask |= POLLIN | POLLRDNORM; /* The data aviable */
}
return mask;
}
static int button_release(struct inode *inode, struct file *file)
{
int i;
struct button_device *pdev = file->private_data;
struct s3c_button_platform_data *pdata;
pdata = pdev->data;
for(i=0; i<pdata->nbuttons; i++)
{
disable_irq(pdata->buttons[i].nIRQ);
free_irq(pdata->buttons[i].nIRQ, (void *)i);
del_timer(&(pdev->timers[i]));
}
kfree(pdev->timers);
kfree((unsigned char *)pdev->status);
return 0;
}
static struct file_operations button_fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.poll = button_poll,
.release = button_release,
};
static int s3c_button_probe(struct platform_device *dev)
{
int result = 0;
dev_t devno;
/* Alloc the device for driver */
if (0 != dev_major)
{
devno = MKDEV(dev_major, dev_minor);
result = register_chrdev_region(devno, 1, DEV_NAME);
}
else
{
result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME);
dev_major = MAJOR(devno);
}
/* Alloc for device major failure */
if (result < 0)
{
printk("%s driver can't get major %d\n", DEV_NAME, dev_major);
return result;
}
/* Initialize button_device structure and register cdev*/
memset(&button_device, 0, sizeof(button_device));
button_device.data = dev->dev.platform_data;
cdev_init (&(button_device.cdev), &button_fops);
button_device.cdev.owner = THIS_MODULE;
result = cdev_add (&(button_device.cdev), devno , 1);
if (result)
{
printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME);
goto ERROR;
}
button_device.dev_class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(button_device.dev_class))
{
printk("%s driver create class failture\n",DEV_NAME);
result = -ENOMEM;
goto ERROR;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
device_create(button_device.dev_class, NULL, devno, NULL, DEV_NAME);
#else
device_create (button_device.dev_class, NULL, devno, DEV_NAME);
#endif
printk("S3C %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
return 0;
ERROR:
printk("S3C %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
cdev_del(&(button_device.cdev));
unregister_chrdev_region(devno, 1);
return result;
}
static int s3c_button_remove(struct platform_device *dev)
{
dev_t devno = MKDEV(dev_major, dev_minor);
cdev_del(&(button_device.cdev));
device_destroy(button_device.dev_class, devno);
class_destroy(button_device.dev_class);
unregister_chrdev_region(devno, 1);
printk("S3C %s driver removed\n", DEV_NAME);
return 0;
}
/*===================== Platform Device and driver regist part ===========================*/
static struct platform_driver s3c_button_driver = {
.probe = s3c_button_probe,
.remove = s3c_button_remove,
.driver = {
.name = "s3c_button",
.owner = THIS_MODULE,
},
};
static int __init s3c_button_init(void)
{
int ret = 0;
ret = platform_device_register(&s3c_button_device);
if(ret)
{
printk(KERN_ERR "%s: Can't register platform device %d\n", __FUNCTION__, ret);
goto fail_reg_plat_dev;
}
dbg_print("Regist S3C %s Device successfully.\n", DEV_NAME);
ret = platform_driver_register(&s3c_button_driver);
if(ret)
{
printk(KERN_ERR "%s: Can't register platform driver %d\n", __FUNCTION__, ret);
goto fail_reg_plat_drv;
}
dbg_print("Regist S3C %s Driver successfully.\n", DEV_NAME);
return 0;
fail_reg_plat_drv:
platform_driver_unregister(&s3c_button_driver);
fail_reg_plat_dev:
return ret;
}
static void s3c_button_exit(void)
{
platform_driver_unregister(&s3c_button_driver);
dbg_print("S3C %s platform device removed.\n", DEV_NAME);
platform_device_unregister(&s3c_button_device);
dbg_print("S3C %s platform driver removed.\n", DEV_NAME);
}
module_init(s3c_button_init);
module_exit(s3c_button_exit);
module_param(debug, int, S_IRUGO);
module_param(dev_major, int, S_IRUGO);
module_param(dev_minor, int, S_IRUGO);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:S3C24XX_button");
****************************************************************************************************************************************************************
说明:这个button_led.c文件中调用了两个头文件s3c_driver.h,plat_ioctl.h,这两个头文件在
我这篇博客当中有定义http://blog.csdn.net/zouleideboke/article/details/68969544
****************************************************************************************************************************************************************
/*小节:学习了这个按键驱动,我还是分析一下这个流程吧!
首先是定义一个struct s3c_button_info结构体,它用来保存按键的一些重要信息,如中断号,管脚配置,按键号等!接着又定义了按键的的数据结构体struct s3c_button_platform_data,下面又紧接着定义了s3c_button_info类型的一个结构体数组用来保存4个按键的具体信息,以及定义了s3c_button_platform_data类型的的一个结构体,里面有两个指针,用来指向获取按键结构体数组中有多少个按键的宏操作。
其次是定义了按键设备结构体,里面保存了一些按键状态status,data,timer,class等指针,下面最为重要的的是platform_device s3c_button_device结构体的定义,里面定义了设备name,设备id,设备dev.
对于这个按键驱动的关键就是接下来的两个中断服务程序s3c_button_intterupt与button_timer_handler,而s3c_button_intterupt处理程序的主要作用是通过判断获取具体的中断号之后便进行去抖的处理以及判断按键是否有效(此时通过按键的状态不管是否按下依然为BUTTON_UP状态,这是我们最初认为设定的)。紧接着通过mod_timer激活定时器,在超时之后然后进入定时器原本设定好的定时器中断处理程序button_timer_handler中正式的处理这个中断请求,也就是说真正处理中断的程序是定时器里面的button_timer_handler( ),再看Button_timer_handler函数,因为当按下的时候进入了s3c_button_intterupt函数然后定时器超时调用到了button_timer_handler里,此时为低电平。然后我们这时候将他设置为BUTTON_DOWN状态。且唤醒等待队列wake_up_interruptible让设备进行读取。ev_press为按键按下的发生标识,按下ev_press即为1。也通过ev_press参数值来设置工作模式,选定阻塞或者是非阻塞模式。
接着是button_read与button_open两个函数的定义,这两个函数是我们在用户空间的系统调用。首先在open函数里面会获得设备信息,并且会动态的申请内存为每一个button都创建一个定时器。再有就是动态的创建存储状态status的buf。然后初始化让所有按键的状态为BUTTON_UP以及调用Setup_timer初始化定时器以及设置定时器的回调函数button_timer_handler.,s3c2410_gpio_cfgpin把相应的管脚设置为IRQ中断模式(否则可能是GPIO模式),irq_set_irq_type把相应的中断号设置为下降沿触发的方式。然后request_irq()是向内核申请中断。一旦发生中断则调用s3c_button_intterupt这个中断处理程序。而对于read函数而言,获取了按键的设备信息之后通过ev_press来剔除非阻塞模式选择阻塞模式,在阻塞模式下读取且没按键按下则让等待队列进入睡眠,直到ev_press为1。通过位运算来判断哪一个按键按下并保存状态值到status中。
另外还有poll函数,类似于应用层select,它们的原理是差不多的,主要是通过轮询方式不断的监听文件描述符,先是将所有的文件描述符阻塞着,当所有文件描述符发生可读时,函数立即返回,当然如果有超时的话,文件描述符仍然不可读,select就一直阻塞着!然而驱动中的轮询button_poll这个与应用程序中select的使用相对应,也就是说用户空间调用select时接着就会调用驱动中的button_poll。
下面是button_release函数的定义,这个函数的作用是释放中断号,定时器以及释放之前定时器和按键status申请的内存空间!接着是结构体·file_operations button_fops的定义里面保存的是一些函数操作!再接着是 s3c_button_probe与s3c_button_remove函数的定义,s3c_button_probe函数功能是注册主次设备号以及自动创建设备节点,而s3c_button_remove功能是删除cdev,device,以及class类。
最后是platform_driver s3c_button_driver驱动的定义,以及按键模块的初始化和模块退出清除函数的定义!
嘿嘿!^_^,到这里整个按键驱动流程算是分析完了,不过感觉好多文字啊!!!(^_^)
参考:http://blog.csdn.net/u010944778/article/details/45113687/感觉这个学长分析蛮好的,值得借鉴!
*****************************************************************************************************************************
总结:学习这个驱动遇到很多新的知识点,总结一下!
1.DMA(协处理器):功能是将外设的数据搬到内存中,或者将硬盘中的数据搬运到内存中。
2.kmalloc是内核空间动态申请内存的一个函数,而malloc是用户空间动态申请内存的一个函数,它会用到C库,kmalloc用不到C库,因为内核中没有C库。
3.int mod_timer(struct timer_list *timer, unsigned long expires)
{
expires = apply_slack(timer, expires);
* This is a common optimization triggered by the
* networking code – if the timer is re-modified
* to be the same thing then just return:
*/
if (timer_pending(timer) && timer->expires == expires)
return 1;
}
原型: void wake_up_interruptible ( wait_queue_head_t * q);
变量: q : 等待队列变量指针。
wait_event_interruptible(wq, condition),该函数修改task的状态为TASK_INTERRUPTIBLE,意味着该进程将不会继续运行直到被唤醒,然后 被添加到等待队列wq中。
#define wait_event_interruptible(wq, condition) \
({ \
int __ret = 0; \
if (!(condition)) \
__wait_event_interruptible(wq, condition, __ret);\
__ret; \
在无限循环中,__wait_event_interruptible()将本进程置为可中断的挂起状态,反复检查condition是否成立,如果成立则退出,如果不成立则继续休眠;条件满足后,即把本进程运行状态置为运行态