驱动代码如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <mach/gpio-fns.h>
#include <mach/gpio-nrs.h>
int major=0;
struct class *buttons_class;
struct device *buttons_dev;
unsigned int key_val=0;
wait_queue_head_t button_wait;
static volatile int flag_press=0;
static irqreturn_t buttons_ISR(int irq, void *dev_id)
{
switch(irq)
{
case IRQ_EINT0:
if( s3c2410_gpio_getpin( S3C2410_GPF(0) ) )
{
key_val = 0x01;
printk("key_val = 0x%x\n", key_val);
}
else
{
key_val = 0x81;
printk("key_val = 0x%x\n", key_val);
}
break;
case IRQ_EINT2:
if( s3c2410_gpio_getpin( S3C2410_GPF(2) ) )
key_val = 0x02;
else
key_val = 0x82;
break;
case IRQ_EINT11:
if( s3c2410_gpio_getpin(S3C2410_GPG(3) ) )
key_val = 0x03;
else
key_val = 0x83;
break;
case IRQ_EINT19:
if( s3c2410_gpio_getpin( S3C2410_GPG(11) ) )
key_val = 0x04;
else
key_val = 0x84;
break;
default:
break;
}
flag_press = 1;
wake_up_interruptible(&button_wait);
return IRQ_HANDLED;
}
static int buttons_open(struct inode *inode,struct file *file)
{
request_irq(IRQ_EINT0, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button0", 1);
request_irq(IRQ_EINT2, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button2", 1);
request_irq(IRQ_EINT11, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button11", 1);
request_irq(IRQ_EINT19, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button19", 1);
init_waitqueue_head(&button_wait);
return 0;
}
static int buttons_close(struct inode *inode,struct file *file)
{
free_irq(IRQ_EINT0,1);
free_irq(IRQ_EINT2,1);
free_irq(IRQ_EINT11,1);
free_irq(IRQ_EINT19,1);
return 0;
}
ssize_t buttons_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if(size != sizeof(key_val))
{
return -EINVAL;
}
wait_event_interruptible(button_wait, flag_press);
copy_to_user(buf, &key_val, 1);
flag_press = 0;
return sizeof(key_val);
}
const struct file_operations buttons_fops = {
.owner = THIS_MODULE,
.open = buttons_open,
.read = buttons_read,
.release = buttons_close,
};
static int buttons_init(void)
{
major = register_chrdev(0,"buttons",&buttons_fops);
buttons_class = class_create(THIS_MODULE, "my_buttons");
buttons_dev = device_create(buttons_class, NULL, MKDEV(major,0), NULL, "buttons");
/* gpfcon = (volatile unsigned long *)ioremap(0x56000050 ,8);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060 ,8);
gpgdat = gpgcon + 1;
*/
printk("buttons driver initing...\n");
return 0;
}
static void buttons_exit(void)
{
unregister_chrdev(0,"buttons");
device_unregister(buttons_dev);
class_destroy(buttons_class);
// iounmap(gpfcon);
// iounmap(gpgcon);
printk("Bye,buttons.\n");
}
module_init(buttons_init);
module_exit(buttons_exit);
MODULE_LICENSE("GPL");
测试程序代码如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
int key_val=0;
fd = open("/dev/buttons",O_RDWR);
if(fd < 0)
{
printf("open error!\n");
}
while(1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x \n",key_val);
}
return 0;
}
请高手给我看看啊,终端一直向外打印“key_val = 0x0”,看了半天没看出问题啊?