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、按键驱动的流程图

a4c26d1e5885305701be709a3d33442f.png

三、驱动程序

#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键盘驱动程序分析)