(驱动)(中断)简单的外部(按键)中断练习

#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”

       按下按键时,将灯点亮,再按下按键,将灯熄灭。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值