tq2440 按键中断 去抖 改进版 驱动

tq2440 按键中断 去抖 改进版 驱动  

http://chen4013874.blog.163.com/blog/static/2042430132012610104025511/

2012-07-10 22:40:25|  分类: ARM|字号 订阅

功能:
能实现按键中断。
实现去抖动功能,按键基本准确。
用寄存器的方式实现终端屏蔽,还有用端口读的方式来去抖,
和普通的单片机做法统一起来了。


/*************************************************************
基于tq2440开发板,内核2.6.30

功能实现按键中断,并且去抖还行

要去实现用寄存器控制,而不是调用系统给的函数

实现了中断屏蔽,但是读取intoffset寄存器貌似有问题。

实现端口的读。这样把普通单片机的控制思路转换到驱动设计中了。

多用ioremap 地址映射,少调用系统函数,加深自己对arm 的理解。

2012年7月10日22:16:37

**************************************************************/

#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>


#include <mach/regs-gpio.h>
#include <mach/hardware.h>

#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>

#define DEVICE_NAME "driver_button"


volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *srcpnd = NULL;
volatile unsigned long *intmask = NULL;
volatile unsigned long *intpnd = NULL;
volatile unsigned long *intoffset = NULL;

static volatile int ev_press = 0;

static volatile int press_cnt [] = {0, 0, 0, 0};

static unsigned int oft=32;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);


static irqreturn_t irq_interrupt(int irq, int dev_id)
{

//printk("-----------------drive irq_interrupt ok----------------\n");
ev_press=1; //用来指示中断进入了,并且用于唤醒中断

//*intmask|=(0x1<<0); //屏蔽中断有效果 ,果然能屏蔽掉

oft=dev_id;
// oft=*intoffset; //这能读出具体是哪个中断发生了

//貌似中断里面不能用msleep,不然会出错
//*intmask&=~(0x1<<0);//开中断

wake_up_interruptible(&button_waitq); 

//enable_irq(IRQ_EINT0);
return IRQ_RETVAL(IRQ_HANDLED);
}
       
// ------------------- OPEN ------------------------
ssize_t button_open (struct inode * inode ,struct file * file)
{

/*

//eint 1  
//gpf 1

*gpfcon &= ~(0x3<<(1*2));//清零
*gpfcon |=(0x2<<(1*2));//设置成中断模式
*srcpnd //0 中断未请求 1 中断已经请求
*intmask&=~(0x1<<1);//设置成中断eint1  服务有效
//intmod //默认irq 中断
//priority // 使用默认中断优先级
//intpnd //读取它用来知道哪个中断源被服务,这是用二进制显示。
//intoffset 读取它来显示intpnd中哪个中断被服务了,这是用10进制显示
*/

request_irq(IRQ_EINT0, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY1", 1);
request_irq(IRQ_EINT1, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY2", 2);
request_irq(IRQ_EINT2, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY3", 3);
request_irq(IRQ_EINT4, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY4", 4);
printk("-----------------drive button open ok----------------\n");
return 0;


}


// ------------------- RELEASE/CLOSE ---------------
ssize_t button_release (struct inode  * inode ,struct file * file)
{
free_irq(IRQ_EINT0, 1);
free_irq(IRQ_EINT1, 2);
free_irq(IRQ_EINT2, 3);
free_irq(IRQ_EINT4, 4);
return 0;
}


// ------------------- READ ------------------------
ssize_t button_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
//unsigned int i,j;
wait_event_interruptible(button_waitq, ev_press);
//只有产生中断后,才能到这里来
 /* 执行到这里时,ev_press等于1,将它清0 */
ev_press = 0;

*gpfcon &= ~(0x3<<(0*2)|0x3<<(1*2)|0x3<<(2*2)|0x3<<(4*2));
//清零把端口设置成输入模式

if( (*gpfdat&0x13)!=0 )//说明按键按下
{
msleep(200);
if( (*gpfdat&0x13)!=0 )//确认按键按下
{
printk("config %d key in!!!!\n",oft);
switch(oft)
{
//0  1  2  4
//f0 f1 f2 f4
case 1:
press_cnt[0]++;break;
case 2:
press_cnt[1]++;break;
case 3:
press_cnt[2]++;break;
case 4:
press_cnt[3]++;break;
default:
printk("unknow interrupt \n");break;
}
}
}
*gpfcon &= ~(0x3<<(0*2)|0x3<<(1*2)|0x3<<(2*2)|0x3<<(4*2));//清零
*gpfcon |=(0x2<<(0*2))|(0x2<<(1*2))|(0x2<<(2*2))|(0x2<<(4*2));
//设置成中断模式
copy_to_user( buf,(const void *)press_cnt,sizeof(press_cnt) );
return 0;
}


// ------------------- WRITE -----------------------
ssize_t button_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{

return 0;
}

// ------------------- IOCTL -----------------------
ssize_t button_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long arg)
{

return 0;
}


// -------------------------------------------------

static struct file_operations button_ops ={

.owner  = THIS_MODULE,
.open   = button_open,
.read    = button_read,
.write   = button_write,
.ioctl    = button_ioctl,
.release = button_release,
};


static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &button_ops,
};


static int __init init_button_init(void)  
{
int ret;
ret = misc_register(&misc);
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = (volatile unsigned long *)ioremap(0x56000054, 8);
srcpnd = (volatile unsigned long *)ioremap(0x4a000000, 32);
intmask=(volatile unsigned long *)ioremap(0x4a000008, 32);
intpnd =(volatile unsigned long *)ioremap(0x4a000010, 32);
intoffset=(volatile unsigned long *)ioremap(0x4a000014, 32);
printk("-----------------drive button init ok----------------\n");
    return 0;
}

static void __exit exit_button_ctl(void)
{
misc_deregister(&misc);
}



module_init(init_button_init);
module_exit(exit_button_ctl);
MODULE_LICENSE("GPL");








#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <fcntl.h>      // open() close()
#include <unistd.h>     // read() write()

#define DEVICE_NAME "/dev/driver_button"

//------------------------------------- main ---------------------------------------------
int main(int argc, char **argv)
{
int fd,ret;

int cnt=0;

unsigned int key_val[4];
       fd = open(DEVICE_NAME, O_RDWR);
 
if (fd == -1)
{
printf("can't open device mknod %s c zhu ci \n",DEVICE_NAME);
return 0;
}

while(1)
{
read(fd,key_val,sizeof(key_val));

printf(" key_val=  %d  %d  %d  %d\n",key_val[0],key_val[1],key_val[2],key_val[3]);
}
        // close 
ret = close(fd);
printf ("close gpio_led_driver test\n");

        return 0;
}// end main
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值