歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 詳細注釋FL2440按鍵中斷驅動(含poll機制)

詳細注釋FL2440按鍵中斷驅動(含poll機制)

日期:2017/3/1 10:17:02   编辑:Linux編程

詳細注釋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");

Copyright © Linux教程網 All Rights Reserved