您的位置:首页 > 运维架构 > Linux

Linux设备驱动开发基础---字符设备驱动程序开发之基于中断的按键驱动

2013-07-09 22:18 477 查看
二、硬件原理分析

Mini2440 具有6 个用户测试按键,它们都是连接到CPU 的中断引脚。如图:



由原理图可知,这些引脚在按键没有按下的情况下被上拉为高电平,按键被按下的时候变为低电平。

三、实现方式

1、在/linux-2.6.32.2/drivers/buttons目录下创建一个新的驱动程序文件mini2440_buttons.c,内容及详细注释如下:

#include <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/init.h>

#include <linux/delay.h>

#include <linux/poll.h>

#include <linux/irq.h>

#include <asm/irq.h>

#include <linux/interrupt.h>

#include <asm/uaccess.h>

#include <mach/regs-gpio.h>

#include <mach/hardware.h>

#include <linux/platform_device.h>

#include <linux/cdev.h>

#include <linux/sched.h>

#include <linux/gpio.h>

#include <linux/device.h>

/*创建类和设备*/

static struct class *button_class;

static struct device *button_class_dev;

#define DEVICE_NAME "buttons" //加载模式后,执行”cat /proc/devices”命令看到的设备名称

/*

*定义中断所用的结构体

*/

struct button_irq_desc {

int irq; //按键对应的中断号

int pin; //按键所对应的GPIO 端口

int number; //定义键值,以传递给应用层/用户态

char *name; //每个按键的名称

};

/*

*结构体实体定义

*/

static struct button_irq_desc button_irqs[]={

{IRQ_EINT8 , S3C2410_GPG(0) , 0, "K1"},

{IRQ_EINT11, S3C2410_GPG(3) , 1, "K2"},

{IRQ_EINT13, S3C2410_GPG(5) , 2, "K3"},

{IRQ_EINT14, S3C2410_GPG(6) , 3, "K4"},

{IRQ_EINT15, S3C2410_GPG(7) , 4, "K5"},

{IRQ_EINT19, S3C2410_GPG(11) , 5, "K6"},

};

/*

*开发板上按键的状态变量,注意这里是'0',对应的ASCII 码为30

*/

static volatile char key_values[] = {'0', '0', '0', '0', '0', '0'};

/*

*因为本驱动是基于中断方式的,在此创建一个等待队列,以配合中断函数使用;当有按键按下并读取到键

*值时,将会唤醒此队列,并设置中断标志,以便能通过read函数判断和读取键值传递到用户态;当没有按

*键按下时,如果有进程调用mini2440_buttons_read函数,它将休眠

*/

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/*

*中断标识变量,配合上面的队列使用,中断服务程序会把它设置为1,read 函数会把它清零

*/

static volatile int ev_press = 0;

/*

*按键驱动的中断服务程序

*/

static irqreturn_t button_interrupt(int irq, void *dev_id)

{

struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;

int down;

/*获取被按下的按键状态*/

down = !s3c2410_gpio_getpin(button_irqs->pin);

/*状态改变,按键被按下,从这句可以看出,当按键没有被按下的时候,寄存器的值为1(上拉),当按

键被按下的时候,寄存器对应的值为0*/

if (down != (key_values[button_irqs->number] & 1)) { // Changed

/*如果key1 被按下,则key_value[0]就变为'1',对应的ASCII 码为31*/

key_values[button_irqs->number] = '0' + down;

ev_press = 1; /*设置中断标志为1*/

wake_up_interruptible(&button_waitq); /*唤醒等待队列*/

}

return IRQ_RETVAL(IRQ_HANDLED);/*具体解释见LDD3p272,增加了中断返回,来区别那些伪中断,

驱动程序正在处理那个中断时却返回了 IRQ_NONE,说明存在bug*/

}

/*

*在应用程序执行open("/dev/buttons",…)时会调用到此函数,在这里,它的作用主要是注册6 个按键的中断。

*所用的中断类型是IRQ_TYPE_EDGE_BOTH,也就是双沿触发,在上升沿和下降沿均会产生中断,这样做

*是为了更加有效地判断按键状态

*/

static int button_open(struct inode *inode, struct file *file)

{

int i;

int err = 0;

for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {

/*注册中断函数,如果注册成功,则6个按键GPIO引脚的功能被设置为外部中断,触发方式为设定的触发方式

详解见韦东山完全开发手册P406*/

err = request_irq(button_irqs[i].irq, button_interrupt, IRQ_TYPE_EDGE_BOTH,

button_irqs[i].name, (void *)&button_irqs[i]);

if (err)

break;

}

if (err) { /*如果出错,释放已经注册的中断,并返回*/

i--;

for (; i >= 0; i--) {

disable_irq(button_irqs[i].irq);

free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);

}

return -EBUSY;

}

/*注册成功,则中断队列标记为1,表示可以通过read 读取*/

ev_press = 1;

/*正常返回*/

return 0;

}

/*

*此函数对应应用程序的系统调用close(fd)函数,在此,它的主要作用是当关闭设备时释放6 个按键的中断*

*处理函数

*/

static int button_close(struct inode *inode, struct file *file)

{

int i;

for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {

if (button_irqs[i].irq < 0) {

continue;

}

/*释放中断号,并注销中断处理函数*/

free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);

}

return 0;

}

/*

*对应应用程序的read(fd,…)函数,主要用来向用户空间传递键值

*/

static int button_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)

{

unsigned long err;

if (!ev_press) {

if (filp->f_flags & O_NONBLOCK)

/*当中断标识为0 时,并且该设备是以非阻塞方式打开时,返回*/

return -EAGAIN;

else

/*当中断标识为0 时,并且该设备是以阻塞方式打开时,进入休眠状态,等待被唤醒*/

wait_event_interruptible(button_waitq, ev_press);

}

/*把中断标识清零*/

ev_press = 0;

/*一组键值被传递到用户空间*/

err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));

return err ? -EFAULT : min(sizeof(key_values), count);

}

/* 这个结构是字符设备驱动程序的核心

* 当应用程序操作设备文件时所调用的open、read、write等函数,

* 最终会调用这个结构中的对应函数

*/

static struct file_operations buttons_fops = {

.owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */

.open = button_open,

.release = button_close,

.read = button_read,

};

/*

* 执行“insmod mini2440_buttons.ko”命令时就会调用这个函数

*/

int button_major;

static int __init button_init(void)

{

/* 注册字符设备驱动程序

* 参数为主设备号、设备名字、file_operations结构;

* 这样,主设备号就和具体的file_operations结构联系起来了,

* 操作主设备为BUTTON_MAJOR的设备文件时,就会调用mini2440_buttons_fops中的相关成员函数

* BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号

*/

//ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &mini2440_buttons_fops);

button_major = register_chrdev(0, DEVICE_NAME, &buttons_fops);

if (button_major < 0) {

printk(DEVICE_NAME " can't register major number\n");

return button_major;

}

button_class = class_create(THIS_MODULE, DEVICE_NAME); //创建类

button_class_dev = device_create(button_class, NULL, MKDEV(button_major, 0), NULL, DEVICE_NAME); /* 创建设备/dev/buttons */

printk(DEVICE_NAME " initialized\n");

return 0;

}

/*

* 执行”rmmod mini2440_buttons.ko”命令时就会调用这个函数

*/

static void __exit button_exit(void)

{

/* 卸载驱动程序 */

device_unregister(button_class_dev); //删除设备

class_destroy(button_class); //删除类,顺序不能颠倒

unregister_chrdev(button_major, DEVICE_NAME);

}

/* 这两行指定驱动程序的初始化函数和卸载函数 */

module_init(button_init);

module_exit(button_exit);

/* 描述驱动程序的一些信息,不是必须的 */

MODULE_AUTHOR("DreamCatcher"); // 驱动程序的作者

MODULE_DESCRIPTION("MINI2440 BUTTON Driver"); // 一些描述信息

MODULE_LICENSE("GPL"); //版权信息

2、编写本目录下的Makefile文件

KERN_DIR = /work/armlinux/linux-2.6.32.2

all:

make -C $(KERN_DIR) M=`pwd` modules

clean:

make -C $(KERN_DIR) M=`pwd` modules clean

rm -rf modules.order

obj-m := mini2440_buttons.o

3、测试

为了测试该驱动程序,我们还需要编写一个简单的测试程序,在友善官方提供的光盘中已经提供了该测试程序的源代码

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/ioctl.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <sys/select.h>

#include <sys/time.h>

#include <errno.h>

int main(void)

{

int buttons_fd;

char buttons[6] = {'0', '0', '0', '0', '0', '0'}; //定义按键值变量,对于驱动函数中的key_values 数组

buttons_fd = open("/dev/buttons", 0); /*打开按键设备/dev/buttons*/

if (buttons_fd < 0) {

perror("open device buttons"); /*打开失败则退出*/

exit(1);

}

for (;;) { /*永读按键并打印键值和状态*/

char current_buttons[6];

int count_of_changed_key;

int i;

/*使用read 函数读取一组按键值(6 个)*/

if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {

perror("read buttons:");

exit(1);

}

/*逐个分析读取到的按键值*/

for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {

if (buttons[i] != current_buttons[i]) {

buttons[i] = current_buttons[i];

/*打印按键值,并标明按键按下/抬起的状态*/

printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "up" : "down");

count_of_changed_key++;

}

}

if (count_of_changed_key) {

printf("\n");

}

}

close(buttons_fd); /*关闭按键设备文件*/

return 0;

}

编译后进行测试:

root@MINI2440:/lib/modules/2.6.32.2# insmod mini2440_buttons.ko

buttons initialized

root@MINI2440:/lib/modules/2.6.32.2# cd /opt

root@MINI2440:/opt# ls

Helloword backlight_test buttons_test led_test

root@MINI2440:/opt# ./buttons_test

key 1 is down

key 1 is up

key 2 is down

key 2 is up

key 3 is down

key 3 is up

此驱动程序还存在不少问题,首先对于request_irq函数的用法,目前只查到韦东山的书中描述的用法,在网上没有查到关于参数flags,如设置为中断的触发方式时,会调用irq_desc[irq]结构中的chip->set_type成员函数进行设置:设置引脚功能为外部中断,设置中断触发方式。当然这样设置没有错误,否则驱动也不能正常运行了,自己对于内核的讲解也是一知半解,后续将按照网上大部分的设置方式对驱动进行更改。

其次,按键没有去抖动,后续将参照网上的方法进行更改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐