#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include<linux/timer.h>
#include <plat/irqs.h>
#include<linux/jiffies.h>
#include <linux/delay.h>
static volatile unsigned int *GPJ2CON;
static volatile unsigned int *GPJ2DAT;
static volatile unsigned int *GPH2CON;
static volatile unsigned int *GPH2DAT;
static int major;
irqreturn_t key_interr(int irqno,void *dev)
{
static int flag = 0;
if( !( (*GPH2DAT) & 1 ))
printk("hello keyboard\n");
flag = ~flag;
if(0 == flag)
*GPJ2DAT &= 0xf0;//点亮LED
else
*GPJ2DAT |= 0x0f;//熄灭LED
return IRQ_HANDLED;
}
static int led_open(struct inode *pi, struct file *pf)
{
GPJ2CON = ioremap(0xe0200280,8);
GPJ2DAT = GPJ2CON + 1;
GPH2CON = ioremap(0xe0200c40,8);
GPH2DAT = GPH2CON+1;
*GPJ2CON &= 0xffff0000;
*GPJ2CON |= 0x00001111; //设置为输出
*GPJ2DAT |= 0x0f; //熄灭所有LED
*GPH2CON |= 0x0000000f; //K1键设置为外部中断
request_irq(IRQ_EINT(16),key_interr,IRQF_TRIGGER_FALLING,"key1",NULL);//中断请求函数
return 0;
}
static ssize_t led_write(struct file *pf, const char __user *pbuf, size_t len, loff_t *ppos)
{
return 0;
}
ssize_t led_read (struct file *pf, char __user *pbuf, size_t count, loff_t *off)
{
return 0;
}
static int led_release(struct inode *pi, struct file *pf)
{
printk("<kernel> test release.\n");
return 0;
}
static struct file_operations t_fops=
{
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.write = led_write,
.read = led_read,
};
static __init int led_init(void)
{
major = register_chrdev(0,"LED",&t_fops);
printk("major = %d\n",major);
return 0;
}
static __exit void led_exit(void)
{
iounmap(GPJ2CON);
iounmap(GPH2CON);
unregister_chrdev(major,"LED");
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Huang Dezhi");
MODULE_DESCRIPTION("This is the interrupt");
把.ko文件下载到开发板上,装载,创建设备结点
输入:echo > led(设备名)
每次按键(没有消抖)将会看到:
按下按键时,打印“hello keyboard”
按下按键时,将灯点亮,再按下按键,将灯熄灭。