linux键盘驱动程序分析,基于Linux按键驱动分析与编程
硬件平臺:Mini2440
Size of NAND:256M
linux kernel:linux-2.6.32.2
一、首先編寫按鍵驅動要用到的Mini2440的硬件是中斷控制器和定時器
那么linux-2.6.32.2的中斷號的預定義文件IRQ(X)是在2.6.32.2/arch/arm/mach-s3c2410/include/mach的irq.h的頭文件中如下:
#ifndef __ASM_ARCH_IRQS_H
#define __ASM_ARCH_IRQS_H __FILE__
#define S3C2410_CPUIRQ_OFFSET?(16)
#define S3C2410_IRQ(x) ((x) + S3C2410_CPUIRQ_OFFSET)
#define
IRQ_EINT0?S3C2410_IRQ(0)?#define
IRQ_EINT1?S3C2410_IRQ(1)
#define
IRQ_EINT2?S3C2410_IRQ(2)
#define
IRQ_EINT3?S3C2410_IRQ(3)
#define
IRQ_EINT4t7?S3C2410_IRQ(4)?#define IRQ_EINT8t23?S3C2410_IRQ(5)
#define IRQ_RESERVED6?S3C2410_IRQ(6)?#define
IRQ_CAM?S3C2410_IRQ(6)?#define IRQ_BATT_FLT?S3C2410_IRQ(7)
#define
IRQ_TICK?S3C2410_IRQ(8)?#define
IRQ_WDT?S3C2410_IRQ(9)?#define
IRQ_TIMER0?S3C2410_IRQ(10)
#define
IRQ_TIMER1?S3C2410_IRQ(11)
#define
IRQ_TIMER2?S3C2410_IRQ(12)
#define
IRQ_TIMER3?S3C2410_IRQ(13)
#define
IRQ_TIMER4?S3C2410_IRQ(14)
#define
IRQ_UART2?S3C2410_IRQ(15)
#define
IRQ_LCD?S3C2410_IRQ(16)?#define
IRQ_DMA0?S3C2410_IRQ(17)?#define
IRQ_DMA1?S3C2410_IRQ(18)
#define
IRQ_DMA2?S3C2410_IRQ(19)
#define
IRQ_DMA3?S3C2410_IRQ(20)
#define
IRQ_SDI?S3C2410_IRQ(21)
#define
IRQ_SPI0?S3C2410_IRQ(22)
#define
IRQ_UART1?S3C2410_IRQ(23)
#define IRQ_RESERVED24
S3C2410_IRQ(24)?#define
IRQ_NFCON?S3C2410_IRQ(24)?#define
IRQ_USBD?S3C2410_IRQ(25)
#define
IRQ_USBH?S3C2410_IRQ(26)
#define
IRQ_IIC?S3C2410_IRQ(27)
#define
IRQ_UART0?S3C2410_IRQ(28)?#define
IRQ_SPI1?S3C2410_IRQ(29)
#define
IRQ_RTC?S3C2410_IRQ(30)
#define IRQ_ADCPARENT?S3C2410_IRQ(31)
#define
IRQ_EINT4?S3C2410_IRQ(32)?#define
IRQ_EINT5?S3C2410_IRQ(33)
#define
IRQ_EINT6?S3C2410_IRQ(34)
#define
IRQ_EINT7?S3C2410_IRQ(35)
#define
IRQ_EINT8?S3C2410_IRQ(36)
#define
IRQ_EINT9?S3C2410_IRQ(37)
#define
IRQ_EINT10?S3C2410_IRQ(38)
#define
IRQ_EINT11?S3C2410_IRQ(39)
#define
IRQ_EINT12?S3C2410_IRQ(40)
#define
IRQ_EINT13?S3C2410_IRQ(41)
#define
IRQ_EINT14?S3C2410_IRQ(42)
#define
IRQ_EINT15?S3C2410_IRQ(43)
#define
IRQ_EINT16?S3C2410_IRQ(44)
#define
IRQ_EINT17?S3C2410_IRQ(45)
#define
IRQ_EINT18?S3C2410_IRQ(46)
#define
IRQ_EINT19?S3C2410_IRQ(47)
#define
IRQ_EINT20?S3C2410_IRQ(48)?#define
IRQ_EINT21?S3C2410_IRQ(49)
#define
IRQ_EINT22?S3C2410_IRQ(50)
#define
IRQ_EINT23?S3C2410_IRQ(51)
#define IRQ_EINT_BIT(x)?((x) - IRQ_EINT4 +
4)
#define
IRQ_EINT(x)?(((x) >= 4) ? (IRQ_EINT4 + (x) - 4) : (IRQ_EINT0 + (x)))
#define IRQ_LCD_FIFO?S3C2410_IRQ(52)
#define IRQ_LCD_FRAME?S3C2410_IRQ(53)
#define
S3C2410_IRQSUB(x)?S3C2410_IRQ((x)+54)
#define
IRQ_S3CUART_RX0?S3C2410_IRQSUB(0)?#define
IRQ_S3CUART_TX0?S3C2410_IRQSUB(1)
#define IRQ_S3CUART_ERR0?S3C2410_IRQSUB(2)
#define
IRQ_S3CUART_RX1?S3C2410_IRQSUB(3)?#define
IRQ_S3CUART_TX1?S3C2410_IRQSUB(4)
#define IRQ_S3CUART_ERR1?S3C2410_IRQSUB(5)
#define
IRQ_S3CUART_RX2?S3C2410_IRQSUB(6)?#define
IRQ_S3CUART_TX2?S3C2410_IRQSUB(7)
#define IRQ_S3CUART_ERR2?S3C2410_IRQSUB(8)
#define
IRQ_TC?S3C2410_IRQSUB(9)
#define
IRQ_ADC?S3C2410_IRQSUB(10)
#define IRQ_S3C2412_CFSDI?S3C2410_IRQ(21)
#define
IRQ_S3C2412_SDI?S3C2410_IRQSUB(13)
#define
IRQ_S3C2412_CF?S3C2410_IRQSUB(14)
#define
IRQ_S3C2440_CAM_C?S3C2410_IRQSUB(11)?#define
IRQ_S3C2440_CAM_P?S3C2410_IRQSUB(12)?#define
IRQ_S3C2440_WDT?S3C2410_IRQSUB(13)
#define IRQ_S3C2440_AC97?S3C2410_IRQSUB(14)
#define
IRQ_S3C2443_DMA?S3C2410_IRQ(17)?#define
IRQ_S3C2443_UART3?S3C2410_IRQ(18)?#define
IRQ_S3C2443_CFCON?S3C2410_IRQ(19)?#define
IRQ_S3C2443_HSMMC?S3C2410_IRQ(20)?#define
IRQ_S3C2443_NAND?S3C2410_IRQ(24)
#define
IRQ_HSMMC0?IRQ_S3C2443_HSMMC
#define
IRQ_S3C2443_LCD1?S3C2410_IRQSUB(14)
#define IRQ_S3C2443_LCD2?S3C2410_IRQSUB(15)
#define IRQ_S3C2443_LCD3?S3C2410_IRQSUB(16)
#define IRQ_S3C2443_LCD4?S3C2410_IRQSUB(17)
#define
IRQ_S3C2443_DMA0?S3C2410_IRQSUB(18)
#define IRQ_S3C2443_DMA1?S3C2410_IRQSUB(19)
#define IRQ_S3C2443_DMA2?S3C2410_IRQSUB(20)
#define IRQ_S3C2443_DMA3?S3C2410_IRQSUB(21)
#define IRQ_S3C2443_DMA4?S3C2410_IRQSUB(22)
#define IRQ_S3C2443_DMA5?S3C2410_IRQSUB(23)
#define
IRQ_S3C2443_RX3?S3C2410_IRQSUB(24)
#define
IRQ_S3C2443_TX3?S3C2410_IRQSUB(25)
#define IRQ_S3C2443_ERR3?S3C2410_IRQSUB(26)
#define
IRQ_S3C2443_WDT?S3C2410_IRQSUB(27)
#define IRQ_S3C2443_AC97?S3C2410_IRQSUB(28)
#ifdef CONFIG_CPU_S3C2443
#define NR_IRQS (IRQ_S3C2443_AC97+1)
#else
#define NR_IRQS (IRQ_S3C2440_AC97+1)
#endif
#define
IRQ_UART3?IRQ_S3C2443_UART3
#define
IRQ_S3CUART_RX3?IRQ_S3C2443_RX3
#define
IRQ_S3CUART_TX3?IRQ_S3C2443_TX3
#define IRQ_S3CUART_ERR3?IRQ_S3C2443_ERR3
#ifdef CONFIG_CPU_S3C2440
#define IRQ_S3C244x_AC97 IRQ_S3C2440_AC97
#else
#define IRQ_S3C244x_AC97 IRQ_S3C2443_AC97
#endif
#define FIQ_START?IRQ_EINT0
#endif
//
那么linux下面的定義的中斷函數在Linux-2.6.32.2/kernel/irq/manage.c里面。
中斷的預定義處理命令函數則定義在Linux-2.6.32.2/include/linux/irq.h和interrupt.h里面。
二、驅動使用到中斷之外,按鍵的驅動程序還需要用到定時器。那么定時器在按鍵驅動程序這邊的作用是去除按鍵的抖動作用,一般來說當按鍵驅動程序檢測到按鍵按下之后,還要啟動Linux內部的定時器延時20ms左右,然后再去檢查相應的引腳是否還是低電平。若還是低電平則表示按鍵是真的有按下,若這時候檢測到引腳是高電平那么則可以判斷按鍵比沒有真正的按下去,而是有可能是外部的干擾所致的。所以按鍵驅動程序中的定時器對于一個穩定可靠的按鍵驅動程序時至關重要的。
1、Linux的內核定時器是內核用來控制在未來某個時間點(基于jiffies)調度執行某個函數的一種機制,其實現位于?和?kernel/timer.c?文件中。
內核定時器是內核用來控制在未來某個時間點(基于jiffies)調度執行某個函數的一種機制,其實現位于和
kernel/timer.c?文件中。
2、按鍵驅動的流程圖
三、驅動程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define uchar unsigned char
#define uint?unsigned
int
#define CLEAR_CACHE?0x0f
#define KEY_STATUES_X
0?//按鍵未確定的標識
#define
KEY_DOWN?1?//按鍵按下去的標識
#define
KEY_UP?2?//按鍵釋放的標識
#define KEY_MAJOR 250
//按鍵驅動程序的主設備號(主設備號不是固定的)
#define KEY_SUBMAJOR
0?//按鍵驅動程序的次設備號
#define DEVICE_NAME "key_drive"
//按鍵驅動程序的設備名
#define KEY_NUM
6?//按鍵的數量
#define NUM_KEY_STATE KEY_NUM?//按鍵的狀態數量
#define NUM_KEY_CACHE
16?//按鍵的緩沖器
#define MOD
NUM_KEY_CACHE?//按鍵的循環指針
#define DELAY_TIME_20MS
HZ/50?//按鍵延時
#define KEY_STATUES_20MS?DELAY_TIME_20MS
#define DELAY_TIME_100MS HZ/10
#define KEY_STATUES_100MS?DELAY_TIME_100MS
#define POINTER_BUFFER_CYCLE(x) ((++x)&(MOD-1))
//按鍵的緩沖循環指針定義
static void hander_timer(unsigned long key_num);
//按鍵的定時器
void key_event(void); ?//按鍵的時間產生函數
static void key_setup_cdev(void);
static volatile unsigned char interrupt_num;
unsigned int key_major=KEY_MAJOR;
unsigned long flags;
static struct file_operations key_operation;
struct key_char_dev
{
struct
cdev cdev;
uchar
key[NUM_KEY_STATE];
uchar
key_cache[NUM_KEY_CACHE]; //按鍵的緩沖器
uint?head_read;?//按鍵的讀緩沖指針
uint?head_writer;?//按鍵的寫緩沖指針
struct
timer_list key_timer[KEY_NUM];?//定時器結構體
wait_queue_head_t
key_queue;?//等待隊列頭
};
struct key_info
{
uint
irq_num;?//按鍵對應的中斷號
uint
gpio_num;?//按鍵對應的引腳
uchar
key_num;?//按鍵本身的標號
};
struct key_char_dev *key_cdev;
struct key_info key_inform[KEY_NUM]={
{IRQ_EINT8 ,S3C2410_GPG(0), 0},
{IRQ_EINT11,S3C2410_GPG(3), 1},
{IRQ_EINT13,S3C2410_GPG(5), 2},
{IRQ_EINT14,S3C2410_GPG(6), 3},
{IRQ_EINT15,S3C2410_GPG(7), 4},
{IRQ_EINT19,S3C2410_GPG(11),5}
};
static unsigned int key_down(unsigned int key)
{
return s3c2410_gpio_getpin(key);
}
static void init_key_timer(void)
{
unsigned
char i;
for(i=0;i
{
init_timer(&(key_cdev->key_timer[i]));
key_cdev->key_timer[i].data=(unsigned long)i;
key_cdev->key_timer[i].function=hander_timer;
}
}
static void hander_timer(unsigned long key_num)
{
//先判斷對應引腳的電平是否為低電平
if(key_down((unsigned int)(key_inform[key_num].gpio_num))==0)
{
//如果按鍵上一次的狀態是不確定的狀態,那么再一次檢測到引腳的電平是低電平。那么這時就可以確定按鍵是按下去,則設置對應按鍵標識緩沖器為“按下去”的狀態。
if(key_cdev->key[key_num]==KEY_STATUES_X)
{
key_cdev->key[key_num]=KEY_DOWN;
key_event(); ?//這時候啟動定時器進行延時
key_cdev->key_timer[key_num].expires=jiffies+KEY_STATUES_20MS;
add_timer(&(key_cdev->key_timer[key_num]));
printk("\n Go in the here:judge_function: irq %d",key_num);
}
else
{
//如果按鍵的狀態是已經處于按下去的狀態,并且在一次檢測到按鍵還沒彈起來,則繼續啟動定時器的延時,知道按鍵被釋放為止。
key_cdev->key_timer[key_num].expires=jiffies+KEY_STATUES_100MS;
add_timer(&(key_cdev->key_timer[key_num]));
}
}
else
{
//檢測到按鍵被釋放,則設置按鍵的狀態為釋放狀態,并且重新使能該按鍵的中斷。
key_cdev->key[key_num]=KEY_UP;
enable_irq(key_inform[key_num].irq_num);
printk("\n Go in the here:hander_timer: irq %d",key_num);
}
}
//按鍵的中斷處理函數
static irqreturn_t key_hander(int irq,void
*dev_id)?//The type of the irqreturn_t is
'int';
{
unsigned
char i;
unsigned
int get_irq;
get_irq=irq;
printk("\n Go in the here 1: irq");
//查找是由哪一個按鍵產生觸發中斷的事件。
for(i=0;i
{
if(key_inform[i].irq_num==get_irq)
{
interrupt_num=i;
break;
}
}
printk("\n Go in the here 2: irq");
key_cdev->key[interrupt_num]=KEY_STATUES_X;
//當檢測到有相應的按鍵按下去的時候,則需要關閉對應的按鍵中斷標識位,以防止發生再次的中斷。
disable_irq_nosync(key_inform[interrupt_num].irq_num);
//設置定時器定時20ms,檢查是否按鍵有按下
key_cdev->key_timer[interrupt_num].expires=jiffies+DELAY_TIME_20MS;
printk("\n Go in the here 3: irq %d",interrupt_num);
add_timer(&key_cdev->key_timer[interrupt_num]);
printk("\n Go in the here 4: irq");
return
interrupt_num;
}
//按鍵事件的處理函數
void key_event(void)
{
//增加按鍵的寫頭指針
key_cdev->head_writer=POINTER_BUFFER_CYCLE(key_cdev->head_writer);
//把按鍵號存入到按鍵的緩沖器中,以便應用程序能夠讀取
key_cdev->key_cache[key_cdev->head_writer]=interrupt_num;
//喚醒按鍵的讀程序
wake_up_interruptible(&key_cdev->key_queue);
printk("\n Go in the here:head_writer
%d",key_cdev->head_writer);
printk("\n Go in the here:head_read
%d",key_cdev->head_read);
printk("\n Go in the here:key_event: irq %d",interrupt_num);
}
//加載字符設備
static void key_setup_cdev(void)
{
int
ret_num;
dev_t
dev_num;
dev_num=MKDEV(key_major,KEY_SUBMAJOR);
cdev_init(&key_cdev->cdev,&key_operation);
key_cdev->cdev.owner=THIS_MODULE;
ret_num=cdev_add(&key_cdev->cdev,dev_num,1);
if(ret_num<0)
{
printk("Error adding dev ");
}
}
static int?__init?key_dev_init(void)
{
//注冊字符設備
dev_t
dev_num;
int
ret_num,i,error_irq;
dev_num=MKDEV(key_major,KEY_SUBMAJOR);
if(dev_num)
{
ret_num=register_chrdev_region(dev_num,1,"key_drive");
}
else
{
ret_num=alloc_chrdev_region(&dev_num,0,1,"key_drive");
}
if(ret_num<0)
{
return ret_num;
}
key_cdev=kmalloc(sizeof(struct key_char_dev),GFP_KERNEL);
if(!key_cdev)
{
ret_num=-ENOMEM;
goto fail_request;
}
memset(key_cdev,0,sizeof(struct key_char_dev));
key_setup_cdev();
//初始化中斷
for(i=0;i
{
set_irq_type(key_inform[i].irq_num,IRQF_TRIGGER_FALLING);
error_irq=request_irq(key_inform[i].irq_num,key_hander,IRQF_DISABLED,"key_drive",NULL);
if(error_irq<0)
{
if(error_irq==-EINVAL)
{
printk("error:The interrupt number is invalid");
}
else if(error_irq==-EBUSY)
{
printk("error:The interrupt number have been employed");
}
}
}
//初始化等待隊列頭
init_waitqueue_head(&(key_cdev->key_queue));//等待隊列頭文件的初始化
init_key_timer();//定時器的初始化
fail_request:
unregister_chrdev_region(&dev_num,1);
return ret_num;
}
static int?key_open(struct inode
*inodep,struct file *filep)
{
printk("succeful key open");
key_cdev->head_read=key_cdev->head_writer=0;
return
0;
}
static ssize_t key_read(struct file *filp,char __user
*buf,size_t count,loff_t *pps)
{
unsigned
char keynum;
unsigned
long error;
re_check:
if(key_cdev->head_read!=key_cdev->head_writer)
{
local_irq_save(flags);
keynum=key_cdev->key_cache[key_cdev->head_read];
key_cdev->head_read=POINTER_BUFFER_CYCLE(key_cdev->head_read);
error=copy_to_user(buf,&keynum,sizeof(unsigned char));
local_irq_restore(flags);
if(error)
{
printk("error:The copy_to_user is invalid");
return 0;
}
return sizeof(unsigned
char);
}
else
{
if(filp->f_flags&O_NONBLOCK)?//在應用程序打開時如果OPEN函數有指定O_NONBLOCK參數那么將在這里起作用
{?//O_NONBLOCK這個參數就是驅動程序讀數據時是執行非阻塞操
return
-EAGAIN;?//This is a once again flag
}
//如果不能立即獲取按鍵的值,或者根本就沒有按鍵按下去,則按鍵驅動程序進入睡眠狀態
interruptible_sleep_on(&(key_cdev->key_queue));
goto re_check;
}
return 0;
}
static int key_ioctl(struct inode *inodep,struct file
*filp,unsigned int cmd)
{
unsigned
char i;
switch(cmd)
{
case CLEAR_CACHE :
local_irq_disable();
key_cdev->head_writer=key_cdev->head_read=0;
for(i=0;i
{
key_cdev->key_cache[NUM_KEY_CACHE]=0;
}
local_irq_enable();
break;
default:
return -EINVAL;
}
return 0;
}
static struct file_operations key_operation={
.open=key_open,
.read=key_read,
.ioctl=key_ioctl,
.owner=THIS_MODULE
};
static void __exit key_dev_exit(void)
{
dev_t
dev_num;
unsigned
char i;
dev_num=MKDEV(key_major,KEY_SUBMAJOR);
unregister_chrdev_region(dev_num,1);
cdev_del(&(key_cdev->cdev));
kfree(key_cdev);
key_cdev=NULL;
for(i=0;i
{
free_irq(key_inform[i].irq_num,NULL);
}
}
module_init(key_dev_init);
module_exit(key_dev_exit);
Makefile文件:
1 obj-m := key.o
2 CC :=arm-linux-gcc
3 KDIR := /opt/linux-2.6.32.2
4 PWD := $(shell pwd)
5 default:
6?$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
7 clean:
8?rm -rf *.o
9?rm -rf *.ko
10?rm -rf .*.cmd
11?rm -rf *.mod.*
12
三、測試程序
#include
#include
#include
#include
#include
#include
int main(void)
{
int fd,come_value;
unsigned char Buff;
fd=open("/dev/key",O_NONBLOCK);
if(fd<0)
{
printf("error: Open the file come out error");
exit(1);
}
while(1)
{
read(fd,&Buff,sizeof(unsigned char));
printf("The keys value is :%d\n",Buff);
// sleep(1);
}
close(fd);
return
0;
}
Makefile文件:
1 CC := arm-linux-gcc
2
3 key_app : key_app.o
4?$(CC) -o key_app key_app.c
5
6 clean:
7?rm -rf key_*.o*~
8
總結
以上是生活随笔為你收集整理的linux键盘驱动程序分析,基于Linux按键驱动分析与编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: verycd没有的资源有很多方法下载
- 下一篇: Boilsoft Video Split