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

linux-2.6.32在mini2440开发板上移植 ---按键驱动程序移植

2013-09-20 14:36 501 查看
编者:按键驱动程序涉及到linux中断程序的编写。

1、按键原理图。



2、驱动程序的编写移植。

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

<span style="font-size: 18px;">#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/miscdevice.h>

#include <linux/sched.h>

#include <linux/gpio.h>

#define DEVICE_NAME "buttons" <span style="color: rgb(255, 0, 0);">//设备名称

</span><span style="color: rgb(255, 0, 0);">/*定义中断所用的结构体*/

</span>struct button_irq_desc {

int irq; <span style="color: rgb(255, 0, 0);">//按键对应的中断号

</span>int pin; <span style="color: rgb(255, 0, 0);">//按键所对应的GPIO 端口

</span>int pin_setting; <span style="color: rgb(255, 0, 0);">//按键对应的引脚描述,实际并未用到,保留</span>

int number; <span style="color: rgb(255, 0, 0);">//定义键值,以传递给应用层/用户态

</span>char *name; <span style="color: rgb(255, 0, 0);">//每个按键的名称

</span>};

<span style="color: rgb(255, 0, 0);">/*结构体实体定义*/

</span>static struct button_irq_desc button_irqs [] = {

{IRQ_EINT8 , S3C2410_GPG(0) , S3C2410_GPG0_EINT8 , 0, "KEY0"},

{IRQ_EINT11, S3C2410_GPG(3) , S3C2410_GPG3_EINT11 , 1, "KEY1"},

{IRQ_EINT13, S3C2410_GPG(5) , S3C2410_GPG5_EINT13 , 2, "KEY2"},

{IRQ_EINT14, S3C2410_GPG(6) , S3C2410_GPG6_EINT14 , 3, "KEY3"},

{IRQ_EINT15, S3C2410_GPG(7) , S3C2410_GPG7_EINT15 , 4, "KEY4"},

{IRQ_EINT19, S3C2410_GPG(11), S3C2410_GPG11_EINT19, 5, "KEY5"},

};

<span style="color: rgb(255, 0, 0);">/*开发板上按键的状态变量,注意这里是’0’,对应的ASCII 码为30*/

</span>static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};

<span style="color: rgb(255, 0, 0);">/*因为本驱动是基于中断方式的,在此创建一个等待队列,以配合中断函数使用;当有按键按下并读取到键

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

键按下时,系统并不会轮询按键状态,以节省时钟资源*/

</span>static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

<span style="color: rgb(255, 0, 0);">/*中断标识变量,配合上面的队列使用,中断服务程序会把它设置为1,read 函数会把它清零*/</span>

static volatile int ev_press = 0;

<span style="color: rgb(255, 0, 0);">/*本按键驱动的中断服务程序*/

</span>static irqreturn_t buttons_interrupt(int irq, void *dev_id)

{

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

int down;

// udelay(0);

<span style="color: rgb(255, 0, 0);">/*获取被按下的按键状态*/

</span>down = !s3c2410_gpio_getpin(button_irqs->pin);

/<span style="color: rgb(255, 0, 0);">*状态改变,按键被按下,从这句可以看出,当按键没有被按下的时候,寄存器的值为1(上拉),但按

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

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

<span style="color: rgb(255, 0, 0);">/*如果key1 被按下,则key_value[0]就变为’1’,对应的ASCII 码为31*/</span>

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

ev_press = 1;<span style="color: rgb(255, 0, 0);"> /*设置中断标志为1*/

</span>wake_up_interruptible(&button_waitq); <span style="color: rgb(255, 0, 0);">/*唤醒等待队列*/

</span>}

return IRQ_RETVAL(IRQ_HANDLED);

}

<span style="color: rgb(255, 0, 0);">/*

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

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

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

*/

</span>static int s3c24xx_buttons_open(struct inode *inode, struct file *file)

{

int i;

int err = 0;

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

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

continue;

}

<span style="color: rgb(255, 0, 0);">/*注册中断函数*/

</span>err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH,

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

if (err)

break;

}

if (err) {

<span style="color: rgb(255, 0, 0);">/*如果出错,释放已经注册的中断,并返回*/

</span>i--;

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

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

continue;

}

disable_irq(button_irqs[i].irq);

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

}

return -EBUSY;

}

<span style="color: rgb(255, 0, 0);">/*注册成功,则中断队列标记为1,表示可以通过read 读取*/

</span>ev_press = 1;

<span style="color: rgb(255, 0, 0);">/*正常返回*/

</span>return 0;

}

<span style="color: rgb(255, 0, 0);">/*

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

处理函数

*/

</span>static int s3c24xx_buttons_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;

}

<span style="color: rgb(255, 0, 0);">/*释放中断号,并注销中断处理函数*/

</span>free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);

}

return 0;

}

<span style="color: rgb(255, 0, 0);">/*

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

*/

</span>static int s3c24xx_buttons_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)

<span style="color: rgb(255, 0, 0);">/*当中断标识为0 时,并且该设备是以非阻塞方式打开时,返回*/

</span>return -EAGAIN;

else

<span style="color: rgb(255, 0, 0);">/*当中断标识为0 时,并且该设备是以阻塞方式打开时,进入休眠状态,等待被唤醒*/

</span>wait_event_interruptible(button_waitq, ev_press);

}

<span style="color: rgb(255, 0, 0);">/*把中断标识清零*/

</span>ev_press = 0;

<span style="color: rgb(255, 0, 0);">/*一组键值被传递到用户空间*/

</span>err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));

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

}

static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)

{

unsigned int mask = 0;

<span style="color: rgb(255, 0, 0);">/*把调用poll 或者select 的进程挂入队列,以便被驱动程序唤醒*/</span>

poll_wait(file, &button_waitq, wait);

if (ev_press)

mask |= POLLIN | POLLRDNORM;

return mask;

}

<span style="color: rgb(255, 0, 0);">/*设备操作集*/

</span>static struct file_operations dev_fops = {

.owner = THIS_MODULE,

.open = s3c24xx_buttons_open,

.release = s3c24xx_buttons_close,

.read = s3c24xx_buttons_read,

.poll = s3c24xx_buttons_poll,

};

static struct miscdevice misc = {

.minor = MISC_DYNAMIC_MINOR,

.name = DEVICE_NAME,

.fops = &dev_fops,

};

<span style="color: rgb(255, 0, 0);">/*设备初始化,主要是注册设备*/

</span>static int __init dev_init(void)

{

int ret;

<span style="color: rgb(255, 0, 0);">/*把按键设备注册为misc 设备,其设备号是自动分配的*/

</span>ret = misc_register(&misc);

printk (DEVICE_NAME"\tinitialized\n");

return ret;

}

<span style="color: rgb(255, 0, 0);">/*注销设备*/

</span>static void __exit dev_exit(void)

{

misc_deregister(&misc);

}

module_init(dev_init); <span style="color: rgb(255, 0, 0);">//模块初始化,仅当使用insmod/podprobe 命令加载时有用,如果设备不是通过模块方

式加载,此处将不会被调用

</span>module_exit(dev_exit); <span style="color: rgb(255, 0, 0);">//卸载模块,当该设备通过模块方式加载后,可以通过rmmod 命令卸载,将调用此函</span><span style="color: rgb(255, 0, 0);">数

</span>MODULE_LICENSE("GPL");//<span style="color: rgb(255, 0, 0);">版权信息

</span>MODULE_AUTHOR("FriendlyARM Inc.");//<span style="color: rgb(255, 0, 0);">作者名字

3 把按键驱动加入内核

接下来,我们把按键驱动加入到内核中,打开linux-2.6.32.2_fa/drivers/char/Kconfig 文件,加入如下红色部分内容:

config MINI2440_BUTTONS

tristate "Buttons driver for FriendlyARM Mini2440 development boards"

depends on MACH_MINI2440

default y if MACH_MINI2440

help

this is buttons driver for FriendlyARM Mini2440 development boards

config MINI2440_BUZZER

tristate "Buzzer driver for FriendlyARM Mini2440 development boards"

depends on MACH_MINI2440

default y if MACH_MINI2440

help

this is buzzer driver for FriendlyARM Mini2440 development boards

接下来,再根据该驱动的配置定义,把对应的驱动目标文件加入内核中,打开linux-2.6.32.2/drivers/char/Makefile 文件,添加如下红色部分内容:

obj-$(CONFIG_JS_RTC) += js-rtc.o

js-rtc-y = rtc.o

obj-$(CONFIG_LEDS_MINI2440) += mini2440_leds.o

obj-$(CONFIG_MINI2440_BUTTONS) += mini2440_buttons.o

obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o

# Files generated that shall be removed upon make clean

clean-files := consolemap_deftbl.c defkeymap.c

这样,我们就在内核中添加做好了LED 驱动

4 配置编译新内核

接上面的步骤,在内核源代码目录下执行:make menuconfig 重新配置内核,依次选择进入如下子菜单项:

Device Drivers --->

Character devices --->

在此按空格键选择“<*> Buttons driver for FriendlyARM Mini2440 development boards(NEW) ”选项,并退出保存内核配置。在内核源代码根目录下执行;make zImage,把生成的新内核烧写到开发板中。

5 测试按键

为了测试上面的按键驱动,创建一个按键测试程序:btn-test.c,内容如下:

<span style="font-size: 18px;">#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'}; <span style="color: rgb(255, 0, 0);">//定义按键值变量,对于驱动函数中的key_values 数组

</span><span style="color: rgb(255, 0, 0);">/*打开按键设备/dev/buttons*/

</span>buttons_fd = open("/dev/buttons", 0);

if (buttons_fd < 0) {

<span style="color: rgb(255, 0, 0);">/*打开失败则退出*/

</span>perror("open device buttons");

exit(1);

}

<span style="color: rgb(255, 0, 0);">/*永读按键并打印键值和状态*/

</span>for (;;) {

char current_buttons[6];

int count_of_changed_key;

int i;

<span style="color: rgb(255, 0, 0);">/*使用read 函数读取一组按键值(6 个)*/

</span>if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {

perror("read buttons:");

exit(1);

}

<span style="color: rgb(255, 0, 0);">/*逐个分析读取到的按键值*/

</span>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];

<span style="color: rgb(255, 0, 0);">/*打印按键值,并标明按键按下/抬起的状态*/

</span>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");

}

}</span>

<span style="font-size: 18px;"><span style="color: rgb(255, 0, 0);">/*关闭按键设备文件*/

</span>close(buttons_fd);

return 0;

}</span>

以下是测试图片

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐