EPC-6708 按键驱动
来源:互联网 发布:称重软件数据库 编辑:程序博客网 时间:2024/06/10 18:53
* linux/drivers/char/tiny4412_buttons.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#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/sched.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <linux/gpio.h>
#include <mach/gpio.h>
#define DEVICE_NAME "mybutton"
struct button_desc {
int gpio;
int number;
char *name;
struct timer_list timer;
};
static struct button_desc buttons[] = {
{ IMX_GPIO_NR(1,0), 0, "KEY0" },
{ IMX_GPIO_NR(1,2), 1, "KEY1" },
{ IMX_GPIO_NR(1,4), 2, "KEY2" },
{ IMX_GPIO_NR(1,5), 3, "KEY3" },
};
static volatile char key_values[] = {
'0', '0', '0', '0', '0', '0', '0', '0'
};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static void tiny4412_buttons_timer(unsigned long _data)
{
struct button_desc *bdata = (struct button_desc *)_data;
int down;
int number;
unsigned tmp;
tmp = gpio_get_value(bdata->gpio);
/* active low */
down = !tmp;
printk(KERN_DEBUG "KEY %d: %08x\n", bdata->number, down);
number = bdata->number;
if (down != (key_values[number] & 1)) {
key_values[number] = '0' + down;
ev_press = 1;
wake_up_interruptible(&button_waitq);
}
}
static irqreturn_t button_interrupt(int irq, void *dev_id)
{
struct button_desc *bdata = (struct button_desc *)dev_id;
mod_timer(&bdata->timer, jiffies + msecs_to_jiffies(40));
return IRQ_HANDLED;
}
static int tiny4412_buttons_open(struct inode *inode, struct file *file)
{
int irq;
int i;
int err = 0;
for (i = 0; i < ARRAY_SIZE(buttons); i++) {
if (!buttons[i].gpio)
continue;
//初始化定时器。
setup_timer(&buttons[i].timer, tiny4412_buttons_timer,
(unsigned long)&buttons[i]);
irq = gpio_to_irq(buttons[i].gpio);
//上升沿和下降沿触发
err = request_irq(irq, button_interrupt, IRQ_TYPE_EDGE_BOTH,
buttons[i].name, (void *)&buttons[i]);
if (err)
break;
}
if (err) {
i--;
for (; i >= 0; i--) {
if (!buttons[i].gpio)
continue;
irq = gpio_to_irq(buttons[i].gpio);
disable_irq(irq);
free_irq(irq, (void *)&buttons[i]);
del_timer_sync(&buttons[i].timer);
}
return -EBUSY;
}
ev_press = 1;
return 0;
}
static int tiny4412_buttons_close(struct inode *inode, struct file *file)
{
int irq, i;
for (i = 0; i < ARRAY_SIZE(buttons); i++) {
if (!buttons[i].gpio)
continue;
irq = gpio_to_irq(buttons[i].gpio);
free_irq(irq, (void *)&buttons[i]);
del_timer_sync(&buttons[i].timer);
}
return 0;
}
static int tiny4412_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)
return -EAGAIN;
else
wait_event_interruptible(button_waitq, ev_press);
}
ev_press = 0;
err = copy_to_user((void *)buff, (const void *)(&key_values),
min(sizeof(key_values), count));
return err ? -EFAULT : min(sizeof(key_values), count);
}
static unsigned int tiny4412_buttons_poll( struct file *file,
struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = tiny4412_buttons_open,
.release = tiny4412_buttons_close,
.read = tiny4412_buttons_read,
.poll = tiny4412_buttons_poll,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init button_dev_init(void)
{
int ret;
ret = misc_register(&misc);
printk(DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit button_dev_exit(void)
{
misc_deregister(&misc);
}
module_init(button_dev_init);
module_exit(button_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
Makefile
ifneq ($(KERNELRELEASE),)
obj-m:=my4412_buttons.o
else
#KDIR := /lib/modules/$(shell uname -r)/build
KDIR = /home/xxq/kernel-imx/
PWD := $(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm *.mod.c *.o *.ko *.order *.symvers
endif
#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'};
buttons_fd = open("/dev/mybutton", 0);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}
for (;;) {
char current_buttons[6];
int count_of_changed_key;
int i;
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;
}
测试说明:
1.文件目录make ARCH=arm CROSS_COMPILE=arm-linux- ,生成my4412_buttons.ko
2.arm-none-linux-gnueabi-gcc test.c -o test 生成test
2.加载驱动模块:insmod my4412_buttons.ko
3.运行test测试。
- EPC-6708 按键驱动
- EPC-6708 linux 字符设备驱动 读取小灯
- EPC-6708接口调试
- EPC-事件驱动的流程链(转载)
- 按键驱动
- 按键驱动
- 按键驱动
- 按键驱动
- 按键驱动
- 按键驱动
- Linux驱动---按键驱动
- 事件驱动的工作流过程链模型EPC
- 按键驱动(浏览按键)
- EPC-6708 arm 开发板常用命令等
- 2.EPC-6708 应用程序开发环境搭建
- linux驱动之按键驱动
- 4412驱动-key 按键驱动
- wince 按键驱动
- 什么时候用DFS,什么时候用BFS?
- NSKeyedArchiver
- 剑指offer题解 两个链表的第一个公共结点
- MySQL行级锁、表级锁、页级锁详细介绍
- 6410 内核的uboot-分析笔记
- EPC-6708 按键驱动
- 几款实用android studio插件
- Linux创建、删除 文件和文件夹
- Block在界面之间的基本使用(传值操作, 方法嵌套)
- text()/html()/val()的区别
- javaweb学习总结(十一)——使用Cookie进行会话管理
- PHP调用elasticsearch的_update_by_query接口
- Ubuntu下FastDFS安装以及配置
- 自定义选择器(同时支持单滚轮和双滚轮联动选择器)